`Driver.quit()`在我的测试运行后没有关闭任何标签页。

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

Driver.quit() is not closing any of my tabs after my test is run

问题

I am running a test in Selenium with junit4 which opens multiple windows during the test. I then want to close the driver (all windows) after the test but for some reason this isn't working. I am new to java/junit and Selenium so apologies if the resolution is obvious

//I start the Webdriver here outside of my test
public WebDriver driver = TestContext.StartDriver(TestContext.getDashboardsUrlSaml());

//My test iterates around 7 times through parameters set (params not included in question)
@Test
public void login() throws InterruptedException, ParseException {
String currentHandle = driver.getWindowHandle();
Thread.sleep(2000);
java.util.Date date = (java.util.Date) new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/2030 00:00:00");
//expirationTime
Cookie cookie = new Cookie(cookieName, content, "rfqasv0003d.chicago.cme.com", "/sso/accountstatus", date, true);
driver.manage().addCookie(cookie);
Thread.sleep(1000);
System.out.println("Cookie has been added for: " + role);

  1. driver.findElement(By.id("user")).sendKeys(username);
  2. driver.findElement(By.id("pwd")).sendKeys(password);
  3. driver.findElement(By.id("loginBtn")).click();
  4. System.out.println("Logged in successfully for: " + role);
  5. Thread.sleep(1000);
  6. System.out.println("The dashboards required for " + role + " are: " + dashboards);
  7. Thread.sleep(1000);
  8. String[] reqDashboards = dashboards.split("\\|");
  9. List<String> requiredDash = Arrays.asList(reqDashboards);
  10. Thread.sleep(2000);
  11. Select select = new Select(driver.findElement(By.xpath("//div[@class='document-selector']/select")));
  12. List<WebElement> options = select.getOptions();
  13. ArrayList<String> displayedDash = new ArrayList<String>();
  14. for (WebElement visibleDash : options) {
  15. String visDash = visibleDash.getText();
  16. displayedDash.add(visDash);
  17. }
  18. System.out.println("The dashboards displayed are: " + displayedDash);
  19. System.out.println("Count of dashboards visible is: " + displayedDash.size());
  20. assertEquals(requiredDash.size(), displayedDash.size());
  21. assertTrue(requiredDash.containsAll(displayedDash));
  22. assertTrue(displayedDash.containsAll(requiredDash));
  23. //logout
  24. driver.findElement(By.className("user-name")).click();
  25. driver.findElement(By.id("ui-id-5")).click();
  26. driver.switchTo().window(currentHandle);

}

//This is where I'm trying to close my driver
//My test is running with no errors; however, my driver stays open after the test has been run
public void finish() {
driver.quit();
}

英文:

I am running a test in Selenium with junit4 which opens multiple windows during the test. I then want to close the driver (all windows) after the test but for some reason this isn't working. I am new to java/junit and Selenium so apologies if the resolution is obvious

  1. //I start the Webdriver here outside of my test
  2. public WebDriver driver = TestContext.StartDriver(TestContext.getDashboardsUrlSaml());
  3. //My test iterates around 7 times through parameters set (params not included in question)
  4. @Test
  5. public void login () throws InterruptedException, ParseException {
  6. String currentHandle= driver.getWindowHandle();
  7. Thread.sleep(2000);
  8. java.util.Date date = (java.util.Date) new SimpleDateFormat(&quot;MM/dd/yyyy HH:mm:ss&quot;).parse(&quot;01/01/2030 00:00:00&quot;);
  9. //expirationTime
  10. Cookie cookie = new Cookie(cookieName,content,&quot;rfqasv0003d.chicago.cme.com&quot;,&quot;/sso/accountstatus&quot;,date,true);
  11. driver.manage().addCookie(cookie);
  12. Thread.sleep(1000);
  13. System.out.println(&quot;Cookie has been added for: &quot; + role);
  14. driver.findElement(By.id(&quot;user&quot;)).sendKeys(username);
  15. driver.findElement(By.id(&quot;pwd&quot;)).sendKeys(password);
  16. driver.findElement(By.id(&quot;loginBtn&quot;)).click();
  17. System.out.println(&quot;Logged in successfully for: &quot; + role);
  18. Thread.sleep(1000);
  19. System.out.println(&quot;The dashboards required for &quot; + role +&quot; are: &quot; + dashboards);
  20. Thread.sleep(1000);
  21. String[] reqDashboards = dashboards.split(&quot;\\|&quot;);
  22. List&lt;String&gt; requiredDash = Arrays.asList(reqDashboards);
  23. Thread.sleep(2000);
  24. Select select = new Select(driver.findElement(By.xpath(&quot;//div[@class=&#39;document-selector&#39;]/select&quot;)));
  25. List&lt;WebElement&gt; options = select.getOptions();
  26. ArrayList&lt;String&gt; displayedDash = new ArrayList&lt;String&gt;();
  27. for (WebElement visibleDash: options) {
  28. String visDash = visibleDash.getText();
  29. displayedDash.add(visDash);
  30. }
  31. System.out.println(&quot;The dashboards displayed are: &quot; + displayedDash);
  32. System.out.println(&quot;Count of dashboards visible is: &quot; + displayedDash.size());
  33. assertEquals(requiredDash.size(), displayedDash.size());
  34. assertTrue(requiredDash.containsAll(displayedDash));
  35. assertTrue(displayedDash.containsAll(requiredDash));
  36. //logout
  37. driver.findElement(By.className(&quot;user-name&quot;)).click();
  38. driver.findElement(By.id(&quot;ui-id-5&quot;)).click();
  39. driver.switchTo().window(currentHandle);
  40. }
  41. //This is where I&#39;m trying to close my driver
  42. //My test is running with no errors however my driver stays open after the test has been run
  43. public void finish () {
  44. driver.quit();
  45. }
  46. }

答案1

得分: 2

根据我所见,你没有运行那个方法
尝试使用注解@AfterMethod来退出方法
像这样:

  1. @AfterMethod
  2. public void finish () {
  3. driver.close();
  4. driver.quit();
  5. }
英文:

As far as I can see, you are not running that method
Try to use annotation @AfterMethod for the quit method
Like this:

  1. @AfterMethod
  2. public void finish () {
  3. driver.close();
  4. driver.quit();
  5. }

huangapple
  • 本文由 发表于 2023年5月10日 17:08:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216688.html
匿名

发表评论

匿名网友

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

确定