java - java-selenium 中的 driver.close 未关闭多个登录会话
问题描述
为了更清楚起见,我添加了代码片段。我正在使用 java-selenium-testNG 并尝试使用 3 个帐户登录网站 zoho.com 并验证是否成功。我已经查看了一些类似的问题,但没有找到适合我的解决方案。
我在@BeforeMethod 中实例化了 ChromeDriver。然后我用第一个帐户登录网站并在@AfterMethod 中关闭浏览器。我必须使用 Driver = new ChromeDriver 重新实例化浏览器,以便在实例关闭时使用新帐户重新登录,或者至少感觉它给出了会话 ID 不存在的错误。
我的问题只是驱动程序的最后一个实例正在关闭,因为我在 @AfterTest 方法中有 driver.quit 。其他两个实例保留在内存中。
我也尝试在不关闭浏览器的情况下使用相同的实例,但在这种情况下,该特定网站上的登录选项不可用,并且我的后续测试失败。
这是下面的代码以进一步说明
package uk.dev.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.util.concurrent.TimeUnit;
public class zohoLogin {
WebDriver driver=null;
String winHandleBefore;
@DataProvider(name ="testData")
public Object[][] loginPasswordData()
{
return new Object[][]{
{"xxx@gmail.com",new String ("somepassword")} ,
{"yyy@gmail.com",new String ("somepassword")},
{"zzz@gmail.com",new String ("somepassword")},
};
}
@Test ( dataProvider = "testData")
public void enterUserDetails(String userEmail, String userPassword) throws InterruptedException {
driver.findElement(By.xpath("//input[@id='login_id']")).sendKeys(userEmail);
System.out.println(("Running for Test data " + userEmail));
Thread.sleep(2000);
driver.findElement(By.xpath("//button[@id='nextbtn']")).click();
Thread.sleep(2000);
System.out.println("clicked on Next");
driver.findElement(By.xpath("//input[@id='password']")).sendKeys(userPassword); System.out.println("输入密码"); driver.findElement(By.xpath("//button[@id='nextbtn']//span[contains(text(),'Sign in')]")).click(); System.out.println("点击登录"); 线程.sleep(2000);
}
@BeforeMethod
public void loginZoho() throws InterruptedException
{
driver = new ChromeDriver();
driver.get("https://www.zoho.com");
System.out.println("Open the browser");
driver.findElement(By.xpath("//a[@class='zh-login']")).click();
//driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
}
@AfterMethod
public void closeBrosers() throws InterruptedException {
Thread.sleep(2000);
driver.close();
System.out.println("Close the browser");
}
@BeforeTest
public void setupBrowser()
{ WebDriverManager.chromedriver().setup();
}
@AfterTest
public void tearDown()
{
driver.quit();
}
}
附上可以看到 2 个 chrome 驱动程序实例的运行结果。另请注意 AfterMethod 未执行。 在此处输入图像描述
解决方案
我在 selenium 中使用 driver.quit() 方法,但面临同样的问题。我认为问题与最新版本的 chromedriver 有关。
public static void CloseDriver()
{
if (Driver != null)
{
Driver.Quit();
Driver = null;
}
}
我什至尝试过 Driver.Kill() 但它导致关闭所有浏览器。
推荐阅读
- php - 在代码中更新用户角色时触发 WordPress 挂钩
- .htaccess - 如何将 URL 缩短为 SEO 友好的 URL
- node.js - 使用 Where 和 Include 失败的 Sequelize 查询
- javascript - 如何在设定的时间后更改 MongoDB 文档中的字段?
- html - 如何使 HTML 表格中的一列缩小以适应?
- python - 在锚标记中抓取嵌套标题 - python 中的网络抓取
- enterprise-architect - 是否可以在 MDG 技术中添加观点?
- python - Python:使用 Beautifulsoup 进行 Web 抓取 - 为什么函数 get() 不一致?
- amazon-sns - EventBridge with SNS Target,可以自定义SNS Message吗
- python-3.x - Python-pptx:如果两个图表出现在同一张幻灯片上,如何更新第二个图表的图表数据