Skip to content

Commit 054ade1

Browse files
committed
Include explicit wait in tests using the login page
1 parent 6dd9c2a commit 054ade1

File tree

16 files changed

+140
-25
lines changed

16 files changed

+140
-25
lines changed

selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch07/page_objects/BasicLoginPage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.junit4.ch07.page_objects;
1818

19+
import java.time.Duration;
20+
1921
import org.openqa.selenium.By;
2022
import org.openqa.selenium.WebDriver;
23+
import org.openqa.selenium.WebElement;
24+
import org.openqa.selenium.support.ui.ExpectedConditions;
25+
import org.openqa.selenium.support.ui.WebDriverWait;
2126

2227
public class BasicLoginPage {
2328

@@ -42,7 +47,10 @@ public void with(String username, String password) {
4247
}
4348

4449
public boolean successBoxPresent() {
45-
return driver.findElement(successBox).isDisplayed();
50+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
51+
WebElement success = wait.until(
52+
ExpectedConditions.visibilityOfElementLocated(successBox));
53+
return success.isDisplayed();
4654
}
4755

4856
}

selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch07/page_objects/ExtendedLoginPage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.junit4.ch07.page_objects;
1818

19+
import java.time.Duration;
20+
1921
import org.openqa.selenium.By;
22+
import org.openqa.selenium.WebElement;
23+
import org.openqa.selenium.support.ui.ExpectedConditions;
24+
import org.openqa.selenium.support.ui.WebDriverWait;
2025

2126
public class ExtendedLoginPage extends ExtendedBasePage {
2227

@@ -42,7 +47,10 @@ public void with(String username, String password) {
4247
}
4348

4449
public boolean successBoxPresent() {
45-
return isDisplayed(successBox);
50+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
51+
WebElement success = wait.until(
52+
ExpectedConditions.visibilityOfElementLocated(successBox));
53+
return success.isDisplayed();
4654
}
4755

4856
}

selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch07/page_objects/FactoryLoginPage.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.junit4.ch07.page_objects;
1818

19+
import java.time.Duration;
20+
1921
import org.openqa.selenium.WebElement;
2022
import org.openqa.selenium.support.CacheLookup;
2123
import org.openqa.selenium.support.FindBy;
2224
import org.openqa.selenium.support.PageFactory;
25+
import org.openqa.selenium.support.ui.ExpectedConditions;
26+
import org.openqa.selenium.support.ui.WebDriverWait;
2327

2428
public class FactoryLoginPage extends ExtendedBasePage {
2529

@@ -57,7 +61,10 @@ public void with(String username, String password) {
5761
}
5862

5963
public boolean successBoxPresent() {
60-
return isDisplayed(successBox);
64+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
65+
WebElement success = wait
66+
.until(ExpectedConditions.visibilityOf(successBox));
67+
return success.isDisplayed();
6168
}
6269

6370
}

selenium-webdriver-junit4/src/test/java/io/github/bonigarcia/webdriver/junit4/ch07/tests/VanillaBasicLoginJUnit4Test.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.junit4.ch07.tests;
1818

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

2121
import org.junit.After;
2222
import org.junit.Before;
2323
import org.junit.Test;
2424
import org.openqa.selenium.By;
2525
import org.openqa.selenium.WebDriver;
26+
import org.openqa.selenium.support.ui.ExpectedConditions;
27+
import org.openqa.selenium.support.ui.WebDriverWait;
2628

2729
import io.github.bonigarcia.wdm.WebDriverManager;
2830

@@ -49,7 +51,9 @@ public void testVanillaBasicLoginSuccess() {
4951
driver.findElement(By.id("password")).sendKeys("user");
5052
driver.findElement(By.cssSelector("button")).click();
5153

52-
assertThat(driver.findElement(By.id("success")).isDisplayed()).isTrue();
54+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
55+
wait.until(ExpectedConditions
56+
.visibilityOfElementLocated(By.id("success")));
5357
}
5458

5559
@Test
@@ -61,7 +65,9 @@ public void testVanillaBasicLoginFailure() {
6165
driver.findElement(By.id("password")).sendKeys("bad-password");
6266
driver.findElement(By.cssSelector("button")).click();
6367

64-
assertThat(driver.findElement(By.id("invalid")).isDisplayed()).isTrue();
68+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
69+
wait.until(ExpectedConditions
70+
.visibilityOfElementLocated(By.id("invalid")));
6571
}
6672

6773
}

selenium-webdriver-junit5-seljup/src/test/java/io/github/bonigarcia/webdriver/seljup/ch07/page_objects/BasicLoginPage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.seljup.ch07.page_objects;
1818

19+
import java.time.Duration;
20+
1921
import org.openqa.selenium.By;
2022
import org.openqa.selenium.WebDriver;
23+
import org.openqa.selenium.WebElement;
24+
import org.openqa.selenium.support.ui.ExpectedConditions;
25+
import org.openqa.selenium.support.ui.WebDriverWait;
2126

2227
public class BasicLoginPage {
2328

@@ -42,7 +47,10 @@ public void with(String username, String password) {
4247
}
4348

4449
public boolean successBoxPresent() {
45-
return driver.findElement(successBox).isDisplayed();
50+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
51+
WebElement success = wait.until(
52+
ExpectedConditions.visibilityOfElementLocated(successBox));
53+
return success.isDisplayed();
4654
}
4755

4856
}

selenium-webdriver-junit5-seljup/src/test/java/io/github/bonigarcia/webdriver/seljup/ch07/page_objects/ExtendedLoginPage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.seljup.ch07.page_objects;
1818

19+
import java.time.Duration;
20+
1921
import org.openqa.selenium.By;
22+
import org.openqa.selenium.WebElement;
23+
import org.openqa.selenium.support.ui.ExpectedConditions;
24+
import org.openqa.selenium.support.ui.WebDriverWait;
2025

2126
public class ExtendedLoginPage extends ExtendedBasePage {
2227

@@ -42,7 +47,10 @@ public void with(String username, String password) {
4247
}
4348

4449
public boolean successBoxPresent() {
45-
return isDisplayed(successBox);
50+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
51+
WebElement success = wait.until(
52+
ExpectedConditions.visibilityOfElementLocated(successBox));
53+
return success.isDisplayed();
4654
}
4755

4856
}

selenium-webdriver-junit5-seljup/src/test/java/io/github/bonigarcia/webdriver/seljup/ch07/page_objects/FactoryLoginPage.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.seljup.ch07.page_objects;
1818

19+
import java.time.Duration;
20+
1921
import org.openqa.selenium.WebElement;
2022
import org.openqa.selenium.support.CacheLookup;
2123
import org.openqa.selenium.support.FindBy;
2224
import org.openqa.selenium.support.PageFactory;
25+
import org.openqa.selenium.support.ui.ExpectedConditions;
26+
import org.openqa.selenium.support.ui.WebDriverWait;
2327

2428
public class FactoryLoginPage extends ExtendedBasePage {
2529

@@ -57,7 +61,10 @@ public void with(String username, String password) {
5761
}
5862

5963
public boolean successBoxPresent() {
60-
return isDisplayed(successBox);
64+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
65+
WebElement success = wait
66+
.until(ExpectedConditions.visibilityOf(successBox));
67+
return success.isDisplayed();
6168
}
6269

6370
}

selenium-webdriver-junit5-seljup/src/test/java/io/github/bonigarcia/webdriver/seljup/ch07/tests/VanillaBasicLoginJupiterTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.seljup.ch07.tests;
1818

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

2121
import org.junit.jupiter.api.Test;
2222
import org.junit.jupiter.api.extension.ExtendWith;
2323
import org.openqa.selenium.By;
2424
import org.openqa.selenium.chrome.ChromeDriver;
25+
import org.openqa.selenium.support.ui.ExpectedConditions;
26+
import org.openqa.selenium.support.ui.WebDriverWait;
2527

2628
import io.github.bonigarcia.seljup.SeleniumJupiter;
2729

@@ -37,7 +39,9 @@ void testVanillaBasicLoginSuccess(ChromeDriver driver) {
3739
driver.findElement(By.id("password")).sendKeys("user");
3840
driver.findElement(By.cssSelector("button")).click();
3941

40-
assertThat(driver.findElement(By.id("success")).isDisplayed()).isTrue();
42+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
43+
wait.until(ExpectedConditions
44+
.visibilityOfElementLocated(By.id("success")));
4145
}
4246

4347
@Test
@@ -49,7 +53,9 @@ void testVanillaBasicLoginFailure(ChromeDriver driver) {
4953
driver.findElement(By.id("password")).sendKeys("bad-password");
5054
driver.findElement(By.cssSelector("button")).click();
5155

52-
assertThat(driver.findElement(By.id("invalid")).isDisplayed()).isTrue();
56+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
57+
wait.until(ExpectedConditions
58+
.visibilityOfElementLocated(By.id("invalid")));
5359
}
5460

5561
}

selenium-webdriver-junit5/src/test/java/io/github/bonigarcia/webdriver/jupiter/ch07/page_objects/BasicLoginPage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.jupiter.ch07.page_objects;
1818

19+
import java.time.Duration;
20+
1921
import org.openqa.selenium.By;
2022
import org.openqa.selenium.WebDriver;
23+
import org.openqa.selenium.WebElement;
24+
import org.openqa.selenium.support.ui.ExpectedConditions;
25+
import org.openqa.selenium.support.ui.WebDriverWait;
2126

2227
public class BasicLoginPage {
2328

@@ -42,7 +47,10 @@ public void with(String username, String password) {
4247
}
4348

4449
public boolean successBoxPresent() {
45-
return driver.findElement(successBox).isDisplayed();
50+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
51+
WebElement success = wait.until(
52+
ExpectedConditions.visibilityOfElementLocated(successBox));
53+
return success.isDisplayed();
4654
}
4755

4856
}

selenium-webdriver-junit5/src/test/java/io/github/bonigarcia/webdriver/jupiter/ch07/page_objects/ExtendedLoginPage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
*/
1717
package io.github.bonigarcia.webdriver.jupiter.ch07.page_objects;
1818

19+
import java.time.Duration;
20+
1921
import org.openqa.selenium.By;
22+
import org.openqa.selenium.WebElement;
23+
import org.openqa.selenium.support.ui.ExpectedConditions;
24+
import org.openqa.selenium.support.ui.WebDriverWait;
2025

2126
public class ExtendedLoginPage extends ExtendedBasePage {
2227

@@ -42,7 +47,10 @@ public void with(String username, String password) {
4247
}
4348

4449
public boolean successBoxPresent() {
45-
return isDisplayed(successBox);
50+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
51+
WebElement success = wait.until(
52+
ExpectedConditions.visibilityOfElementLocated(successBox));
53+
return success.isDisplayed();
4654
}
4755

4856
}

0 commit comments

Comments
 (0)