Skip to content

Commit ae318ce

Browse files
committed
Include explicit wait in login steps for Cucumber tests
1 parent f3df7e3 commit ae318ce

File tree

4 files changed

+28
-16
lines changed
  • selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch09/cucumber
  • selenium-webdriver-junit5-seljup/src/test/java/io/github/bonigarcia/webdriver/seljup/ch09/cucumber
  • selenium-webdriver-junit5/src/test/java/io/github/bonigarcia/webdriver/jupiter/ch09/cucumber
  • selenium-webdriver-testng/src/test/java/io/github/bonigarcia/webdriver/testng/ch09/cucumber

4 files changed

+28
-16
lines changed

selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch09/cucumber/LoginSteps.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.junit4.ch09.cucumber;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
19+
import java.time.Duration;
2020

2121
import org.openqa.selenium.By;
2222
import org.openqa.selenium.WebDriver;
23+
import org.openqa.selenium.support.ui.ExpectedConditions;
24+
import org.openqa.selenium.support.ui.WebDriverWait;
2325

2426
import io.cucumber.java.en.And;
2527
import io.cucumber.java.en.Given;
@@ -45,7 +47,6 @@ public void iNavigateTo(String url) {
4547
public void iLogin(String username, String password) {
4648
driver.findElement(By.id("username")).sendKeys(username);
4749
driver.findElement(By.id("password")).sendKeys(password);
48-
4950
}
5051

5152
@And("I click Submit")
@@ -56,8 +57,10 @@ public void iPressEnter() {
5657
@Then("I should see the message {string}")
5758
public void iShouldSee(String result) {
5859
try {
59-
String bodyText = driver.findElement(By.tagName("body")).getText();
60-
assertThat(bodyText).contains(result);
60+
WebDriverWait wait = new WebDriverWait(driver,
61+
Duration.ofSeconds(10));
62+
wait.until(ExpectedConditions.textToBePresentInElementLocated(
63+
By.tagName("body"), result));
6164
} finally {
6265
driver.quit();
6366
}

selenium-webdriver-junit5-seljup/src/test/java/io/github/bonigarcia/webdriver/seljup/ch09/cucumber/LoginSteps.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.seljup.ch09.cucumber;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
19+
import java.time.Duration;
2020

2121
import org.openqa.selenium.By;
2222
import org.openqa.selenium.WebDriver;
23+
import org.openqa.selenium.support.ui.ExpectedConditions;
24+
import org.openqa.selenium.support.ui.WebDriverWait;
2325

2426
import io.cucumber.java.en.And;
2527
import io.cucumber.java.en.Given;
@@ -45,7 +47,6 @@ public void iNavigateTo(String url) {
4547
public void iLogin(String username, String password) {
4648
driver.findElement(By.id("username")).sendKeys(username);
4749
driver.findElement(By.id("password")).sendKeys(password);
48-
4950
}
5051

5152
@And("I click Submit")
@@ -56,8 +57,10 @@ public void iPressEnter() {
5657
@Then("I should see the message {string}")
5758
public void iShouldSee(String result) {
5859
try {
59-
String bodyText = driver.findElement(By.tagName("body")).getText();
60-
assertThat(bodyText).contains(result);
60+
WebDriverWait wait = new WebDriverWait(driver,
61+
Duration.ofSeconds(10));
62+
wait.until(ExpectedConditions.textToBePresentInElementLocated(
63+
By.tagName("body"), result));
6164
} finally {
6265
driver.quit();
6366
}

selenium-webdriver-junit5/src/test/java/io/github/bonigarcia/webdriver/jupiter/ch09/cucumber/LoginSteps.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.jupiter.ch09.cucumber;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
19+
import java.time.Duration;
2020

2121
import org.openqa.selenium.By;
2222
import org.openqa.selenium.WebDriver;
23+
import org.openqa.selenium.support.ui.ExpectedConditions;
24+
import org.openqa.selenium.support.ui.WebDriverWait;
2325

2426
import io.cucumber.java.en.And;
2527
import io.cucumber.java.en.Given;
@@ -45,7 +47,6 @@ public void iNavigateTo(String url) {
4547
public void iLogin(String username, String password) {
4648
driver.findElement(By.id("username")).sendKeys(username);
4749
driver.findElement(By.id("password")).sendKeys(password);
48-
4950
}
5051

5152
@And("I click Submit")
@@ -56,8 +57,10 @@ public void iPressEnter() {
5657
@Then("I should see the message {string}")
5758
public void iShouldSee(String result) {
5859
try {
59-
String bodyText = driver.findElement(By.tagName("body")).getText();
60-
assertThat(bodyText).contains(result);
60+
WebDriverWait wait = new WebDriverWait(driver,
61+
Duration.ofSeconds(10));
62+
wait.until(ExpectedConditions.textToBePresentInElementLocated(
63+
By.tagName("body"), result));
6164
} finally {
6265
driver.quit();
6366
}

selenium-webdriver-testng/src/test/java/io/github/bonigarcia/webdriver/testng/ch09/cucumber/LoginSteps.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.testng.ch09.cucumber;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
19+
import java.time.Duration;
2020

2121
import org.openqa.selenium.By;
2222
import org.openqa.selenium.WebDriver;
23+
import org.openqa.selenium.support.ui.ExpectedConditions;
24+
import org.openqa.selenium.support.ui.WebDriverWait;
2325

2426
import io.cucumber.java.en.And;
2527
import io.cucumber.java.en.Given;
@@ -45,7 +47,6 @@ public void iNavigateTo(String url) {
4547
public void iLogin(String username, String password) {
4648
driver.findElement(By.id("username")).sendKeys(username);
4749
driver.findElement(By.id("password")).sendKeys(password);
48-
4950
}
5051

5152
@And("I click Submit")
@@ -56,8 +57,10 @@ public void iPressEnter() {
5657
@Then("I should see the message {string}")
5758
public void iShouldSee(String result) {
5859
try {
59-
String bodyText = driver.findElement(By.tagName("body")).getText();
60-
assertThat(bodyText).contains(result);
60+
WebDriverWait wait = new WebDriverWait(driver,
61+
Duration.ofSeconds(10));
62+
wait.until(ExpectedConditions.textToBePresentInElementLocated(
63+
By.tagName("body"), result));
6164
} finally {
6265
driver.quit();
6366
}

0 commit comments

Comments
 (0)