通过Selenium运行testcase时的java.lang.NullPointerException

时间:2016-07-21 07:06:17

标签: java selenium selenium-webdriver nullpointerexception testng

我遇到了NullPointerException的问题。我尽可能多地尝试,但我无法解决这个问题。我正在为我的项目实现POM模型(Selenium)。

页面:

public class VendorsHomePageApp 
{
    WebDriver driver;

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

    @FindBy(how=How.XPATH,using=".//*[@id='navbarCollapse']/ul[1]/li[1]/a")
    WebElement dashboardTab;

    @FindBy(how=How.XPATH,using=".//*[@id='navbarCollapse']/ul[1]/li[2]/a")
    WebElement tendersTab;

    @FindBy(how=How.XPATH,using=".//*[@id='navbarCollapse']/ul[1]/li[3]/a")
    WebElement notificationsTab;

    @FindBy(how=How.XPATH,using=".//*[@id='navbarCollapse']/ul[1]/li[4]/a")
    WebElement profileTab;

    @FindBy(how=How.XPATH,using=".//*[@id='navbarCollapse']/ul[2]/li/a")
    WebElement vendorName;

    @FindBy(how=How.XPATH,using=".//*[@id='navbarCollapse']/ul[2]/li/ul/li[1]/a")
    WebElement vendorHelp;

    @FindBy(how=How.XPATH,using=".//*[@id='navbarCollapse']/ul[2]/li/ul/li[3]/a")
    WebElement vendorSignOut;

    @FindBy(how=How.XPATH,using="html/body/section/div/div/div/div[2]/div/table/tbody/tr[1]/td[1]/a")
    WebElement firstTender;

    @FindBy(how=How.XPATH,using="html/body/section/div/div/div/div[2]/div/table/tbody/tr[2]/td[1]/a")
    WebElement secondTender;

    @FindBy(how=How.XPATH,using="html/body/section/div/div/div/div[2]/div/table/tbody/tr[3]/td[1]/a")
    WebElement thirdTender;

    public void clickOnDashboardTab()
    {
        dashboardTab.click();
    }

    public void clickOnTendersTab()
    {
        tendersTab.click();
    }

    public void clickOnNotificationsTab()
    {
        notificationsTab.click();
    }

    public void clickOnProfileTab()
    {
        profileTab.click();
    }

    public void clickOnFirstTender()
    {
        firstTender.click();
    }
}

识别TestClass:

public class VendorsHomePageTest 
{
    public WebDriver driver;
    @Test
    public void verifyVendorsHomePageTest() throws Exception
    {
        LoginIntoVendors login=PageFactory.initElements(driver, LoginIntoVendors.class);
        login.verifyLoginVendors();

        VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        Thread.sleep(3000);

        vhpapp.clickOnNotificationsTab();
        vhpapp.clickOnProfileTab();
        vhpapp.clickOnTendersTab();
        vhpapp.clickOnDashboardTab();
        vhpapp.clickOnFirstTender();
    }
}

错误日志:

FAILED: verifyVendorsHomePageTest
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.$Proxy4.click(Unknown Source)
    at com.digitalmqc.automation.vendorspages.VendorsHomePageApp.clickOnNotificationsTab(VendorsHomePageApp.java:60)
    at com.digitalmqc.automation.vendorstests.VendorsHomePageTest.verifyVendorsHomePageTest(VendorsHomePageTest.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)

类别:

public class LoginIntoVendors 
{
    @Test
    public void verifyLoginVendors() throws Exception
    {
        WebDriver driver= new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("******");

        IntiationPage vendorInit=PageFactory.initElements(driver, IntiationPage.class);

        vendorInit.clickOnLoginButton();

        VendorsLoginAction Loginven=PageFactory.initElements(driver, VendorsLoginAction.class);

        Loginven.vendorlogin("***","***@gmail.com", "****");

        String title = driver.getTitle();
        System.out.println("Title is  :" + title  );
   }
}

任何帮助?

2 个答案:

答案 0 :(得分:1)

您需要先初始化WebDriver。当我看到你正在创建WebDriver的引用但未初始化时,请尝试如下: -

WebDriver driver = new ChromeDriver(); //or other driver which you want

注意: - 如果要初始化ChromeDriver,则需要先下载chromedriver并在初始化驱动程序之前设置系统属性: -

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

详情如下: -

public class VendorsHomePageTest 

{
    public WebDriver driver;
    @Test
    public void verifyVendorsHomePageTest() throws Exception
    {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver(); //or other driver which you want

        LoginIntoVendors login=PageFactory.initElements(driver, LoginIntoVendors.class);

        login.verifyLoginVendors();

        VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);

        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        Thread.sleep(3000);

        vhpapp.clickOnNotificationsTab();

        vhpapp.clickOnProfileTab();

        vhpapp.clickOnTendersTab();

        vhpapp.clickOnDashboardTab();

        vhpapp.clickOnFirstTender();

    }

}

已修改: - 您需要创建单独的类,为您提供WebDriver实例,如下所示: -

public class DriverInit {

    public WebDriver driver;
    private static DriverInit driverInit = null;

    public static DriverInit getInstance() {
        if (driverInit == null) {
            driverInit = new DriverInit();
        }
        return driverInit;
    }

    private DriverInit() {
        this.driver = new FirefoxDriver();
        this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        this.driver.get("******");
    }

    public WebDriver getDriver() {
        return this.driver;
    }

现在您可以将其调用LoginIntoVendors,如下所示: -

public class LoginIntoVendors 
{
    @Test
       public void verifyLoginVendors() throws Exception
       {
           WebDriver driver = DriverInit.getInstance().getDriver();

           IntiationPage vendorInit=PageFactory.initElements(driver, IntiationPage.class);

           vendorInit.clickOnLoginButton();

           VendorsLoginAction Loginven=PageFactory.initElements(driver, VendorsLoginAction.class);

           Loginven.vendorlogin("***","***@gmail.com", "****");

           String title = driver.getTitle();
           System.out.println("Title is  :" + title  );


       }
}

并在VendorsHomePageTest中,如下所示: -

public class VendorsHomePageTest 

{
    public WebDriver driver;
    @Test
    public void verifyVendorsHomePageTest() throws Exception
    {

        driver = DriverInit.getInstance().getDriver();

        LoginIntoVendors login=PageFactory.initElements(driver, LoginIntoVendors.class);

        login.verifyLoginVendors();

        VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);

        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        Thread.sleep(3000);

        vhpapp.clickOnNotificationsTab();

        vhpapp.clickOnProfileTab();

        vhpapp.clickOnTendersTab();

        vhpapp.clickOnDashboardTab();

        vhpapp.clickOnFirstTender();

    }

}

希望它有所帮助.. :)

答案 1 :(得分:0)

您的驱动程序似乎是null您可以采取哪些措施来避免这种情况是instantiate您的驱动程序参考。

在您的测试类中,您可以实例化您的驱动程序。

public class VendorsHomePageTest {
    public WebDriver driver;
    @Test
    public void verifyVendorsHomePageTest() throws Exception {
        driver = new FirefoxDriver(); // or whatever browser you want.
        // Here you can initialise the elements in `PageObject` class.
        VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);
        // Rest of your code
    }
}