0

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)

}
  • Just two days ago, there was some related discussion in the [Spock chat on Gitter](https://gitter.im/spockframework/spock). Look for a thread started by _jcattau_ on Mar-23. Please also note that Spock 2.x itself supports running parallel tests, instead of you having to configure it via Jupiter. But of course, when using JUnit Jupiter, you are stuck there, but I don't know much about it. Spock is a separate engine on top of the JUnit 5 platform (i.e. an independent sibling to Jupiter). If you use Groovy for writing your tests anyway, why not switch to Spock, too? – kriegaex Mar 25 '22 at 02:32
  • I forgot to mention: Of course you do not need to migrate to Spock, even though I have no idea why anyone would prefer JUnit over Spock in the first place. But even if you cannot migrate due to external restrictions, maybe you can take some ideas from that chat and adapt them to your situation. The related Spock issue is [#1449](https://github.com/spockframework/spock/issues/1449). The content intersects with the Spock chat. – kriegaex Mar 25 '22 at 03:10

0 Answers0