【问题标题】:TestNG.java.lang.NullPointerException when running second @Test运行第二个 @Test 时的 TestNG.java.lang.NullPointerException
【发布时间】:2016-05-24 08:13:32
【问题描述】:

它运行第一个测试正常,但是在运行“编辑配置文件”测试时出现空指针异常,我不知道为什么。我全局声明了公共驱动程序。

public class TestLogin
{

    public WebDriver driver;// = new FirefoxDriver();

    public String baseURL = "mytestsite.com";

    @BeforeTest
    public void setBaseURL()
    {
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait( 20L, TimeUnit.SECONDS );
        driver.get( baseURL );
    }

    // Login test
    @Test
      public void testLogin() throws InterruptedException{

      driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);

      analyticsLoginPage mylogin = PageFactory.initElements(driver,     `     `       analyticsLoginPage.class);
    analyticsLandingPage landingpage = mylogin.login("username", "password");
    Thread.sleep(3000);
   }

    // Edit Profile test

    @Test // (dependsOnMethods = { "testLogin" })
    public void verifyProfile()
        throws InterruptedException
    {
        // driver = new FirefoxDriver();

        Thread.sleep( 3000 );
        analyticsLandingPage landingpage = new analyticsLandingPage( driver );
        Thread.sleep( 3000 );
        landingpage.gotoProfile();

        // Thread.sleep(5000);
        analyticsEditProfilePage editprofile = PageFactory.initElements( driver, analyticsEditProfilePage.class );
        editprofile.verifyEditFirstName();
        editprofile.verifyEditLastName();
        editprofile.verifyCompanyName();
        editprofile.verifyReportingProfile();
        editprofile.verifyUsageStatistics();

}

着陆页类

package com.tapanalytics.pom.domain;

import java.util.concurrent.TimeUnit;

import javax.security.auth.login.Configuration;

import org.junit.Test;

public class analyticsLandingPage
{

    WebDriver driver;

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

    @FindBy( xpath = Configuration.manage_dashboard )
    public WebElement manage_dashboard;

    @FindBy( xpath = Configuration.manage_services )
    public WebElement manage_services;

    @FindBy( xpath = Configuration.profile )
    public WebElement profile;

    @FindBy( xpath = Configuration.support )
    public WebElement support;

    @FindBy( xpath = Configuration.logout )
    public WebElement logout;

    public void gotoMangeDashboards()
    {
        manage_dashboard.click();
    }

    public void gotoServices()
    {
        manage_services.click();
    }

    public void gotoProfile()
    {
        profile.click();
    }

    public void gotoSupport()
    {
        support.click();
    }

    public void Logout()
    {
        logout.click();
    }

}

【问题讨论】:

  • npe 在哪里?

标签: selenium webdriver testng


【解决方案1】:

有多种方法可以解决此问题。我认为您可以使用 @BeforeMethod 方法在每个测试方法之前初始化驱动程序,然后使用 @AfterMethod 调用 driver.quit() 并将驱动程序设置为 null 在每个测试方法之后。非静态驱动程序可以保持与类成员相同。

就我个人而言,对于 Selenium 测试,我从不为多种测试方法重复使用同一个驱动程序。这样做是自找麻烦,通常需要强制执行测试方法顺序。

更高级:在数据提供者中创建您的 WebDriver 实例,然后将其传递给每个测试方法。然后,有一个 @AfterMethod 在每次测试后关闭驱动程序。然后,您的测试类不需要包含自己的共享驱动程序实例,TestNG 会处理您的测试多线程。

【讨论】:

  • 感谢您的帮助,我目前正在使用:@BeforeTest 和 after test 来关闭浏览器。 @AfterTest public static void quitDriver(){ driver.quit(); }
  • 这就是你从提供者传递到每个测试的意思:analyticsLandingPage Landingpage = new analyticsLandingPage( driver );
  • 不,我的意思是,如果您查看我在这个问题中的示例,然后将 WebDriverHelper 对象替换为“Object a”,因为帮助程序有一个 createWebDriver 方法:stackoverflow.com/questions/32536537/… 这就是为什么我称它为高级示例,因为它需要进一步解释,此处不合适。
【解决方案2】:

试试

public static WebDriver driver;

分两个班。

谢谢你, 壁画

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多