2017-08-30 70 views
0

Selenium pageFactory NullPointerException。任何幫助將不勝感激。它適用於登錄setUp(),但在此之後,驅動程序出現null。Selenium pageFactory NullPointerException for driver

public class TestBase { 

    public WebDriver driver; 
    String url = PropertiesFile.readPropertiesFile("autUrl"); 

    public WebDriver getDriver() { 
     System.setProperty("webdriver.chrome.driver", 
     System.getProperty("user.dir") + "/driver/chromedriver.exe"); 

     driver = new ChromeDriver(); 

     return driver; 
    } 

    public void getUrl(String url) { 
     driver.get(url); 
     driver.manage().window().maximize(); 
    } 

    public void init() { 
     getDriver(); 
     getUrl(url); 
    } 
} 



public class LogInPage extends TestBase { 
    WebDriver driver; 
    @FindBy(id = "loginEmail")public WebElement userName_field; 
    @FindBy(name = "password")public WebElement password_field; 
    @FindBy(xpath = "//*[@id='main-content']/aside/div/form/input[2]")public WebElement SignMeIn_btn; 

    public LogInPage(WebDriver driver) { 
     this.driver = driver; 
     PageFactory.initElements(driver, this); 
    } 

    // Login 
    public void logIn(String userName, String password) { 
     userName_field.sendKeys(userName); 
     password_field.sendKeys(password); 
     SignMeIn_btn.click(); 
    } 

} 


    public class LogIn extends TestBase { 

    LogInPage logInPage; 

    private String user = PropertiesFile.readPropertiesFile("user"); 
    private String password = PropertiesFile.readPropertiesFile("password"); 



    public LogIn(WebDriver driver) { 
     this.driver = driver; 
    } 


    public void setUp(){ 
     init(); 
    } 


    public void logIn(){ 
     logInPage = new LogInPage(driver); 
     logInPage.logIn(user, password); 
    } 

} 

public class PortalsPage extends TestBase { 

    WebDriver driver; 

    @FindBy(id = "loginEmail") public WebElement userName_field; 
    @FindBy(xpath=".//*[@id='tabDetail']") public WebElement tenantPage_li; 
    @FindBy(id="tabDetail") public WebElement ownerPage_li; 
    @FindBy(xpath="//a[contains(@href,'tenant.action')]") public WebElement tenantPortal_link; 
    @FindBy(xpath="//a[contains(@href,'owner.action')]") public WebElement ownerPortal_link; 

    public PortalsPage(WebDriver driver){ 
     this.driver = driver; 
     PageFactory.initElements(driver, this); 
    } 


    public void goToPortals(){  
     userName_field.sendKeys("a"); 
     tenantPage_li.click(); 
    } 

} 

public class Portals extends TestBase { 
    PortalsPage portals; 
    WebDriver driver; 

    @BeforeClass 
    public void setUp(){ 
     LogIn login = new LogIn(driver); 
     login.setUp(); 
     login.logIn(); 

    } 

    @Test 
    public void goToPortal(){ 
     portals = new PortalsPage(driver); 
     portals.goToPortals(); 
    } 
} 

以下異常我:

失敗:goToPortal顯示java.lang.NullPointerException在 org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69) 在 org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) at com.sun.proxy。$ Proxy6.sendKeys(Unknown Source)at com.demo.pages.PortalsPage.goToPortals (PortalsPage.java:30) com.demo.control.Portals.goToPortal(Portals.java:28)處 sun.reflect.NativeMethodAccessorImpl.invoke(未知來源) sun.reflect.NativeMethodAccessorImpl.invoke0(本機方法)在 sun.reflect.DelegatingMethodAccessorImpl .invoke(Unknown Source) java.lang.reflect.Method.invoke(Unknown Source)at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) at org.testng.internal.Invoker.invokeMethod (Invoker.java:714)處 org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)在 org.testng.internal .TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767)at org.testng.TestRunner.run(TestRunner.java:617)at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)在 org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)在 org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)在 組織.testng.SuiteRunner.run(SuiteRunner.java:240)at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)at org.testng .TestNG.runSuitesSequentially(TestNG.java:1198)at org.testng.TestNG.runSuitesLocally(TestNG.java: 1123)在 org.testng.TestNG.run(TestNG.java:1031)在 org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) 在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG。 Java的:230) 在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

+0

TestBase的外觀如何?爲什麼所有的類都擴展它,並且除了Login外,它們都有自己的驅動程序實例? –

+0

基本上我想從TestBase獲取驅動程序的實例。我試圖在門戶類中使用或不使用本地類實例。嘗試了兩種方法並獲得了相同的結果。 – ktmrocks

+0

我也添加了TestBase類代碼 – ktmrocks

回答

2

您目前的做法似乎是一個有點令人費解。但是,如果不深入探討可以用來修復整體設計的其他方法的細節,那麼應該使用清理後的代碼版本。

這裏的想法是,您只需要頁面類(以*Page代表特定頁面,並公開一些可在該特定頁面上執行的業務功能的頁面類)和測試類,以及測試類本身從TestBase

所以這裏延伸的類

LoginPage。的java

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.FindBy; 
import org.openqa.selenium.support.PageFactory; 

public class LogInPage { 
    @FindBy(id = "loginEmail") 
    private WebElement userNameTextField; 
    @FindBy(name = "password") 
    private WebElement passwordTextField; 
    @FindBy(xpath = "//*[@id='main-content']/aside/div/form/input[2]") 
    private WebElement signInButton; 

    public LogInPage(WebDriver driver) { 
     PageFactory.initElements(driver, this); 
    } 

    public void logIn(String userName, String password) { 
     userNameTextField.sendKeys(userName); 
     passwordTextField.sendKeys(password); 
     signInButton.click(); 
    } 

} 

PortalsPage.java

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.FindBy; 
import org.openqa.selenium.support.PageFactory; 

public class PortalsPage { 
    @FindBy(id = "loginEmail") 
    private WebElement usernameTextField; 
    @FindBy(xpath = ".//*[@id='tabDetail']") 
    private WebElement tenantPage; 
    @FindBy(id = "tabDetail") 
    private WebElement ownerPage; 
    @FindBy(xpath = "//a[contains(@href,'tenant.action')]") 
    private WebElement tenantPortalLink; 
    @FindBy(xpath = "//a[contains(@href,'owner.action')]") 
    private WebElement ownerPortalLink; 


    public PortalsPage(WebDriver driver) { 
     PageFactory.initElements(driver, this); 
    } 

    public void goToPortals() { 
     usernameTextField.sendKeys("a"); 
     tenantPage.click(); 
    } 
} 

TestBase.java

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 

public class TestBase { 
    private WebDriver driver; 

    protected WebDriver getDriver() { 
     if (driver != null) { 
      return driver; 
     } 
     System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/driver/chromedriver.exe"); 
     driver = new ChromeDriver(); 
     return driver; 
    } 

    public void getUrl(String url) { 
     getDriver().get(url); 
     getDriver().manage().window().maximize(); 
    } 

} 

PortalsTest.java

import org.testng.annotations.BeforeClass; 
import org.testng.annotations.Test; 

public class PortalsTest extends TestBase { 
    private PortalsPage portals; 
    private String user = PropertiesFile.readPropertiesFile("user"); 
    private String password = PropertiesFile.readPropertiesFile("password"); 
    private String url = PropertiesFile.readPropertiesFile("autUrl"); 


    @BeforeClass 
    public void setUp() { 
     LogInPage login = new LogInPage(getDriver()); 
     getUrl(url); 
     login.logIn(user, password); 
    } 

    @Test 
    public void goToPortal() { 
     portals = new PortalsPage(getDriver()); 
     portals.goToPortals(); 
    } 
} 
+0

太棒了!感謝克裏希南。感謝您對設計的反饋。 – ktmrocks

相關問題