0

I am implementing pagefactory in my Page object model automation framework. My existing framework works fine without pagefactory. Now I am implementing pagefactory to invoke elements. I am facing a issue where Pagefactory element throwing null values. I have a doubt that it may be because of constructors not sure.

I have build a framework based on Page Object Model Have packages as below :- com.automationframework com.configutaion com.pageObject com.testcases com.testsuites

My testsuites classes invokes testcases present in testcases package and invoke driver and other framework based classes in automationframework. I know it's hard to grab the error by simply read few stuff but may anyone faced same issue and can help me.

My Page class looks something like below :-

public class SynchronizationPage extends Page {

    ExcelLib xl = new ExcelLib();

    private WebElement element = null;

    public SynchronizationPage(WebDriver driver) {
        super(driver);
    }

    protected boolean isSecured() {
        return true;
    }

    @FindBy(how = How.XPATH, using = "//a[@class='header-font']/span[@class='icon-cog']")
    @CacheLookup
    public WebElement HoverOnSettings;
}

My testcase class :-

public class SyncTest extends AutomationTestCaseVerification {

//  SynchronizationPage sync = new SynchronizationPage(Page.driver);
    SignOutPage signout = new SignOutPage(Page.driver);
    ExcelLib xl = new ExcelLib();
    SynchronizationPage synccc = PageFactory.initElements(Page.driver, SynchronizationPage.class);

    private WebElement element = null;
    private WebDriver driver;

    public SyncTest() {
        super();
    }


    @Override
    protected void verifyTestCases() throws Exception {
        syncFeature();
    }

    public void syncFeature() throws Exception {

        try {
         WebDriverWait waits = new WebDriverWait(Page.driver, 60);
    //   waits.until(ExpectedConditions.elementToBeClickable(synccc.clickOnOffButton)).click();
         synccc.clickOnOffButton.click();
         System.out.println("Yes I clicked");
        }
        catch(Exception ex)
        {
            System.out.println("error ="+ex.getMessage());
            }

    }
}

enter image description here

enter image description here

I am getting error like below :-

java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) at com.sun.proxy.$Proxy5.click(Unknown Source) at com.cc.automation.testcases.SyncTest.syncFeature(SyncTest.java:130) at com.cc.automation.testcases.SyncTest.verifyTestCases(SyncTest.java:60) at com.cc.automation.automationframework.AutomationTestCaseVerification.Execute(AutomationTestCaseVerification.java:58) at com.cc.automation.testsuites.SynchronizationTest.testSync(SynchronizationTest.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108) at org.testng.internal.Invoker.invokeMethod(Invoker.java:661) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:744) at org.testng.TestRunner.run(TestRunner.java:602) at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) at org.testng.SuiteRunner.run(SuiteRunner.java:289) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) at org.testng.TestNG.runSuites(TestNG.java:1144) at org.testng.TestNG.run(TestNG.java:1115) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

I have also tried to apply below but still not working :-

PageFactory.initElements(this.driver, this);

enter image description here

enter image description here

16
  • instead of printing ex.getMessage() - use ex.printStackTrace() to show error and add it to the topic Commented Jul 7, 2017 at 8:07
  • Thanks for response @Vitaliy .. I have added the stacktrace Commented Jul 7, 2017 at 8:33
  • Error is in com.cc.automation.testcases.SyncTest.syncFeature(SyncTest.java:130). Which line exactly is that? Commented Jul 7, 2017 at 8:52
  • you've got nullPointerException on line synccc.clickOnOffButton.click(); Stop test in debug on that place and check if synccc object is not null , same with clickOnOffButton. Where it initialized? Probably clickOnOffButton is null. Commented Jul 7, 2017 at 8:58
  • 1
    in debug purpose try to do in debug "driver.findElement(By.xpath(...)).isDisplayed()" and provide OffButton locator there. I think element is just not found, or you haven't waited enough for it to load Commented Jul 7, 2017 at 9:18

1 Answer 1

1

Can you try once by calling PageFactory.initElements(driver, this); inside the constructor of your SynchronizationPage class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.