使用 JUnit 5 实现自动测试的并行执行GEB(无斑点)

发布于 2025-01-16 10:51:12 字数 3710 浏览 6 评论 0原文

我正在尝试使用 JUnit 5 和 GEB 实现自动测试的并行执行。目前,测试已经并行运行。问题是每个页面元素在创建页面对象时都必须可见。如果该对象未显示在页面上,那么当您尝试访问它时,将使用新页面创建一个新的浏览器对象,从而启动一个额外的线程。如何避免这种情况?

package tests

import geb.Browserimport geb.Pageimport geb.junit5.GebReportingTest

import org.junit.jupiter.api.AfterEachimport org.junit.jupiter.api.BeforeEachimport org.junit.jupiter.api.Testimport org.junit.jupiter.api.extension.ExtendWithimport io.github.bonigarcia.seljup.SeleniumJupiterimport org.openqa.selenium.chrome.ChromeDriver;import pages.CbsLoginPageimport static org.assertj.core.api.Assertions.*

@ExtendWith(SeleniumJupiter.class)class LoginToCbsTest extends GebReportingTest {public Browser browserpublic CbsLoginPage page

@BeforeEach
public void classLevelSetup() {
    browser = new Browser()
    browser.setDriver(new ChromeDriver())
    page = browser.createPage(CbsLoginPage.class)
}

@AfterEach
public void teardown() {
    browser.quit()
}

@Test
void loginFailsWhenPasswordIsWrong() {
    // When
    page.fillCredentialsForm("username", "123_Wrong_password")
    page.clickLoginButton()

    // Then
    verifyLoginErrorIsDisplayed()
}

@Test
void loginFailsWhenUsernameIsWrong() {
    // When
    page.fillCredentialsForm("Wrong_username", "password")
    page.clickLoginButton()

    // Then
    verifyLoginErrorIsDisplayed()
}

package pages

import geb.Pageimport modules.CbsLoginPageModule

import static geb.Browser.drive

class CbsLoginPage extends Page {static at = { title == "Log in to Application" }

static content = {
    loginForm { module(CbsLoginPageModule) }
}

void fillCredentialsForm(String username, String password) {
    drive(getBrowser(), {
        getBrowser().to(this)
        loginForm.loginField.value(username)
        loginForm.passwordField.value(password)
    })
}

void clickLoginButton() {
    drive(getBrowser(), {
        getBrowser().at(this)
        loginForm.loginButton.click()
    })
}

void getErrorMessage() {
    drive(getBrowser(), {
        getBrowser().at(this)
        page
        waitFor { $("div", innerHTML: contains("Invalid username or password.")) //This element is not visible when page was created}
    })
}

}


package modules

import geb.Module

class CbsLoginPageModule extends Module {
    static content = {form { $("form") }
        loginField { form.$(id: "name") }
        passwordField { form.$(id: "password") }
        loginButton { form.$(name: "login") }
}

}


/*This is the Geb configuration file.

See: http://www.gebish.org/manual/current/#configuration

*/

import org.openqa.selenium.chrome.ChromeDriver

waiting {timeout = 2}

environments {

driver = { new ChromeDriver() }

}reportsDir = new File("target/runtime_reports_dir")baseUrl = "url"

plugins {id "idea"id "groovy"}

repositories {mavenCentral()}

dependencies {testImplementation 'io.github.bonigarcia:selenium-jupiter:4.0.1'testImplementation 'org.seleniumhq.selenium:selenium-java:4.1.2'testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'testImplementation 'org.gebish:geb-junit5:5.1'testImplementation 'org.assertj:assertj-core:3.22.0'}

task chromedriverTest(type: Test) {useJUnitPlatform()}

task chromeheadlessTest(type: Test) {useJUnitPlatform()}

test {useJUnitPlatform()testLogging {events "passed", "skipped", "failed"}

systemProperty("junit.jupiter.execution.parallel.enabled" , "true")
systemProperty("junit.jupiter.execution.parallel.config.strategy", "fixed")
systemProperty("junit.jupiter.execution.parallel.mode.default", "concurrent")
systemProperty("junit.jupiter.execution.parallel.config.fixed.parallelism", 2)

}

I'm trying to implement parallel execution of autotests using JUnit 5 and GEB. At the moment, the tests are already running in parallel. The problem is that every page element must be visible at the time the page object is created. If the object was not displayed on the page, then when you try to access it, a new browser object is created with a new page, starting an extra thread. How can this be avoided?

package tests

import geb.Browserimport geb.Pageimport geb.junit5.GebReportingTest

import org.junit.jupiter.api.AfterEachimport org.junit.jupiter.api.BeforeEachimport org.junit.jupiter.api.Testimport org.junit.jupiter.api.extension.ExtendWithimport io.github.bonigarcia.seljup.SeleniumJupiterimport org.openqa.selenium.chrome.ChromeDriver;import pages.CbsLoginPageimport static org.assertj.core.api.Assertions.*

@ExtendWith(SeleniumJupiter.class)class LoginToCbsTest extends GebReportingTest {public Browser browserpublic CbsLoginPage page

@BeforeEach
public void classLevelSetup() {
    browser = new Browser()
    browser.setDriver(new ChromeDriver())
    page = browser.createPage(CbsLoginPage.class)
}

@AfterEach
public void teardown() {
    browser.quit()
}

@Test
void loginFailsWhenPasswordIsWrong() {
    // When
    page.fillCredentialsForm("username", "123_Wrong_password")
    page.clickLoginButton()

    // Then
    verifyLoginErrorIsDisplayed()
}

@Test
void loginFailsWhenUsernameIsWrong() {
    // When
    page.fillCredentialsForm("Wrong_username", "password")
    page.clickLoginButton()

    // Then
    verifyLoginErrorIsDisplayed()
}

package pages

import geb.Pageimport modules.CbsLoginPageModule

import static geb.Browser.drive

class CbsLoginPage extends Page {static at = { title == "Log in to Application" }

static content = {
    loginForm { module(CbsLoginPageModule) }
}

void fillCredentialsForm(String username, String password) {
    drive(getBrowser(), {
        getBrowser().to(this)
        loginForm.loginField.value(username)
        loginForm.passwordField.value(password)
    })
}

void clickLoginButton() {
    drive(getBrowser(), {
        getBrowser().at(this)
        loginForm.loginButton.click()
    })
}

void getErrorMessage() {
    drive(getBrowser(), {
        getBrowser().at(this)
        page
        waitFor { $("div", innerHTML: contains("Invalid username or password.")) //This element is not visible when page was created}
    })
}

}


package modules

import geb.Module

class CbsLoginPageModule extends Module {
    static content = {form { $("form") }
        loginField { form.$(id: "name") }
        passwordField { form.$(id: "password") }
        loginButton { form.$(name: "login") }
}

}


/*This is the Geb configuration file.

See: http://www.gebish.org/manual/current/#configuration

*/

import org.openqa.selenium.chrome.ChromeDriver

waiting {timeout = 2}

environments {

driver = { new ChromeDriver() }

}reportsDir = new File("target/runtime_reports_dir")baseUrl = "url"

plugins {id "idea"id "groovy"}

repositories {mavenCentral()}

dependencies {testImplementation 'io.github.bonigarcia:selenium-jupiter:4.0.1'testImplementation 'org.seleniumhq.selenium:selenium-java:4.1.2'testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'testImplementation 'org.gebish:geb-junit5:5.1'testImplementation 'org.assertj:assertj-core:3.22.0'}

task chromedriverTest(type: Test) {useJUnitPlatform()}

task chromeheadlessTest(type: Test) {useJUnitPlatform()}

test {useJUnitPlatform()testLogging {events "passed", "skipped", "failed"}

systemProperty("junit.jupiter.execution.parallel.enabled" , "true")
systemProperty("junit.jupiter.execution.parallel.config.strategy", "fixed")
systemProperty("junit.jupiter.execution.parallel.mode.default", "concurrent")
systemProperty("junit.jupiter.execution.parallel.config.fixed.parallelism", 2)

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文