java selenium中的driver.close无法关闭多个登录会话

vngu2lb8  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(319)

为了更清楚,我添加了代码片段。我正在使用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(“点击登录”);睡眠(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铬驱动程序示例可以看到。还请注意,aftermethod没有得到执行。在此处输入图像描述

4smxwvx5

4smxwvx51#

我在selenium中使用driver.quit()方法,但面临相同的问题。我认为这个问题与chromedriver的最新版本有关。

public static void CloseDriver()
        {
            if (Driver != null)
            {
                Driver.Quit();
                Driver = null;
            }
        }

我甚至尝试过driver.kill(),但结果是关闭了所有浏览器。

相关问题