Skip to content

Commit 6abe107

Browse files
Give each Test the ability to run on Sauce Labs with browser of choice (Code Design Pattern: Strategy Pattern)
1 parent 60240d2 commit 6abe107

File tree

6 files changed

+170
-8
lines changed

6 files changed

+170
-8
lines changed

src/test/java/todomvc/acceptancetests/BaseTest.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,33 @@
22

33
import org.junit.jupiter.api.AfterEach;
44
import org.junit.jupiter.api.BeforeEach;
5-
import org.junit.jupiter.params.ParameterizedTest;
65
import org.junit.jupiter.params.provider.Arguments;
7-
import org.junit.jupiter.params.provider.CsvSource;
8-
import org.junit.jupiter.params.provider.MethodSource;
96
import org.openqa.selenium.WebDriver;
107
import org.openqa.selenium.chrome.ChromeDriver;
118
import org.openqa.selenium.firefox.FirefoxDriver;
9+
import org.openqa.selenium.remote.RemoteWebDriver;
1210
import todomvc.pageactions.ToDoPage;
1311

12+
import java.net.MalformedURLException;
1413
import java.util.stream.Stream;
1514

1615
public class BaseTest {
1716
WebDriver driver;
1817
ToDoPage toDoPage;
1918

2019
@BeforeEach
21-
public void setUp(){
20+
public void setUp() {
2221
this.driver = new ChromeDriver();
2322
toDoPage = new ToDoPage(driver);
2423
}
2524

2625
@AfterEach
27-
public void tearDown(){
26+
public void tearDown() {
2827
driver.quit();
2928
}
3029

30+
31+
3132
//Method source for each Test method in the Child Test Classes
3233
//This base class does not use this method
3334
public static Stream<Arguments> browserDriverProvider() {
@@ -36,4 +37,23 @@ public static Stream<Arguments> browserDriverProvider() {
3637
Arguments.of(new FirefoxDriver())
3738
);
3839
}
40+
41+
42+
/*
43+
* SAUCE LABS
44+
* */
45+
//Method to connect to SauceLabs and run tests
46+
//Called by at least by one child test. Not called within this BaseTest.
47+
public RemoteWebDriver remoteDriverForSauceLabsOn(String browserName) throws MalformedURLException {
48+
BrowserDriver browserDriver = getRemoteDriverForBrowser(browserName);
49+
return browserDriver.configure();
50+
}
51+
52+
private BrowserDriver getRemoteDriverForBrowser(String browserName) {
53+
switch(browserName){
54+
case "Firefox": return new FirefoxBrowser();
55+
case "Chrome":
56+
default: return new ChromeBrowser();
57+
}
58+
}
3959
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package todomvc.acceptancetests;
2+
3+
import org.openqa.selenium.remote.RemoteWebDriver;
4+
5+
import java.net.MalformedURLException;
6+
7+
public interface BrowserDriver {
8+
public RemoteWebDriver configure() throws MalformedURLException;
9+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package todomvc.acceptancetests;
2+
3+
import org.openqa.selenium.chrome.ChromeOptions;
4+
import org.openqa.selenium.remote.RemoteWebDriver;
5+
6+
import java.net.MalformedURLException;
7+
import java.net.URL;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
public class ChromeBrowser implements BrowserDriver {
12+
@Override
13+
public RemoteWebDriver configure() throws MalformedURLException {
14+
URL url = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
15+
RemoteWebDriver driver = new RemoteWebDriver(url, chromeOptions());
16+
return driver;
17+
}
18+
19+
public ChromeOptions chromeOptions(){
20+
ChromeOptions browserOptions = new ChromeOptions();
21+
browserOptions.setPlatformName("Windows 11");
22+
browserOptions.setBrowserVersion("latest");
23+
browserOptions.setCapability("sauce:options", sauceOptions());
24+
25+
return browserOptions;
26+
}
27+
28+
public Map<String, Object> sauceOptions(){
29+
Map<String, Object> sauceOptionsMap = new HashMap<>();
30+
sauceOptionsMap.put("username", System.getenv("SAUCE_USERNAME"));
31+
sauceOptionsMap.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
32+
33+
return sauceOptionsMap;
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package todomvc.acceptancetests;
2+
3+
import org.openqa.selenium.Capabilities;
4+
import org.openqa.selenium.chrome.ChromeOptions;
5+
import org.openqa.selenium.firefox.FirefoxOptions;
6+
import org.openqa.selenium.remote.RemoteWebDriver;
7+
8+
import java.net.MalformedURLException;
9+
import java.net.URL;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
public class FirefoxBrowser implements BrowserDriver {
14+
15+
@Override
16+
public RemoteWebDriver configure() throws MalformedURLException {
17+
URL url = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
18+
RemoteWebDriver driver = new RemoteWebDriver(url, fireFoxOptions());
19+
return driver;
20+
}
21+
22+
private FirefoxOptions fireFoxOptions() {
23+
FirefoxOptions browserOptions = new FirefoxOptions();
24+
browserOptions.setPlatformName("macOs 13");
25+
browserOptions.setBrowserVersion("latest");
26+
browserOptions.setCapability("sauce:options", sauceOptions());
27+
28+
return browserOptions;
29+
}
30+
31+
private Map<String, Object> sauceOptions() {
32+
Map<String, Object> sauceOptionsMap = new HashMap<>();
33+
sauceOptionsMap.put("username", System.getenv("SAUCE_USERNAME"));
34+
sauceOptionsMap.put("accessKey", System.getenv("SAUCE_ACCESS_KEY"));
35+
36+
return sauceOptionsMap;
37+
}
38+
}

src/test/java/todomvc/acceptancetests/CrossBrowserAndParallelTesting.java renamed to src/test/java/todomvc/acceptancetests/crossbrowsertests/LocalCrossBrowserAndParallelTesting.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
package todomvc.acceptancetests;
1+
package todomvc.acceptancetests.crossbrowsertests;
22

33
import org.assertj.core.api.Assertions;
44
import org.junit.jupiter.api.AfterEach;
55
import org.junit.jupiter.api.BeforeEach;
6-
import org.junit.jupiter.api.extension.ExtendWith;
76
import org.junit.jupiter.api.parallel.Execution;
87
import org.junit.jupiter.api.parallel.ExecutionMode;
98
import org.junit.jupiter.params.ParameterizedTest;
109
import org.junit.jupiter.params.provider.MethodSource;
1110
import org.openqa.selenium.WebDriver;
11+
import todomvc.acceptancetests.BaseTest;
1212
import todomvc.pageactions.ToDoPage;
1313

1414
//The individual Test Methods is parameterized to run on different driver types, sequentially
1515
//The JUnit concurrency execution mode annotation powers the iterations to run in parallel as opposed to sequential
1616
@Execution(ExecutionMode.CONCURRENT)
17-
public class CrossBrowserAndParallelTesting extends BaseTest{
17+
public class LocalCrossBrowserAndParallelTesting extends BaseTest {
1818
ToDoPage toDoPage;
1919

2020
@Override
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package todomvc.acceptancetests.crossbrowsertests;
2+
3+
import org.assertj.core.api.Assertions;
4+
import org.junit.jupiter.api.AfterEach;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.openqa.selenium.remote.RemoteWebDriver;
8+
import todomvc.acceptancetests.BaseTest;
9+
import todomvc.pageactions.ToDoPage;
10+
11+
import java.net.MalformedURLException;
12+
13+
//The individual Test Method can be parameterized to run on different driver types, sequentially
14+
//The JUnit concurrency execution mode annotation can power the iterations to run in parallel as opposed to sequential
15+
//@Execution(ExecutionMode.CONCURRENT)
16+
public class SauceLabsCrossBrowserTesting extends BaseTest {
17+
RemoteWebDriver driver;
18+
ToDoPage toDoPage;
19+
20+
@Override
21+
@BeforeEach
22+
public void setUp(){
23+
//Do nothing for now. Cross browser testing is powered by ParameterizedTest directly.
24+
}
25+
26+
@Override
27+
@AfterEach
28+
public void tearDown(){
29+
driver.quit();
30+
}
31+
32+
@Test
33+
public void addingASingleTask() throws MalformedURLException {
34+
driver = remoteDriverForSauceLabsOn("Chrome");
35+
toDoPage = new ToDoPage(driver);
36+
// Add "Feed The Cat" to the list
37+
// Check that "Feed The Cat" appears in the list
38+
39+
toDoPage.openToDoApp();
40+
toDoPage.addItemCalled("Feed the cat");
41+
42+
Assertions.assertThat(toDoPage.listOfTextsOfToDoItems()).contains("Feed the cat");
43+
44+
driver.quit();
45+
}
46+
47+
@Test
48+
public void addingMultipleTasks() throws MalformedURLException {
49+
driver = remoteDriverForSauceLabsOn("Firefox");
50+
toDoPage = new ToDoPage(driver);
51+
52+
// Add "Feed The Cat" and "Walk the dog" to the list
53+
// Check that they all appear in the list
54+
toDoPage.openToDoApp();
55+
toDoPage.addItemsCalled("Feed the cat", "Walk the dog");
56+
57+
Assertions.assertThat(toDoPage.listOfTextsOfToDoItems())
58+
.containsExactly("Feed the cat", "Walk the dog");
59+
}
60+
}

0 commit comments

Comments
 (0)