英文:
NullPointerException when running tests in different feature files and step definitions files
问题
我在一个特性文件和一个步骤定义文件中拥有所有测试时,没有遇到这个问题。然而,我决定开始拆分测试,创建了2个特性文件和2个步骤定义文件。然而,我遇到了NullPointerException错误。
我正在使用一个全局变量类来初始化浏览器并设置驱动程序,如下所示。
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();
//在chrome驱动中执行
}
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}
登录特性文件(因为要打开浏览器等,所以首先运行)
@SmokeTest
@Login
Feature: Queries SmokeTest
Scenario Outline: 使用Chrome登录
Given 我打开Chrome
And 我浏览Queries
When 我使用"<username>"和"<password>"登录Queries
And 验证用户成功登录
Then 关闭浏览器
Examples:
| username | password |
| user1| pass1 |
| user2| pass2 |
登录特性文件的步骤定义
public class login_steps extends globalVariables {
@Given("我打开Chrome")
public void iOpenChrome() throws Throwable {
driver = initializeDriver();
// throw new PendingException();
}
@When("我浏览Queries")
public void iBrowseToQueries() {
driver.get("https://qry.com/");
// throw new PendingException();
}
@When("我使用{string}和{string}登录Queries")
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("验证用户成功登录")
public void verifyThatUserIsSuccessfullyLoggedIn() {
WebElement HomeButton = driver.findElement(By.id("divHomeLink"));
Assert.assertEquals(true, HomeButton.isDisplayed());
}
@And("关闭浏览器")
public void closeBrowser() {
driver.quit();
}
}
搜索特性文件
@SmokeTest
@Search
Feature: Queries SmokeTest
Scenario: 搜索功能
Given 我打开Chrome
And 我浏览Queries
Then 我使用"user1"和"pass1"登录Queries
And 点击Queries搜索按钮
And 输入关键词
And 点击搜索
Then 关闭浏览器
搜索步骤定义文件
public class search_steps extends globalVariables {
@And("点击Queries搜索按钮")
public void clickOnQueriesSearchButton() {
driver.findElement(By.id("imgSearchQueries")).click(); //错误发生在这里
}
@And("输入关键词")
public void enterKeyword() {
driver.findElement(By.id("txtKeywords")).sendKeys("Smoke Query");
}
@And("点击搜索")
public void clickSearch() {
driver.findElement(By.className("btn-search")).click();
}
}
英文:
I did not have this issue when I had all the tests in 1 feature file and 1 step definition file. However I decided to start splitting the tests and created 2 feature files and 2 step definitions files. However, I am getting NullPointerException error.
I am using a global variables class to initialize browser and set driver, as per the below.
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;
}
}
Login Feature File (which runs first, because of opening of browser etc..)
@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 |
Step definition for the Login Feature file
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();
}
}
Search Feature file
@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
Search step definition file
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();
}
}
答案1
得分: 0
请确保在所有类中都使用相同的 Chrome Driver 实例,否则将会出现空指针异常。
请使用全局变量来存储驱动实例。
private static Webdriver driver;(在每个类的顶部实例化此变量)
英文:
You need to make sure you are using same chrome driver instance for all classes otherwise Null point exception will occur
Use global variable for driver instance
private static Webdriver driver;( Instatiate this on top of every classes)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论