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

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

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(&quot;MM/dd/yyyy HH:mm:ss&quot;).parse(&quot;01/01/2030 00:00:00&quot;);  
    //expirationTime
	Cookie cookie = new Cookie(cookieName,content,&quot;rfqasv0003d.chicago.cme.com&quot;,&quot;/sso/accountstatus&quot;,date,true);
	driver.manage().addCookie(cookie);
    Thread.sleep(1000);
    System.out.println(&quot;Cookie has been added for: &quot; + role);

    driver.findElement(By.id(&quot;user&quot;)).sendKeys(username);
    driver.findElement(By.id(&quot;pwd&quot;)).sendKeys(password);
    driver.findElement(By.id(&quot;loginBtn&quot;)).click();
    
    System.out.println(&quot;Logged in successfully for: &quot; + role);
    Thread.sleep(1000);
    
    System.out.println(&quot;The dashboards required for &quot; + role +&quot; are: &quot; + dashboards);
    Thread.sleep(1000);
   
    String[] reqDashboards = dashboards.split(&quot;\\|&quot;);     
    List&lt;String&gt; requiredDash = Arrays.asList(reqDashboards);	    
    
    Thread.sleep(2000);
    Select select = new Select(driver.findElement(By.xpath(&quot;//div[@class=&#39;document-selector&#39;]/select&quot;)));
    List&lt;WebElement&gt; options = select.getOptions();
    
    ArrayList&lt;String&gt; displayedDash = new ArrayList&lt;String&gt;(); 
    
    for (WebElement visibleDash: options) {
    	String visDash = visibleDash.getText();
    	displayedDash.add(visDash);
    }	
    	 
    System.out.println(&quot;The dashboards displayed are: &quot; + displayedDash);
    System.out.println(&quot;Count of dashboards visible is: &quot; + displayedDash.size());
    
    assertEquals(requiredDash.size(), displayedDash.size());
    assertTrue(requiredDash.containsAll(displayedDash));
    assertTrue(displayedDash.containsAll(requiredDash));
    	
    //logout
	driver.findElement(By.className(&quot;user-name&quot;)).click();
	driver.findElement(By.id(&quot;ui-id-5&quot;)).click();
	
	driver.switchTo().window(currentHandle);
	
    }	

//This is where I&#39;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();
    }

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:

确定