liveBook · Manning

Selenium

As explained in BDD in Action, Second Edition

The WebDriver API is the programmatic interface for controlling browser sessions and interacting with web pages through WebDriver.

A successful login is more than a valid password check: a member must be able to enter credentials in the browser and reach the right account page. Selenium WebDriver automates that visible journey by launching a real browser, interacting with the page, and inspecting the result. It is useful when the UI behavior itself matters; business rules that do not require a browser are better tested below the UI layer. section 10.2.7 section 10.0

A test uses a language-specific WebDriver client to communicate with a browser-specific driver through the W3C WebDriver protocol. The same approach supports browsers including Chrome, Firefox, and Microsoft Edge; Selenium Grid can control remote browsers. Java is one option among several supported languages. section 10.2.6

A typical browser step follows this flow:

  • Open the application with `driver.get()`, then locate a page element with `findElement()`. section 10.2.11
  • Use the returned `WebElement` to click, type with `sendKeys()`, or read text and attributes. Clear a field first if existing text must be replaced. section 10.2.11 section 10.2.13
  • Assert the visible outcome, such as the signed-in member’s email on the account page. section 10.2.11
@When("{} logs on with a valid username and password")
public void logsOnWithAValidUsernameAndPassword()
  WebDriver driver = WebTestSupport.currentDriver();
 
  driver.get("http://localhost:3000");                                     #1
  driver.findElement(By.linkText("Login")).click();                        #2
  driver.findElement(By.id("email")).sendKeys(frequentFlyer.email);        #3
  driver.findElement(By.id("password")).sendKeys(frequentFlyer.password);  #3
  driver.findElement(By.id("login-button")).click();                       #4
}

This login step opens the site, follows the Login link, fills credentials using element IDs, and submits the form. section 10.2.11

For a registration form, locators can target IDs, CSS selectors, or XPath expressions. `Keys.TAB` can complete a keyboard interaction, and `Select` can choose a visible option. Meaningful IDs or CSS classes make these tests easier to sustain. Wait conditions help when an asynchronous page has not yet reached the expected state. section 10.2.13 section 10.5

Browser tests can capture screenshots for reports or illustrated documentation, but too many can slow the suite and create a maintenance burden. Keep them focused on important UI interactions and end-to-end journeys. section 10.2.6 section 10.5

How BDD in Action, Second Edition covers it

How deep it goes
Works through navigation, element lookup, interaction, and assertions; sketches browser-driver communication without detailing the protocol.
Examples
Uses member login and registration forms to demonstrate credential entry, link clicks, field selection, and outcome checks.
What it assumes
Introduces WebDriver from browser setup and basic interaction; requires no Node.js knowledge to run the sample website.
What you can do after
Apply it via Selenium WebDriver to automate selected Java browser tests and assert visible results.
How it connects
Connects UI acceptance testing with Cucumber step definitions, Gherkin scenarios, WebElement locators, Selenium Grid, and living documentation.

Also explained in

Compare how 2 books explain Selenium →

This explainer was generated by Manning's knowledge graph from the text of BDD in Action, Second Edition. The section links point to the parts of the book it draws on.