当前位置: 首页 > 面试题库 >

通过Selenium运行测试用例时,java.lang.NullPointerException

郎飞龙
2023-03-14
问题内容

我遇到了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();
    }
}

测试类:

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

有什么帮助吗?


问题答案:

您需要先初始化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();

    }

}

希望能帮助到你..:)



 类似资料:
  • 问题内容: 我有一台服务器A,上面装有eclipse,testNG,selenium和其他东西。我有另一个服务器B,上面装有Bamboo设置。 我能够在服务器A上运行一个testNG脚本,该脚本在我正在开发的Web应用程序上运行一组selenium测试。我使用了eclipse中显示的vm属性中的java命令,通过Bamboo在服务器B上创建了一个ssh任务。 但是,它似乎只是坐在那里而从未真正完成

  • 遇到以下错误。无法确定确切的根本原因。 WebDriverException:无法连接到端口7055上的二进制FirefoxBinary(/usr/lib64/Firefox/Firefox);进程输出如下:错误:无法打开Display::99错误:无法打开Display::99 构建信息:版本:“2.45.0”,修订版:“32A636C”,时间:“2015-03-05 22:01:35”系统信息

  • 我试图通过testng运行简单的Cucumber/Java测试。xml。 所以,我有testng。xml: 我用的是runner。类,在其中我将路径/选项/etc设置为功能文件、步骤和报告: 但是当我运行testng时。xml作为TestNG套件,它: 1) 通过我自己的设想, 但是 我做错了什么?

  • 类似于这个问题。然而,当使用ddt时,公认的解决方案不适用于我。 例如: 如果我在终端上运行这个,它就不起作用了

  • 在下面的场景中,我试图从主菜单中选择Option4-1。已经使用了moveToElement(),click()。当我执行脚本时,用例显示为pass,但我没有看到窗口中出现的幻灯片,这是点击“Option4-1”后的预期行为。请提供您的意见。 代码:

  • 需要一些帮助来获得并行运行testng测试用例的正确方法。