为什么我收到“会话 ID 为空。调用 quit() 后使用 WebDriver?” SeleniumWebdriver 中的异常?

问题描述 投票:0回答:1

我收到“org.openqa.selenium.NoSuchSessionException:会话 ID 为空。调用 quit() 后使用 WebDriver?”我运行测试用例时出现异常。

我的测试课是这样的;

import org.testng.annotations.Test;

public class LoginTests extends TestBase{

    LoginAndSignUpPage loginAndSignUpPage=new LoginAndSignUpPage();

    @Test (priority = 1)
    public void loginWithValidCredentials(){

        loginAndSignUpPage.navigateToModule("Log in");
        loginAndSignUpPage.login_userName.sendKeys(ConfigurationReader.get("username"));
        loginAndSignUpPage.login_password.sendKeys(ConfigurationReader.get("password"));
        loginAndSignUpPage.login_button.click();
        BrowserUtils.waitFor(3);

        String expectedUser= "Welcome " + ConfigurationReader.get("username");
        Assert.assertEquals(expectedUser, loginAndSignUpPage.nameOfUser.getText());
       
    }

  
    @Test (priority = 2)
    public void loginWithInvalidPassword(){

       loginAndSignUpPage.navigateToModule("Log in");
       loginAndSignUpPage.login_userName.sendKeys(ConfigurationReader.get("username"));
       loginAndSignUpPage.login_password.sendKeys("wrongpassword)");
       loginAndSignUpPage.login_button.click();

        String actual =alert.getText();
        String expected ="Wrong password.";

        BrowserUtils.waitFor(3);
        Assert.assertEquals(expected, actual);*/

    }

    @Test (priority = 3)
    public void loginWithInvalidUsername(){

        loginAndSignUpPage.navigateToModule("Log in");
        loginAndSignUpPage.login_userName.sendKeys("wrongUsername");
        loginAndSignUpPage.login_password.sendKeys(ConfigurationReader.get("password"));

        loginAndSignUpPage.login_button.click();
        Alert alert= Driver.get().switchTo().alert();;
        String actual =alert.getText();
        String expected ="User does not exist.";
        BrowserUtils.waitFor(3);
       Assert.assertEquals(expected, actual);

    }
}

我的Driver类很快就是这样的;

public class Driver {

    private Driver(){}

    private static InheritableThreadLocal<WebDriver> driverPool = new InheritableThreadLocal<>();

    public static WebDriver get(){

        if (driverPool.get() == null){

          
            String browser = System.getProperty("browser") != null ? browser = System.getProperty("browser") : ConfigurationReader.get("browser");

            switch (browser){
                case "chrome":
                    WebDriverManager.chromedriver().setup();
                    ChromeOptions options = new ChromeOptions();
                    options.addArguments("--remote-allow-origins=*");
                    driverPool.set(new ChromeDriver(options));
                    driverPool.get().manage().window().maximize();
                    driverPool.get().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                    break;
}
....

我的TestBase是这样的;

public class TestBase {

    protected WebDriver driver;
    protected Actions actions;
    protected WebDriverWait wait;

    @BeforeMethod
    public void setUp(){
        driver= Driver.get();
        actions= new Actions(driver);
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.get(ConfigurationReader.get("url"));
    }
    @AfterMethod
    public void tearDown(ITestResult result) throws IOException, InterruptedException {
       
            Thread.sleep(2000);
            Driver.closeDriver();
        }

    }

还有我的 POM 课程;

public class LoginAndSignUpPage extends BasePage{
    public LoginAndSignUpPage() { PageFactory.initElements(Driver.get(), this); }

    @FindBy(id = "sign-username")
    public WebElement signUp_userName;
...

和基本页面

public abstract class BasePage {


    public BasePage() { PageFactory.initElements(Driver.get(), this); }

...

我执行扩展测试库的测试类。第一个测试被执行,但其他测试则没有执行。我尝试使用@AfterTestSuite,@AfterClass等方法,它们在TestBase中不起作用。

java selenium-webdriver testing testng
1个回答
0
投票

看起来您正在为每个测试创建并关闭浏览器;

LoginAndSignUpPage
也需要做同样的事情。我认为你的每个测试都需要初始化
LoginAndSignUpPage

© www.soinside.com 2019 - 2024. All rights reserved.