Skip to content

Commit ea81291

Browse files
committed
amending to match the text
1 parent be9bf7c commit ea81291

9 files changed

Lines changed: 171 additions & 36 deletions

File tree

pom.xml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>eviltester.com</groupId>
8-
<artifactId>startUsingSelenium4</artifactId>
8+
<artifactId>startUsingSeleniumWebDriver</artifactId>
99
<version>1.0</version>
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13-
1413
<!-- add junit -->
1514
<junit.jupiter.version>5.14.0</junit.jupiter.version>
1615

16+
<!-- configure webdriver version -->
17+
<selenium-webdriver-version>4.38.0</selenium-webdriver-version>
18+
1719
<!-- configure the compiler via properties rather than plugin -->
1820
<maven.compiler.source>21</maven.compiler.source>
1921
<maven.compiler.target>21</maven.compiler.target>
@@ -24,17 +26,13 @@
2426
<!-- to generate html junit reports for test phase -->
2527
<maven-site-plugin.version>4.0.0-M14</maven-site-plugin.version>
2628
<maven.surefire.report.version>3.2.5</maven.surefire.report.version>
27-
28-
29-
<!-- configure webdriver version -->
30-
<selenium-webdriver-version>4.38.0</selenium-webdriver-version>
3129
</properties>
3230

3331
<!--
34-
To run the Chrome test either right click on 'MyFirstChromeTest'
32+
To run the basic Chrome test either right click on 'AFirstChromeTest'
3533
and run it, or at the command line type:
3634
37-
`mvn test -Dtest=MyFirstChromeTest`
35+
`mvn test -Dtest=AFirstChromeTest`
3836
3937
If you type `mvn test` it will run all the tests.
4038
-->
@@ -44,7 +42,7 @@
4442
<!--
4543
You might need to update this number to the most recent version,
4644
you can find it on the official SeleniumHQ.org download page
47-
https://www.seleniumhq.org/download/
45+
https://www.selenium.dev/downloads/
4846
-->
4947
<dependency>
5048
<groupId>org.seleniumhq.selenium</groupId>
@@ -64,7 +62,8 @@
6462
<build>
6563
<plugins>
6664
<!--
67-
Maven Surefire Plugin to run the Unit tests
65+
Maven Surefire Plugin to run the Unit tests and generate
66+
xml reports
6867
https://maven.apache.org/surefire/maven-surefire-plugin/usage.html
6968
-->
7069
<plugin>

src/test/java/com/eviltester/webdriver/basics/AFirstChromeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99

1010
public class AFirstChromeTest {
1111
@Test
12-
public void startWithWebDriver(){
12+
public void myFirstWebDriverTest(){
1313

1414
WebDriver driver = new ChromeDriver();
1515

1616
driver.get(
1717
"https://testpages.eviltester.com/pages/basics/basic-web-page/"
1818
);
1919

20-
WebElement elem = driver.findElement(By.tagName("h1"));
20+
WebElement button = driver.findElement(By.id("button1"));
2121

22-
Assertions.assertEquals(elem.getText(), "Basic Web Page");
22+
Assertions.assertEquals("Click Me", button.getText());
2323

2424
driver.quit();
2525
}

src/test/java/com/eviltester/webdriver/basicsOfJunit/BeforeAfterChromeTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public void loadPage(){
2323
}
2424

2525
@Test
26-
public void pageHasCorrectHeading(){
27-
WebElement elem = driver.findElement(By.tagName("h1"));
26+
public void buttonHasCorrectText(){
27+
WebElement button = driver.findElement(By.id("button1"));
2828

29-
Assertions.assertEquals(elem.getText(), "Basic Web Page");
29+
Assertions.assertEquals("Click Me", button.getText());
3030
}
3131

3232
@AfterAll

src/test/java/com/eviltester/webdriver/basicsOfPageObjects/BasicWebPage.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import org.openqa.selenium.By;
44
import org.openqa.selenium.WebDriver;
55
import org.openqa.selenium.WebElement;
6+
import org.openqa.selenium.support.ui.ExpectedCondition;
7+
import org.openqa.selenium.support.ui.ExpectedConditions;
68

79
public class BasicWebPage {
10+
811
private final WebDriver driver;
912

1013
// locators
11-
public final By HEADING1_LOCATOR = By.tagName("h1");
14+
public final By CLICK_ME_BUTTON = By.id("button1");
15+
public final By CLICK_MESSAGE = By.id("click-message");
1216

1317
public BasicWebPage(WebDriver driver) {
1418
this.driver = driver;
@@ -20,12 +24,24 @@ public void get() {
2024
}
2125

2226
// element accessors
23-
public WebElement getHeading1(){
24-
return driver.findElement(HEADING1_LOCATOR);
27+
public WebElement getButton(){
28+
return driver.findElement(CLICK_ME_BUTTON);
2529
}
2630

2731
// helpers
28-
public String getHeadingText() {
29-
return getHeading1().getText();
32+
public String getButtonText() {
33+
return getButton().getText();
34+
}
35+
36+
public WebElement getClickMessage() {
37+
return driver.findElement(CLICK_MESSAGE);
38+
}
39+
40+
public ExpectedCondition<Boolean> showsMessage() {
41+
return ExpectedConditions.not(ExpectedConditions.textToBe(CLICK_MESSAGE, ""));
42+
}
43+
44+
public ExpectedCondition<Boolean> successMessageNotShown() {
45+
return ExpectedConditions.textToBe(CLICK_MESSAGE, "");
3046
}
3147
}

src/test/java/com/eviltester/webdriver/basicsOfPageObjects/UsingPageObjectsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public void loadPage(){
2121
}
2222

2323
@Test
24-
public void pageHasCorrectHeading(){
25-
Assertions.assertEquals(page.getHeadingText(), "Basic Web Page");
24+
public void pageHasCorrectButtonText(){
25+
Assertions.assertEquals(page.getButtonText(), "Click Me");
2626
}
2727

2828
@AfterAll

src/test/java/com/eviltester/webdriver/basicsOfSynchronization/WaitingForStateTest.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.eviltester.webdriver.basicsOfPageObjects.BasicWebPage;
44
import org.junit.jupiter.api.*;
5+
import org.openqa.selenium.By;
56
import org.openqa.selenium.WebDriver;
7+
import org.openqa.selenium.WebElement;
68
import org.openqa.selenium.chrome.ChromeDriver;
79
import org.openqa.selenium.support.ui.ExpectedConditions;
810
import org.openqa.selenium.support.ui.WebDriverWait;
@@ -26,23 +28,39 @@ public void loadPage(){
2628
}
2729

2830
@Test
29-
public void pageHasCorrectHeading(){
31+
public void buttonHasCorrectText(){
3032
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
3133

32-
wait.until(ExpectedConditions.visibilityOf(page.getHeading1()));
34+
wait.until(ExpectedConditions.elementToBeClickable(page.getButton()));
3335

34-
Assertions.assertEquals(page.getHeadingText(), "Basic Web Page");
36+
Assertions.assertEquals("Click Me", page.getButtonText());
3537
}
3638

3739
@Test
38-
public void pageHasCorrectHeadingSyncBy(){
40+
public void buttonHasCorrectTextSyncBy(){
3941
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
4042

41-
wait.until(ExpectedConditions.visibilityOfElementLocated(page.HEADING1_LOCATOR));
43+
wait.until(ExpectedConditions.elementToBeClickable(page.CLICK_ME_BUTTON));
4244

43-
Assertions.assertEquals(page.getHeadingText(), "Basic Web Page");
45+
Assertions.assertEquals("Click Me", page.getButtonText());
4446
}
4547

48+
@Test
49+
public void clickingButtonShowsMessage(){
50+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
51+
52+
wait.until(ExpectedConditions.elementToBeClickable(page.CLICK_ME_BUTTON));
53+
page.getButton().click();
54+
55+
WebElement message = page.getClickMessage();
56+
57+
// this might be flaky so we can wait for condition
58+
String successMessage = "You clicked the button!";
59+
wait.until(ExpectedConditions.textToBe(page.CLICK_MESSAGE, successMessage));
60+
Assertions.assertEquals(successMessage, message.getText());
61+
}
62+
63+
4664
@AfterAll
4765
public static void closeWebDriver(){
4866
driver.quit();
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.eviltester.webdriver.basicsOfSynchronization;
2+
3+
import com.eviltester.webdriver.basicsOfPageObjects.BasicWebPage;
4+
import org.junit.jupiter.api.*;
5+
import org.openqa.selenium.WebDriver;
6+
import org.openqa.selenium.WebElement;
7+
import org.openqa.selenium.chrome.ChromeDriver;
8+
import org.openqa.selenium.support.ui.ExpectedConditions;
9+
import org.openqa.selenium.support.ui.WebDriverWait;
10+
11+
import java.time.Duration;
12+
13+
public class WaitingInsteadOfAssertionTest {
14+
15+
static WebDriver driver;
16+
private BasicWebPage page;
17+
18+
@BeforeAll
19+
public static void initiateWebDriver(){
20+
driver = new ChromeDriver();
21+
}
22+
23+
@BeforeEach
24+
public void loadPage(){
25+
page = new BasicWebPage(driver);
26+
page.get();
27+
}
28+
29+
@Test
30+
public void clickingButtonShowsMessageWait(){
31+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
32+
33+
wait.until(ExpectedConditions.elementToBeClickable(page.CLICK_ME_BUTTON));
34+
page.getButton().click();
35+
36+
WebElement message = page.getClickMessage();
37+
38+
// this might be flaky
39+
// Assertions.assertEquals("You clicked the button!", message.getText());
40+
// instead wait for the text to appear and disappear
41+
// no asserts are really needed but we can add them so that reviewers know the waits
42+
// are not just for synchronisation
43+
String successMessage = "You clicked the button!";
44+
45+
wait.until(ExpectedConditions.textToBe(page.CLICK_MESSAGE, successMessage));
46+
Assertions.assertEquals(successMessage, message.getText());
47+
48+
wait.until(ExpectedConditions.textToBe(page.CLICK_MESSAGE, ""));
49+
Assertions.assertEquals("", message.getText());
50+
}
51+
52+
@Test
53+
public void clickingButtonShowsMessageSyncWaitDifferentFromAssert(){
54+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
55+
56+
wait.until(ExpectedConditions.elementToBeClickable(page.CLICK_ME_BUTTON));
57+
page.getButton().click();
58+
59+
WebElement message = page.getClickMessage();
60+
61+
// The asserts are different from the wait conditions
62+
// so both are necessary now
63+
String successMessage = "You clicked the button!";
64+
65+
//wait until a message is shown
66+
wait.until(ExpectedConditions.not(ExpectedConditions.textToBe(page.CLICK_MESSAGE, "")));
67+
Assertions.assertEquals(successMessage, message.getText());
68+
69+
// wait until the success message is not shown
70+
wait.until(ExpectedConditions.not(ExpectedConditions.textToBe(page.CLICK_MESSAGE, successMessage)));
71+
Assertions.assertEquals("", message.getText());
72+
}
73+
74+
@Test
75+
public void clickingButtonShowsMessagePageAbstraction(){
76+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
77+
78+
wait.until(ExpectedConditions.elementToBeClickable(page.CLICK_ME_BUTTON));
79+
page.getButton().click();
80+
81+
WebElement message = page.getClickMessage();
82+
83+
/*
84+
By moving the wait conditions into the page abstraction
85+
the test is more readable, and we maintain sync and assert
86+
conditions separately
87+
*/
88+
wait.until(page.showsMessage());
89+
Assertions.assertEquals("You clicked the button!", message.getText());
90+
91+
wait.until(page.successMessageNotShown());
92+
Assertions.assertEquals("", message.getText());
93+
}
94+
95+
@AfterAll
96+
public static void closeWebDriver(){
97+
driver.quit();
98+
}
99+
}

src/test/java/com/eviltester/webdriver/browsers/ConfigurableChromeTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.eviltester.webdriver.browsers;
22

3+
import com.eviltester.webdriver.basicsOfPageObjects.BasicWebPage;
34
import org.junit.jupiter.api.*;
4-
import org.openqa.selenium.By;
55
import org.openqa.selenium.WebDriver;
66
import org.openqa.selenium.chrome.ChromeDriver;
77
import org.openqa.selenium.chrome.ChromeOptions;
@@ -30,11 +30,12 @@ public static void initiateWebDriver(){
3030
@Test
3131
public void startWebDriver(){
3232

33-
driver.navigate().to("https://testpages.eviltester.com/pages/basics/basic-web-page/");
33+
BasicWebPage page = new BasicWebPage(driver);
34+
page.get();
3435

3536
Assertions.assertEquals(
36-
driver.findElement(By.tagName("h1")).getText(),
37-
"Basic Web Page"
37+
"Click Me",
38+
page.getButton().getText()
3839
);
3940

4041
}

src/test/java/com/eviltester/webdriver/browsers/ConfigurableFirefoxTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.eviltester.webdriver.browsers;
22

3+
import com.eviltester.webdriver.basicsOfPageObjects.BasicWebPage;
34
import org.junit.jupiter.api.AfterAll;
45
import org.junit.jupiter.api.Assertions;
56
import org.junit.jupiter.api.BeforeAll;
@@ -34,11 +35,12 @@ public static void initiateWebDriver(){
3435
@Test
3536
public void startWebDriver(){
3637

37-
driver.navigate().to("https://testpages.eviltester.com/pages/basics/basic-web-page/");
38+
BasicWebPage page = new BasicWebPage(driver);
39+
page.get();
3840

3941
Assertions.assertEquals(
40-
driver.findElement(By.tagName("h1")).getText(),
41-
"Basic Web Page"
42+
"Click Me",
43+
page.getButton().getText()
4244
);
4345

4446
}

0 commit comments

Comments
 (0)