This repository has been archived by the owner on Dec 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
BaseWebDriverTest.java
165 lines (145 loc) · 7.08 KB
/
BaseWebDriverTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.saucelabs.saucedemo.tests;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Simple TestNG test which demonstrates being instantiated via a DataProvider in order to supply multiple browser combinations.
*/
public class BaseWebDriverTest {
private RunType runType;
// ThreadLocal variable containing WebDriver instance and the Sauce Job Id
private ThreadLocal<WebDriver> webDriver = new ThreadLocal<>();
private ThreadLocal<String> sessionId = new ThreadLocal<>();
public enum RunType { LOCAL, SAUCE }
/**
* @return the {@link WebDriver} for the current thread
*/
protected WebDriver getWebDriver() {
return webDriver.get();
}
private void createSauceDriver(MutableCapabilities capabilities, String methodName) {
String username = System.getenv("SAUCE_USERNAME");
String accesskey = System.getenv("SAUCE_ACCESS_KEY");
//Create a map of capabilities called "sauce:options", which contain the info necessary to run on Sauce
// Labs, using the credentials stored in the environment variables. Also runs using the new W3C standard.
MutableCapabilities sauceOptions = new MutableCapabilities();
sauceOptions.setCapability("username", username);
sauceOptions.setCapability("accessKey", accesskey);
sauceOptions.setCapability("seleniumVersion", "3.141.59");
sauceOptions.setCapability("name", methodName);
sauceOptions.setCapability("build", "saucecon19-parallel-testing-workshop");
//Assign the Sauce Options to the base capabilities
capabilities.setCapability("sauce:options", sauceOptions);
//Create a new RemoteWebDriver, which will initialize the test execution on Sauce Labs servers
String SAUCE_REMOTE_URL = "https://ondemand.saucelabs.com/wd/hub";
try {
webDriver.set(new RemoteWebDriver(new URL(SAUCE_REMOTE_URL), capabilities));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
sessionId.set(((RemoteWebDriver)webDriver.get()).getSessionId().toString());
// set current sessionId
String id = ((RemoteWebDriver) getWebDriver()).getSessionId().toString();
sessionId.set(id);
}
private void createLocalDriver(MutableCapabilities capabilities) {
webDriver.set(new ChromeDriver((ChromeOptions) capabilities));
}
/**
* DataProvider that sets the browser combinations to be used.
*
* @param beforeMethod
* @return TestNG's preferred Object[][] structure, containing browser, version, and platform information
*/
@DataProvider(name = "sauceBrowsers", parallel = false)
public static Object[][] sauceBrowserDataProvider(Method beforeMethod) {
RunType runType = RunType.SAUCE;
return new Object[][]{
new Object[]{"chrome", "73.0", "macOS 10.14", runType},
new Object[]{"chrome", "72.0", "Windows 10", runType},
new Object[]{"chrome", "71.0", "Windows 7", runType},
new Object[]{"chrome", "70.0", "macOS 10.14", runType},
new Object[]{"chrome", "70.0", "macOS 10.14", runType},
new Object[]{"chrome", "71.0", "Windows 10", runType},
new Object[]{"chrome", "72.0", "Windows 7", runType},
new Object[]{"chrome", "73.0", "macOS 10.14", runType},
new Object[]{"chrome", "72.0", "macOS 10.14", runType},
new Object[]{"chrome", "73.0", "Windows 10", runType},
new Object[]{"chrome", "70.0", "Windows 7", runType},
new Object[]{"chrome", "71.0", "macOS 10.14", runType},
new Object[]{"firefox", "66.0", "Windows 7", runType},
new Object[]{"firefox", "65.0", "Windows 10", runType},
new Object[]{"firefox", "64.0", "macOS 10.14", runType},
new Object[]{"firefox", "63.0", "macOS 10.13", runType},
new Object[]{"firefox", "62.0", "macOS 10.12", runType},
new Object[]{"firefox", "61.0", "macOS 10.13", runType},
};
}
@DataProvider(name = "localBrowsers")
public static Object[][] localBrowserDataProvider(Method beforeMethod) {
RunType runType = RunType.LOCAL;
return new Object[][]{
new Object[]{"chrome", "latest-1", "Windows 10", runType},
};
}
/**
* Constructs a new {@link RemoteWebDriver} instance which is configured to use the capabilities defined by the browser,
* version and os parameters, and which is configured to run against ondemand.saucelabs.com, using
* the username and access key populated by the {@link #authentication} instance.
*
* @param browser Represents the browser to be used as part of the test run.
* @param browserVersion Represents the version of the browser to be used as part of the test run.
* @param platformName Represents the operating system to be used as part of the test run.
* @param methodName Represents the name of the test case that will be used to identify the test on Sauce.
*/
protected void createDriver(String browser, String browserVersion, String platformName, String methodName, RunType runType) {
this.runType = runType;
//Set up the ChromeOptions object, which will store the capabilities
MutableCapabilities capabilities = new MutableCapabilities();
if (browser.equals("chrome")) {
ChromeOptions caps = new ChromeOptions();
caps.setExperimentalOption("w3c", true);
capabilities = caps;
}
else if (browser.equals("firefox")) {
capabilities = new FirefoxOptions();
}
capabilities.setCapability("browserVersion", browserVersion);
capabilities.setCapability("platformName", platformName);
switch(runType) {
case LOCAL:
createLocalDriver(capabilities);
break;
case SAUCE:
createSauceDriver(capabilities, methodName);
break;
}
}
//Method that gets invoked after test
@AfterMethod
public void tearDown(ITestResult result) {
try {
if (runType.equals(RunType.SAUCE)) {
((JavascriptExecutor) webDriver.get()).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));
}
}
finally {
webDriver.get().quit();
}
}
void annotate(String text) {
if (runType.equals(RunType.SAUCE)) {
((JavascriptExecutor) webDriver.get()).executeScript("sauce:context=" + text);
}
}
}