Pagefactory在页面对象结构

时间:2017-07-07 07:58:07

标签: java selenium selenium-webdriver

我在我的Page对象模型自动化框架中实现了pagefactory。我现有的框架工作正常,没有pagefactory。现在我正在实现pagefactory来调用元素。我正面临一个问题,其中Pagefactory元素抛出空值。 我怀疑这可能是因为施工人员不确定。

我已经构建了一个基于页面对象模型的框架 包装如下: - com.automationframework com.configutaion com.pageObject com.testcases com.testsuites

我的testsuites类调用testcases包中的测试用例,并在automationframework中调用驱动程序和其他基于框架的类。 我知道很难通过阅读一些内容来获取错误,但可能有人遇到同样的问题并且可以帮助我。

我的Page类如下所示: -

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;
}

我的测试用例类: -

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

我收到如下错误: -

  

java.lang.NullPointerException at   org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)     在   org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)     在com.sun.proxy。$ Proxy5.click(未知来源)at   com.cc.automation.testcases.SyncTest.syncFeature(SyncTest.java:130)     在   com.cc.automation.testcases.SyncTest.verifyTestCases(SyncTest.java:60)     在   com.cc.automation.automationframework.AutomationTestCaseVerification.Execute(AutomationTestCaseVerification.java:58)     在   com.cc.automation.testsuites.SynchronizationTest.testSync(SynchronizationTest.java:22)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)     在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)     在   org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)     在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 at   org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)at 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 at   org.testng.TestNG.runSuitesLocally(TestNG.java:1226)at 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)     在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

我也试过在下面申请,但仍然没有工作: -

PageFactory.initElements(this.driver,this);

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以通过在SynchronizationPage类的构造函数中调用PageFactory.initElements(driver, this);来尝试一次。