Skip to main content

The Same Plan in Java and WebdriverIO

Why This Matters

Most companies don’t test in Python. A Java team has a Maven project with JUnit 5 and page objects; a front-end team writes WebdriverIO in TypeScript; a test team has years of Robot Framework. A test agent that only writes pytest is a demo, not a tool.

Selenium AI Agent writes the language and framework of the project it’s in. The plan, the review, the rules, the guardrails and the gate stay exactly the same; only the code changes. This lesson takes one plan scenario and shows it in Python, Java, C#, WebdriverIO and Robot Framework, so you can see what changes and what doesn’t.

What you will learn

  • How the generator chooses the language and framework
  • One scenario in five stacks, side by side
  • What stays the same across languages, and what each one adds
  • Selenium’s rules versus WebdriverIO’s own rules
  • Where the fail-for-the-right-reason check is not available, and what to use instead

How the framework is chosen

  1. You name it: “Write the tests in Java with JUnit 5.”

  2. The project says it. Without a name, generator_setup_page reads the build files and reports what it found:

    FoundWrites
    pom.xml / build.gradleJava with JUnit 5 or TestNG (whichever the project uses)
    *.csprojC# with NUnit, xUnit or MSTest
    package.json with @wdio/*WebdriverIO (TypeScript or JavaScript)
    package.json with selenium-webdriverSelenium for JavaScript (Mocha or Jest)
    pytest.ini, pyproject.toml, requirements.txtPython with pytest (or unittest)
    *.robot filesRobot Framework with SeleniumLibrary
  3. An empty project gets Python with pytest, and the agent says so in one line.

The generator follows the project’s own structure: its folders, base classes and page objects. That matters more than the language: a generated test that ignores the team’s BasePage will be rewritten by the first developer who sees it.

One scenario, five stacks

The scenario, from a plan for the-internet (a practice site that publishes its demo credentials on the page):

### 1.2 Wrong password shows an error and stays on the login page

1. Open `/login`, enter `tomsmith` with a wrong password, click Login
   - expect: the message says "Your password is invalid!"

Java with JUnit 5 and page objects

From the Java sample project in the agent’s repository (examples/java-junit5). The test stays short because the page object hides the locators and waits:

/** Plan section 1: Login (specs/the-internet.md). The site publishes these practice credentials on the page. */
class LoginTest extends BaseTest {

    @Test
    @DisplayName("1.2 Wrong password shows an error and stays on the login page")
    void wrongPassword() {
        LoginPage login = new LoginPage(driver).open(BASE_URL).submitInvalid("tomsmith", "wrong-password");
        assertTrue(login.flashMessage().contains("Your password is invalid!"));
    }
}
/** Shared helpers for the page objects: every lookup waits for the condition it needs. */
public abstract class BasePage {
    protected final WebDriver driver;
    protected final WebDriverWait wait;

    protected BasePage(WebDriver driver) {
        this.driver = driver;
        this.wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    protected WebElement visible(By locator) {
        return wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
    }

    protected void click(By locator) {
        wait.until(ExpectedConditions.elementToBeClickable(locator)).click();
    }
}

The BaseTest starts a fresh ChromeDriver with ChromeOptions before each test (@BeforeEach) and quits it after (@AfterEach). Note Duration.ofSeconds(10): new WebDriverWait(driver, 10) is a Selenium 3 API and an error (java-timeunit-timeout). @DisplayName carries the plan scenario, so the JUnit report shows “1.2 Wrong password…”.

C# with NUnit

From examples/csharp-nunit, the same structure:

/// <summary>Plan section 1: Login (specs/the-internet.md). The site publishes these practice credentials on the page.</summary>
public class LoginTests : BaseTest
{
    [Test(Description = "1.2 Wrong password shows an error and stays on the login page")]
    public void WrongPassword()
    {
        var login = new LoginPage(Driver).Open(BaseUrl).SubmitInvalid("tomsmith", "wrong-password");
        Assert.That(login.FlashMessage, Does.Contain("Your password is invalid!"));
    }
}

.NET has its own rule: there is no ExpectedConditions in Selenium 4 for .NET, so waits are lambdas: new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(d => ...). Teardown must call Quit(), not Close() (csharp-close-not-quit).

WebdriverIO with TypeScript

// Plan section 1: Login (specs/the-internet.md). The site publishes these practice credentials on the page.
import { browser, $, expect } from '@wdio/globals'

describe('1. Login', () => {
    it('1.2 Wrong password shows an error and stays on the login page', async () => {
        await browser.url('/login')
        await $('#username').setValue('tomsmith')
        await $('#password').setValue('wrong-password')
        await $('button[type="submit"]').click()
        // expect: the message says "Your password is invalid!"
        await expect($('#flash')).toHaveText(expect.stringContaining('Your password is invalid!'))
        await expect(browser).toHaveUrl(expect.stringContaining('/login'))
    })
})

No explicit waits here, and that is correct for WebdriverIO: its expect matchers retry until they pass or time out, and $() waits for the element. Writing WebDriverWait-style code in WebdriverIO would be the mistake.

Robot Framework with SeleniumLibrary

*** Settings ***
Documentation     Plan section 1: Login (specs/the-internet.md). Practice credentials published on the site.
Library           SeleniumLibrary
Test Setup        Open Browser    ${BASE_URL}/login    chrome    options=add_argument("--headless=new")
Test Teardown     Close Browser

*** Variables ***
${BASE_URL}       https://the-internet.herokuapp.com

*** Test Cases ***
1.2 Wrong Password Shows An Error And Stays On The Login Page
    Input Text                        id:username    tomsmith
    Input Password                    id:password    wrong-password
    Click Button                      css:button[type="submit"]
    # expect: the message says "Your password is invalid!"
    Wait Until Element Contains       id:flash    Your password is invalid!
    Location Should Contain           /login

The same rules in Robot’s words: no Sleep, no Set Selenium Speed to slow things down, Wait Until ... keywords, a fresh browser per test in Test Setup and Test Teardown.

Python with pytest

For completeness, the version you know from lesson 10, with a fixture instead of a base class:

"""Plan 1.2: Wrong password shows an error and stays on the login page (specs/the-internet.md)."""
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


def test_wrong_password(login_page):
    login_page.submit("tomsmith", "wrong-password")
    # expect: the message says "Your password is invalid!"
    login_page.wait.until(EC.text_to_be_present_in_element((By.ID, "flash"), "Your password is invalid!"))
    assert "/login" in login_page.driver.current_url

What stays the same

In every stack
The planOne plan, approved once, whatever the language
One scenario, one testNamed after the scenario (@DisplayName, Description, it(...), the Robot test name) so reports link back to the plan
ExpectationsEvery expect: is one check, with the plan’s value
The browserOne fresh session per test, quit in teardown
WaitingFor a condition, never for time
The driverSelenium Manager (or WebdriverIO’s own); no driver paths or managers
The guardrailsPlan approval, the rule check, the heal policy, locked locators, the gate: all language-aware

The heal policy (lesson 15) classifies changes in Python, Java, C#, JavaScript/TypeScript and Robot Framework: a changed By.id("x") is a locator change in Java just as (By.ID, "x") is in Python.

Selenium’s rules versus WebdriverIO’s rules

For Selenium in Python, Java, C#, JavaScript, Ruby and Robot Framework, the agent checks Selenium’s rules for AI agents. For WebdriverIO it checks WebdriverIO’s own, because WebdriverIO’s best practices differ. A real refusal:

WebdriverIO rules (https://webdriver.io/docs/ai-agents): 1 error(s), 1 warning(s) in bad.spec.ts
  ERROR line 3 [wdio-no-pause] await browser.pause(2000)
        → Don't pause: wait for the condition with waitForDisplayed / waitForExist / waitForEnabled / waitForClickable, browser.waitUntil(), or a retrying expect matcher.
  warn  line 4 [wdio-xpath] await $('//form/button').click()
        → Avoid XPath: prefer what the user sees (aria/..., button=..., h4*=...), then [data-testid="..."], then ids.

WebdriverIO’s errors also cover the synchronous mode and v4 APIs that no longer exist, and the APIs removed in v10 (executeAsync, switchToFrame, toHaveAttr, …). The agent writes v10 code.

Where the fail check stops

verify_test_stability runs stability runs in every language. Its second part, the assertion mutants (lesson 17), works for Python, JavaScript/TypeScript, WebdriverIO and Robot Framework, but not for Java and C#: a mutant copy of a test class would be a duplicate class and break the build. The result says so (skipped), and the project’s own mutation tool takes over: PIT for Java, Stryker.NET for C#.

Exercise

  1. Clone the agent’s repository and run the Java sample: cd examples/java-junit5 && HEADLESS=1 mvn test. (Java 17+ and Maven needed.)
  2. Ask the generator to write the missing test from examples/java-junit5/specs/the-internet.md (section 4 has no test yet). Does it use the project’s BasePage, BaseTest and page objects, or does it write its own driver code?
  3. Ask for the same scenario in WebdriverIO: “Write test 1.2 of the-internet plan in WebdriverIO with TypeScript.” Find one difference with the Java version in how it waits.
Show suggested answer
  1. Expected: Tests run: 6, Failures: 0, Errors: 0.
  2. A good result adds a page object for the new page (extending BasePage), a test class extending BaseTest, and @DisplayName with the scenario number. If the generator writes new ChromeDriver() in the test, ask it to follow the project’s structure: that is a real defect in a generated test even when it passes.
  3. Java waits explicitly in the page objects (wait.until(ExpectedConditions...)); WebdriverIO relies on its retrying expect matchers and $() auto-waiting. Both wait for a condition; neither sleeps.

Exam-style questions

Quiz

1. A project has a pom.xml with JUnit 5. You ask for tests without naming a language. What does the generator write?

  • A. Python with pytest, the default
  • B. Java with JUnit 5, following the project
  • C. Whatever the AI prefers
  • D. Robot Framework
Show answer
B. The default only applies in an empty project.
Quiz

2. Why is await browser.pause(2000) refused in a WebdriverIO test?

  • A. WebdriverIO doesn’t support pause
  • B. WebdriverIO’s own rules: wait for the condition (waitFor…, waitUntil, retrying expect), never for time
  • C. It’s a Selenium 3 API
  • D. It’s only refused under strict
Show answer
B. It is refused under every profile.
Quiz

3. Which tool proves that Java assertions can fail, since verify_test_stability skips mutants for Java?

  • A. Stryker.NET
  • B. PIT
  • C. JUnit’s assertThrows
  • D. Allure
Show answer
B. Stryker.NET is the C# equivalent.