运行 在不同的特征文件和步骤定义文件中测试时出现 NullPointerException

NullPointerException when running tests in different feature files and step definitions files

当我在 1 个功能文件和 1 个步骤定义文件中进行所有测试时,我没有遇到这个问题。但是我决定开始拆分测试并创建 2 个功能文件和 2 个步骤定义文件。但是,我收到 NullPointerException 错误。

我正在使用全局变量 class 来初始化浏览器并设置驱动程序,如下所示。

public class globalVariables {
public WebDriver driver;
public Properties prop;

public WebDriver initializeDriver() throws IOException
{

    prop= new Properties();

    String browserName= "chrome";
    String pathToDriver = "";

    if(browserName.equals("chrome"))
    {
        pathToDriver = "C://Repositories//webDrivers//chromedriver_win32_85.83//chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", pathToDriver);
        driver= new ChromeDriver();
        driver.manage().window().maximize();
        //execute in chrome driver
    }
    
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    return driver;
}
}

登录功能文件(首先运行,因为打开浏览器等。)

 @SmokeTest
 @Login
 Feature: Queries SmokeTest
 Scenario Outline: Login using Chrome
Given I open Chrome
And I browse to Queries
When I login to Queries using "<username>" and "<password>"
And Verify that user is successfully logged in
Then Close Browser

Examples:
| username |  password |
| user1|  pass1 |
| user2|  pass2 |

登录功能文件的步骤定义

public class login_steps extends globalVariables {
@Given("I open Chrome")
public void iOpenChrome() throws Throwable {
    driver = initializeDriver();
   // throw new PendingException();
}

@When("I browse to Queries")
public void iBrowseToQueries() {
    driver.get("https://qry.com/");
   // throw new PendingException();
}

@When("I login to Queries using {string} and {string}")
public void i_login_to_queries_using_and(String string, String string2) {
    driver.findElement(By.id("UserName")).sendKeys(string);
    driver.findElement(By.id("Password")).sendKeys(string2);
    driver.findElement(By.id("btnLogin")).click();
}

@Then("Verify that user is successfully logged in")
public void verifyThatUserIsSuccessfullyLoggedIn() {
    WebElement HomeButton = driver.findElement(By.id("divHomeLink"));
    Assert.assertEquals(true, HomeButton.isDisplayed());
}

@And("Close Browser")
public void closeBrowser() {
    driver.quit();
}

}

搜索功能文件

 @SmokeTest
 @Search
Feature: Queries SmokeTest
Scenario: Search Function
Given I open Chrome
And I browse to Queries
Then I login to Queries using "user1" and "pass1"
And Click on Queries Search button
And Enter keyword
And Click Search
Then Close Browser

搜索步骤定义文件

public class search_steps extends globalVariables {

@And("Click on Queries Search button")
public void clickOnQueriesSearchButton() {
    driver.findElement(By.id("imgSearchQueries")).click(); //ERROR IS HERE
}

@And("Enter keyword")
public void enterKeyword() {
    driver.findElement(By.id("txtKeywords")).sendKeys("Smoke Query");
}

@And("Click Search")
public void clickSearch() {
    driver.findElement(By.className("btn-search")).click();
}

}

您需要确保对所有 类 使用相同的 chrome 驱动程序实例,否则会发生空点异常

为驱动程序实例使用全局变量

私有静态 Webdriver 驱动程序;(在每个 类 之上实例化它)