我正在尝试使用selenium webdriver,testng和页面工厂自动化。但我面临空指针异常

时间:2018-04-12 05:22:02

标签: java selenium-webdriver

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.$Proxy12.sendKeys(Unknown Source)
    at Place_Order.Place_Reservation(Place_Order.java:24)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

浏览器类

package pageobjpattern;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterSuite;

import org.testng.annotations.BeforeSuite;

public class Browser {

    public static WebDriver driver ;

    @BeforeSuite
    public void setUp() 
    {
        /*Initiate driver*/
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
        //System.setProperty("webdriver.gecko.driver", "E:\\Selenium\\geckodriver-v0.20.0-win64\\geckodriver.exe");
        driver= new ChromeDriver();
        driver.get("url");
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @AfterSuite
    //Test cleanup
    public void TeardownTest()
    {
       Browser.driver.quit();
    }
}

登录页面课程

public class loginpage {
    WebDriver driver;

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

    @FindBy(xpath="//*[@id=\"txtUserId\"]")
    WebElement loginid;

    @FindBy(xpath="//*[@id=\"txtPassword\"]")
    WebElement loginPassword;

    public WebElement userid()  {   
        return loginid;
    }
    public WebElement userPassword(){
        return loginPassword;
    }
}

Place_Order Class

public class Place_Order extends Browser{

    WebDriver driver;

    @Test
    public void Place_Reservation() throws Exception
    {
        loginpage raku = PageFactory.initElements(driver, loginpage.class);
        raku.userid().sendKeys("fp-ifttest");
        raku.userPassword().sendKeys("ift");
    }
}

1 个答案:

答案 0 :(得分:1)

修改测试类中的代码,因为您没有正确初始化驱动程序,而是调用pageobject的构造函数。

@Test
public void Place_Reservation() throws Exception
{
    **loginpage raku = new loginpage(Browser.driver);**
    raku.userid().sendKeys("fp-ifttest");
    raku.userPassword().sendKeys("ift");
}

还尝试遵循Java命名标准,例如使用大写字母作为类名等的开始。