运行多个测试时无效的会话ID [英] Invalid session id when running multiple tests

查看:101
本文介绍了运行多个测试时无效的会话ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾提过这个问题,但似乎没人能回答.因此,我将继续对当前的代码结构再发表一个问题:

I have raised that question previously, but nobody seemed to be able to answer it. Therefore I will go ahead and post a question one more time with the current code structure:

我有一个具有@BeforeEach方法的父母,该方法可以设置所有内容:

I have a parent with a @BeforeEach method that sets up everything:

public class WebDriverSettings {

    public static WebDriver driver;

    @BeforeEach
    public void setUp() {
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
        loginToEnvironment();
    }


    @AfterEach
    public void tearDown(){
        driver.manage().deleteAllCookies();
        driver.close();
    }



private void loginToEnvironment() {
        String TARGET_URL = System.getProperty("TARGET_URL", "");
        String URL = " + "@" + TARGET_URL;
        driver.get(URL);
    }

比父母的孩子还多,其中一个测试类:

Than it's a parent's child, one of the tests classes:

public class LoginServiceTest extends WebDriverSettings {

    private LoginModal loginModal;

    @BeforeEach
    public void setUp() {
        super.setUp();
        loginModal = HomePage.homepageInstance(driver)
                .acceptCookies()
                .closeCovidMessage()
                .clickOnMyAccountTab()
                .switchToLoginTab();
    }

    @Test
    public void shouldSuccessfullyLogin() {
        assertEquals(userName, loginModal.login(loginCorrectData()).myAccountName.getText());
    }

    @Test
    public void shouldDisplayIncorrectCredentialsMessage() {
        loginModal.login(loginIncorrectData());
        assertEquals(incorrectCredentialsMessage,loginModal.wrongCredentials.getText());
    }

pom对象的示例,通过这种方式构建我所有的pom对象:

Example of pom object, in which way all my pom objects are build :

ublic class LoginModal extends BasePage {

private static LoginModal loginModal;

@FindBy(xpath = "//input[@name='email']")
public WebElement emailInputField;

@FindBy(xpath = "//input[@name='password']")
public WebElement passwordInputField;

@FindBy(css = ".button-hover-wrapper > div.button-inner")
public WebElement loginButton;

@FindBy(xpath = "//a[text()='Log In']")
public WebElement loginTab;

@FindBy(css = ".error-info")
public WebElement wrongCredentials;

private LoginModal(WebDriver driver) {
    super(driver);
}

public static LoginModal loginModalInstance(WebDriver driver) {
    return loginModal == null ? loginModal = new LoginModal(driver) : loginModal;
}

public HomePage login(Login login) {
    loginModal.clickOnLoginTab()
            .fillInEmail(login.getEmail())
            .fillInPassword(login.getPassword())
            .clickOnLoginButton();
    return HomePage.homepageInstance(driver);
}

public LoginModal clickOnLoginTab() {
    loginModal.loginTab.click();
    return loginModal;
}

public LoginModal fillInEmail(String email) {
    loginModal.emailInputField.clear();
    loginModal.emailInputField.sendKeys(email);
    return loginModal;
}

public LoginModal fillInPassword(String password) {
    loginModal.passwordInputField.clear();
    loginModal.passwordInputField.sendKeys(password);
    return loginModal;
}

public void clickOnLoginButton() {
    loginModal.loginButton.click();
}

}

据我了解,我的代码执行以下操作:

From my understanding, my code does the following:

setUp方法每次在测试之前运行,子级中的setUp方法开始运行,这将成功打开浏览器并完成测试.但是,当进行另一项测试时,我得到以下信息:

setUp method in parent runs each time before the test, than setUp method in the child starts to run, which successfully opens a browser and completes the test. However, when it goes to another test, I get the following:

    invalid session id
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'Artjoms-MacBook-Air.local', ip: 'fe80:0:0:0:10eb:9260:8538:39df%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.15.6', java.version: '15'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 86.0.4240.183, chrome: {chromedriverVersion: 86.0.4240.22 (398b0743353ff..., userDataDir: /var/folders/z6/46cwjtvx2fj...}, goog:chromeOptions: {debuggerAddress: localhost:56802}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: 9837fd96a4efca85f4bcdbf90fea8a77
*** Element info: {Using=css selector, value=.cookie-accept-all}
org.openqa.selenium.NoSuchSessionException: invalid session id

当前依赖项堆栈:

dependencies {
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: '3.141.5'
    compile group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '4.2.2'
    compileOnly 'org.projectlombok:lombok:1.18.16'
    annotationProcessor 'org.projectlombok:lombok:1.18.16'
    testCompileOnly 'org.projectlombok:lombok:1.18.16'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.16'
}

推荐答案

所以问题在于页面对象是以Singleton方式设计的:

So the issue is that the page objects are designed in Singleton way:

public static LoginModal loginModalInstance(WebDriver driver) {
    return loginModal == null ? loginModal = new LoginModal(driver) : loginModal;
}

因此,在第一次测试中使用驱动程序进行初始化后,它们在新测试中仍将引用旧的驱动程序实例(已关闭).

So that once initialized with a driver in first test they remain refering to old driver instance (which has been already closed) in new test.

解决方案是重新加工loginModalInstance和其他类似方法,以便它们使用新的驱动程序实例重新初始化现有对象的字段.

The solution would be to rework loginModalInstance and other similar methods so that they re-initialize the fields of existing objects with new driver instance.

这篇关于运行多个测试时无效的会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆