英文:
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);
driver.findElement(By.id("user")).sendKeys(username);
driver.findElement(By.id("pwd")).sendKeys(password);
driver.findElement(By.id("loginBtn")).click();
System.out.println("Logged in successfully for: " + role);
Thread.sleep(1000);
System.out.println("The dashboards required for " + role + " are: " + dashboards);
Thread.sleep(1000);
String[] reqDashboards = dashboards.split("\\|");
List<String> requiredDash = Arrays.asList(reqDashboards);
Thread.sleep(2000);
Select select = new Select(driver.findElement(By.xpath("//div[@class='document-selector']/select")));
List<WebElement> options = select.getOptions();
ArrayList<String> displayedDash = new ArrayList<String>();
for (WebElement visibleDash : options) {
String visDash = visibleDash.getText();
displayedDash.add(visDash);
}
System.out.println("The dashboards displayed are: " + displayedDash);
System.out.println("Count of dashboards visible is: " + displayedDash.size());
assertEquals(requiredDash.size(), displayedDash.size());
assertTrue(requiredDash.containsAll(displayedDash));
assertTrue(displayedDash.containsAll(requiredDash));
//logout
driver.findElement(By.className("user-name")).click();
driver.findElement(By.id("ui-id-5")).click();
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
//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);
driver.findElement(By.id("user")).sendKeys(username);
driver.findElement(By.id("pwd")).sendKeys(password);
driver.findElement(By.id("loginBtn")).click();
System.out.println("Logged in successfully for: " + role);
Thread.sleep(1000);
System.out.println("The dashboards required for " + role +" are: " + dashboards);
Thread.sleep(1000);
String[] reqDashboards = dashboards.split("\\|");
List<String> requiredDash = Arrays.asList(reqDashboards);
Thread.sleep(2000);
Select select = new Select(driver.findElement(By.xpath("//div[@class='document-selector']/select")));
List<WebElement> options = select.getOptions();
ArrayList<String> displayedDash = new ArrayList<String>();
for (WebElement visibleDash: options) {
String visDash = visibleDash.getText();
displayedDash.add(visDash);
}
System.out.println("The dashboards displayed are: " + displayedDash);
System.out.println("Count of dashboards visible is: " + displayedDash.size());
assertEquals(requiredDash.size(), displayedDash.size());
assertTrue(requiredDash.containsAll(displayedDash));
assertTrue(displayedDash.containsAll(requiredDash));
//logout
driver.findElement(By.className("user-name")).click();
driver.findElement(By.id("ui-id-5")).click();
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();
}
}
答案1
得分: 2
根据我所见,你没有运行那个方法
尝试使用注解@AfterMethod
来退出方法
像这样:
@AfterMethod
public void finish () {
driver.close();
driver.quit();
}
英文:
As far as I can see, you are not running that method
Try to use annotation @AfterMethod
for the quit method
Like this:
@AfterMethod
public void finish () {
driver.close();
driver.quit();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论