空指针异常:在运行不同功能文件和步骤定义文件中的测试时发生。

huangapple go评论101阅读模式
英文:

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

问题

  1. 我在一个特性文件和一个步骤定义文件中拥有所有测试时没有遇到这个问题然而我决定开始拆分测试创建了2个特性文件和2个步骤定义文件然而我遇到了NullPointerException错误
  2. 我正在使用一个全局变量类来初始化浏览器并设置驱动程序如下所示
  3. public class globalVariables {
  4. public WebDriver driver;
  5. public Properties prop;
  6. public WebDriver initializeDriver() throws IOException
  7. {
  8. prop= new Properties();
  9. String browserName= "chrome";
  10. String pathToDriver = "";
  11. if(browserName.equals("chrome"))
  12. {
  13. pathToDriver = "C://Repositories//webDrivers//chromedriver_win32_85.83//chromedriver.exe";
  14. System.setProperty("webdriver.chrome.driver", pathToDriver);
  15. driver= new ChromeDriver();
  16. driver.manage().window().maximize();
  17. //在chrome驱动中执行
  18. }
  19. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  20. return driver;
  21. }
  22. }
  23. 登录特性文件因为要打开浏览器等所以首先运行
  24. @SmokeTest
  25. @Login
  26. Feature: Queries SmokeTest
  27. Scenario Outline: 使用Chrome登录
  28. Given 我打开Chrome
  29. And 我浏览Queries
  30. When 我使用"<username>""<password>"登录Queries
  31. And 验证用户成功登录
  32. Then 关闭浏览器
  33. Examples:
  34. | username | password |
  35. | user1| pass1 |
  36. | user2| pass2 |
  37. 登录特性文件的步骤定义
  38. public class login_steps extends globalVariables {
  39. @Given("我打开Chrome")
  40. public void iOpenChrome() throws Throwable {
  41. driver = initializeDriver();
  42. // throw new PendingException();
  43. }
  44. @When("我浏览Queries")
  45. public void iBrowseToQueries() {
  46. driver.get("https://qry.com/");
  47. // throw new PendingException();
  48. }
  49. @When("我使用{string}和{string}登录Queries")
  50. public void i_login_to_queries_using_and(String string, String string2) {
  51. driver.findElement(By.id("UserName")).sendKeys(string);
  52. driver.findElement(By.id("Password")).sendKeys(string2);
  53. driver.findElement(By.id("btnLogin")).click();
  54. }
  55. @Then("验证用户成功登录")
  56. public void verifyThatUserIsSuccessfullyLoggedIn() {
  57. WebElement HomeButton = driver.findElement(By.id("divHomeLink"));
  58. Assert.assertEquals(true, HomeButton.isDisplayed());
  59. }
  60. @And("关闭浏览器")
  61. public void closeBrowser() {
  62. driver.quit();
  63. }
  64. }
  65. 搜索特性文件
  66. @SmokeTest
  67. @Search
  68. Feature: Queries SmokeTest
  69. Scenario: 搜索功能
  70. Given 我打开Chrome
  71. And 我浏览Queries
  72. Then 我使用"user1""pass1"登录Queries
  73. And 点击Queries搜索按钮
  74. And 输入关键词
  75. And 点击搜索
  76. Then 关闭浏览器
  77. 搜索步骤定义文件
  78. public class search_steps extends globalVariables {
  79. @And("点击Queries搜索按钮")
  80. public void clickOnQueriesSearchButton() {
  81. driver.findElement(By.id("imgSearchQueries")).click(); //错误发生在这里
  82. }
  83. @And("输入关键词")
  84. public void enterKeyword() {
  85. driver.findElement(By.id("txtKeywords")).sendKeys("Smoke Query");
  86. }
  87. @And("点击搜索")
  88. public void clickSearch() {
  89. driver.findElement(By.className("btn-search")).click();
  90. }
  91. }
英文:

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.

  1. public class globalVariables {
  2. public WebDriver driver;
  3. public Properties prop;
  4. public WebDriver initializeDriver() throws IOException
  5. {
  6. prop= new Properties();
  7. String browserName= &quot;chrome&quot;;
  8. String pathToDriver = &quot;&quot;;
  9. if(browserName.equals(&quot;chrome&quot;))
  10. {
  11. pathToDriver = &quot;C://Repositories//webDrivers//chromedriver_win32_85.83//chromedriver.exe&quot;;
  12. System.setProperty(&quot;webdriver.chrome.driver&quot;, pathToDriver);
  13. driver= new ChromeDriver();
  14. driver.manage().window().maximize();
  15. //execute in chrome driver
  16. }
  17. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  18. return driver;
  19. }
  20. }

Login Feature File (which runs first, because of opening of browser etc..)

  1. @SmokeTest
  2. @Login
  3. Feature: Queries SmokeTest
  4. Scenario Outline: Login using Chrome
  5. Given I open Chrome
  6. And I browse to Queries
  7. When I login to Queries using &quot;&lt;username&gt;&quot; and &quot;&lt;password&gt;&quot;
  8. And Verify that user is successfully logged in
  9. Then Close Browser
  10. Examples:
  11. | username | password |
  12. | user1| pass1 |
  13. | user2| pass2 |

Step definition for the Login Feature file

  1. public class login_steps extends globalVariables {
  2. @Given(&quot;I open Chrome&quot;)
  3. public void iOpenChrome() throws Throwable {
  4. driver = initializeDriver();
  5. // throw new PendingException();
  6. }
  7. @When(&quot;I browse to Queries&quot;)
  8. public void iBrowseToQueries() {
  9. driver.get(&quot;https://qry.com/&quot;);
  10. // throw new PendingException();
  11. }
  12. @When(&quot;I login to Queries using {string} and {string}&quot;)
  13. public void i_login_to_queries_using_and(String string, String string2) {
  14. driver.findElement(By.id(&quot;UserName&quot;)).sendKeys(string);
  15. driver.findElement(By.id(&quot;Password&quot;)).sendKeys(string2);
  16. driver.findElement(By.id(&quot;btnLogin&quot;)).click();
  17. }
  18. @Then(&quot;Verify that user is successfully logged in&quot;)
  19. public void verifyThatUserIsSuccessfullyLoggedIn() {
  20. WebElement HomeButton = driver.findElement(By.id(&quot;divHomeLink&quot;));
  21. Assert.assertEquals(true, HomeButton.isDisplayed());
  22. }
  23. @And(&quot;Close Browser&quot;)
  24. public void closeBrowser() {
  25. driver.quit();
  26. }

}

Search Feature file

  1. @SmokeTest
  2. @Search
  3. Feature: Queries SmokeTest
  4. Scenario: Search Function
  5. Given I open Chrome
  6. And I browse to Queries
  7. Then I login to Queries using &quot;user1&quot; and &quot;pass1&quot;
  8. And Click on Queries Search button
  9. And Enter keyword
  10. And Click Search
  11. Then Close Browser

Search step definition file

  1. public class search_steps extends globalVariables {
  2. @And(&quot;Click on Queries Search button&quot;)
  3. public void clickOnQueriesSearchButton() {
  4. driver.findElement(By.id(&quot;imgSearchQueries&quot;)).click(); //ERROR IS HERE
  5. }
  6. @And(&quot;Enter keyword&quot;)
  7. public void enterKeyword() {
  8. driver.findElement(By.id(&quot;txtKeywords&quot;)).sendKeys(&quot;Smoke Query&quot;);
  9. }
  10. @And(&quot;Click Search&quot;)
  11. public void clickSearch() {
  12. driver.findElement(By.className(&quot;btn-search&quot;)).click();
  13. }
  14. }

答案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)

huangapple
  • 本文由 发表于 2020年10月12日 18:17:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64315862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定