Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.

Commit 415e5fa

Browse files
Merge pull request #99 from saucelabs/fix-hack
Fix hack
2 parents f1e88de + 31e5db8 commit 415e5fa

File tree

7 files changed

+209
-47
lines changed

7 files changed

+209
-47
lines changed

common/src/main/java/com/saucelabs/remotedriver/SauceSession.java

Lines changed: 101 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.saucelabs.remotedriver;
22

3+
import com.saucelabs.common.SauceApi;
4+
import com.saucelabs.saucerest.SauceREST;
35
import org.openqa.selenium.MutableCapabilities;
46
import org.openqa.selenium.WebDriver;
57
import org.openqa.selenium.chrome.ChromeOptions;
68
import org.openqa.selenium.edge.EdgeOptions;
79
import org.openqa.selenium.firefox.FirefoxOptions;
810
import org.openqa.selenium.ie.InternetExplorerOptions;
911
import org.openqa.selenium.remote.CapabilityType;
12+
import org.openqa.selenium.remote.RemoteWebDriver;
1013
import org.openqa.selenium.safari.SafariOptions;
1114

1215
import java.net.MalformedURLException;
@@ -17,15 +20,17 @@ public class SauceSession {
1720
static String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");
1821
static String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
1922

20-
//todo there is some weird bug when this is set to Linux, the session can't be started
23+
String BUILD_TAG = System.getenv("BUILD_TAG");
24+
25+
public SauceApi test;
26+
27+
//todo there is some weird bug when this is set to Linux, the session can't be started
2128
String operatingSystem = "Windows 10";
2229
String browserName = "Chrome";
2330
String testName;
2431
Boolean useSauce = true;
2532
String sauceOptionsTag = "sauce:options";
2633

27-
public String seleniumServer;
28-
2934
ChromeOptions chromeOptions;
3035
FirefoxOptions firefoxOptions;
3136
MutableCapabilities sauceOptions;
@@ -38,7 +43,10 @@ public class SauceSession {
3843
private EdgeOptions edgeOptions;
3944
private InternetExplorerOptions ieOptions;
4045

41-
public SauceSession(){
46+
private String sessionId;
47+
private SauceREST api;
48+
49+
public SauceSession() {
4250
capabilities = new MutableCapabilities();
4351
remoteDriverManager = new ConcreteRemoteDriverManager();
4452
}
@@ -50,18 +58,14 @@ public SauceSession(RemoteDriverInterface remoteManager) {
5058

5159
public SauceSession start() throws MalformedURLException
5260
{
53-
seleniumServer = getSeleniumServer();
5461
capabilities = getCapabilities();
55-
webDriver = remoteDriverManager.getRemoteWebDriver(seleniumServer, capabilities);
56-
return this;
62+
webDriver = remoteDriverManager.getRemoteWebDriver(sauceSeleniumServer, capabilities);
63+
sessionId = ((RemoteWebDriver) webDriver).getSessionId().toString();
64+
test = new SauceApi(webDriver);
65+
api = new SauceREST(SAUCE_USERNAME, SAUCE_ACCESS_KEY);
66+
67+
return this;
5768
}
58-
public String getSeleniumServer() {
59-
if (seleniumServer == null)
60-
{
61-
seleniumServer = sauceSeleniumServer;
62-
}
63-
return seleniumServer;
64-
}
6569
public MutableCapabilities getCapabilities() {
6670
sauceOptions = getSauceOptions();
6771
setBrowserOptions(browserName);
@@ -78,17 +82,19 @@ public MutableCapabilities getSauceOptions()
7882
{
7983
if (useSauce)
8084
{
81-
seleniumServer = sauceSeleniumServer;
82-
8385
sauceOptions = new MutableCapabilities();
8486
sauceOptions.setCapability("username", SAUCE_USERNAME);
8587
sauceOptions.setCapability("accessKey", SAUCE_ACCESS_KEY);
86-
sauceOptions.setCapability("seleniumVersion", "3.141.59");
8788

8889
if (testName != null)
8990
{
9091
sauceOptions.setCapability("name", testName);
9192
}
93+
94+
if (BUILD_TAG != null)
95+
{
96+
sauceOptions.setCapability("build", BUILD_TAG);
97+
}
9298
}
9399

94100
return sauceOptions;
@@ -98,27 +104,23 @@ public void setBrowserOptions(String browserName)
98104
{
99105
if (browserName.equalsIgnoreCase("Chrome"))
100106
{
101-
withChrome();
102107
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
103108
}
104109
else if (browserName.equalsIgnoreCase("Firefox"))
105110
{
106-
withFirefox();
107111
capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, firefoxOptions);
108112
}
109113
else if(browserName.equalsIgnoreCase("Safari"))
110114
{
111-
withSafari();
115+
safariOptions = new SafariOptions();
112116
capabilities.setCapability(SafariOptions.CAPABILITY, safariOptions);
113117
}
114118
else if(browserName.equalsIgnoreCase("Edge"))
115119
{
116-
withEdge();
117120
capabilities.setCapability("Edge", edgeOptions);
118121
}
119122
else if(browserName.equalsIgnoreCase("IE"))
120123
{
121-
withIE();
122124
capabilities.setCapability("se:ieOptions", ieOptions);
123125
}
124126
else {
@@ -159,13 +161,18 @@ public WebDriver getDriver() {
159161
}
160162

161163
//TODO How do we want to handle this?
162-
//1. withSafari(OperatingSystem.MacOs1014), aka, force the user to pass in a mac version
163-
//2. throw an exception for withSafari() used without withMac();
164-
//3. this is the method I chose below: withSafari(String browserVersion)
165-
public SauceSession withSafari() {
164+
//1. withMacOsMojave(OperatingSystem.MacOs1014), aka, force the user to pass in a mac version
165+
//2. throw an exception for withMacOsMojave() used without withMac();
166+
//3. this is the method I chose below: withMacOsMojave(String browserVersion)
167+
public SauceSession withMacOsMojave() {
166168
operatingSystem = "macOS 10.14";
167169
browserName = "Safari";
168-
safariOptions = new SafariOptions();
170+
return this;
171+
}
172+
public SauceSession withMacOsHighSierra()
173+
{
174+
this.operatingSystem = "macOS 10.13";
175+
browserName = "Safari";
169176
return this;
170177
}
171178
public SauceSession withBrowserVersion(String browserVersion){
@@ -189,4 +196,71 @@ public SauceSession withPlatform(String operatingSystem) {
189196
this.operatingSystem = operatingSystem;
190197
return this;
191198
}
199+
200+
201+
public SauceSession withBrowser(String browserName, String browserVersion)
202+
{
203+
this.browserName = browserName;
204+
this.browserVersion = browserVersion;
205+
setBrowserOptions(browserName);
206+
207+
return this;
208+
}
209+
210+
public SauceSession withBrowser(String browserName)
211+
{
212+
this.browserName = browserName;
213+
setBrowserOptions(browserName);
214+
215+
return this;
216+
}
217+
218+
public SauceSession withTestName(String testName)
219+
{
220+
this.testName = testName;
221+
return this;
222+
}
223+
224+
public SauceSession withBuildName(String buildName)
225+
{
226+
this.testName = testName;
227+
return this;
228+
}
229+
230+
public void stop()
231+
{
232+
if(webDriver != null)
233+
webDriver.quit();
234+
}
235+
236+
public void passed()
237+
{
238+
if (webDriver != null) { test.setTestStatus("passed"); }
239+
else {
240+
api.jobPassed(sessionId);
241+
}
242+
}
243+
244+
public void failed()
245+
{
246+
if (webDriver != null) { test.setTestStatus("failed"); }
247+
else {
248+
api.jobFailed(sessionId);
249+
}
250+
}
251+
252+
public void setTestName(String testName)
253+
{
254+
this.testName = testName;
255+
}
256+
257+
public void setBuildTag(String buildTag)
258+
{
259+
this.BUILD_TAG = buildTag;
260+
}
261+
262+
public void setBuild(String build)
263+
{
264+
test.setBuildName(build);
265+
}
192266
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.saucelabs.remotedriver.config;
2+
3+
import org.openqa.selenium.chrome.ChromeOptions;
4+
5+
public class LinuxConfig extends SauceConfig
6+
{
7+
public LinuxConfig withChrome()
8+
{
9+
chromeOptions = new ChromeOptions();
10+
chromeOptions.setExperimentalOption("w3c", true);
11+
browserName = "Chrome";
12+
13+
return this;
14+
}
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.saucelabs.remotedriver.config;
2+
3+
import org.openqa.selenium.chrome.ChromeOptions;
4+
5+
public class SauceConfig
6+
{
7+
public String username;
8+
public String accessKey;
9+
10+
public class testConfig
11+
{
12+
public String name;
13+
public String build;
14+
public String tags;
15+
public String customData;
16+
}
17+
18+
public class OSConfig
19+
{
20+
public String browserName;
21+
}
22+
23+
public ChromeOptions chromeOptions;
24+
public String browserName;
25+
26+
public static LinuxConfig withLinux()
27+
{
28+
return new LinuxConfig();
29+
}
30+
}

common/src/test/java/com/saucelabs/common/acceptance/SauceSessionAcceptaceTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.saucelabs.common.acceptance;
22

3+
import com.saucelabs.remotedriver.SafariVersion;
34
import com.saucelabs.remotedriver.SauceSession;
45
import org.hamcrest.text.IsEqualIgnoringCase;
56
import org.junit.After;
@@ -19,7 +20,8 @@ public class SauceSessionAcceptaceTest {
1920
@After
2021
public void cleanUp()
2122
{
22-
webDriver.quit();
23+
if(webDriver != null)
24+
webDriver.quit();
2325
}
2426
@Test
2527
public void startSession_noSauceOptionsSet_returnsDriver() throws MalformedURLException {
@@ -36,10 +38,33 @@ public void getInstance_nonDefaultCapabilities_returnsCorrectDriver() throws Mal
3638
}
3739
@Test
3840
public void withSafari_osNotSet_returnsValidSafariSession() throws MalformedURLException {
39-
webDriver = new SauceSession().withSafari().start().getDriver();
41+
webDriver = new SauceSession().withMacOsMojave().start().getDriver();
4042
String actualBrowser = getBrowserNameFromCapabilities();
4143
assertThat(actualBrowser, IsEqualIgnoringCase.equalToIgnoringCase("safari"));
4244
}
45+
@Test
46+
public void newSession_default_canUseSauceApi() throws MalformedURLException {
47+
SauceSession sauceLabs = new SauceSession().start();
48+
49+
sauceLabs.test.comment("sample test comment");
50+
sauceLabs.test.setTestStatus("true");
51+
sauceLabs.test.setTestName("MyTestName");
52+
53+
sauceLabs.stop();
54+
}
55+
@Test
56+
public void withSafari_differentVersion_returnsValidSession() throws MalformedURLException {
57+
webDriver = new SauceSession().
58+
withBrowserVersion(SafariVersion.elevenDotOne).
59+
withMacOsHighSierra().
60+
start().
61+
getDriver();
62+
63+
String actualBrowser = getBrowserNameFromCapabilities();
64+
String actualBrowserVersion = (((RemoteWebDriver) webDriver).getCapabilities()).getVersion();
65+
assertThat(actualBrowser, IsEqualIgnoringCase.equalToIgnoringCase("safari"));
66+
assertThat(actualBrowserVersion, IsEqualIgnoringCase.equalToIgnoringCase("13605.3.8"));
67+
}
4368

4469
private String getBrowserNameFromCapabilities() {
4570
return (((RemoteWebDriver) webDriver).getCapabilities()).getBrowserName();

common/src/test/java/com/saucelabs/remotedriver/SauceSessionTest.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ public void setUp()
2525
sauceSession = new SauceSession();
2626
}
2727
@Test
28-
public void seleniumServer_notSet_returnsNull()
28+
public void startSession_invoked_setsNewSauceApiObject() throws MalformedURLException
2929
{
30-
//TODO is this okay to be like a property,
31-
//or should it be a getSeleniumServer()
32-
assertNull(sauceSession.seleniumServer);
30+
RemoteDriverInterface fakeRemoteDriver = mock(RemoteDriverInterface.class);
31+
sauceSession = new SauceSession(fakeRemoteDriver);
32+
sauceSession.start();
33+
assertNotNull(sauceSession.test);
34+
}
35+
@Test
36+
public void withChrome_invoked_setsBrowserNameToChrome() throws MalformedURLException
37+
{
38+
sauceSession = new SauceSession().withChrome();
39+
assertThat(sauceSession.browserName,
40+
IsEqualIgnoringCase.equalToIgnoringCase("chrome"));
3341
}
3442

3543
@Test
@@ -39,16 +47,7 @@ public void defaultConstructor_instantiated_setsConcreteDriverManager()
3947
}
4048

4149
@Test
42-
public void getInstance_serverNotSet_setsSauceSeleniumServer() throws MalformedURLException {
43-
RemoteDriverInterface fakeRemoteDriver = mock(RemoteDriverInterface.class);
44-
sauceSession = new SauceSession(fakeRemoteDriver);
45-
sauceSession.start();
46-
String expectedServer = "https://ondemand.saucelabs.com/wd/hub";
47-
assertEquals(expectedServer, sauceSession.seleniumServer);
48-
}
49-
@Test
50-
//TODO rename and refactor into logic similar to here: setCapability_platformName_returnsCorrectOs
51-
public void browserNameCapability_isSetToCorrectKey() throws MalformedURLException {
50+
public void startSession_browserCapabilityKey_isCorrect() throws MalformedURLException {
5251
RemoteDriverInterface fakeRemoteDriver = mock(RemoteDriverInterface.class);
5352
sauceSession = new SauceSession(fakeRemoteDriver);
5453
sauceSession.start();
@@ -116,7 +115,7 @@ public void defaultSafari_notSet_returnsLatestVersion()
116115
{
117116
RemoteDriverInterface fakeRemoteDriver = mock(RemoteDriverInterface.class);
118117
sauceSession = new SauceSession(fakeRemoteDriver);
119-
sauceSession.withSafari();
118+
sauceSession.withMacOsMojave();
120119

121120
String safariVersion = sauceSession.getCapabilities().getVersion();
122121

@@ -125,14 +124,14 @@ public void defaultSafari_notSet_returnsLatestVersion()
125124
@Test
126125
public void withSafari_browserName_setToSafari()
127126
{
128-
sauceSession.withSafari();
127+
sauceSession.withMacOsMojave();
129128
String actualBrowserName = sauceSession.getCapabilities().getBrowserName();
130129
assertThat(actualBrowserName, IsEqualIgnoringCase.equalToIgnoringCase("safari"));
131130
}
132131
@Test
133132
public void withSafari_versionChangedFromDefault_returnsCorrectVersion()
134133
{
135-
sauceSession.withSafari().withBrowserVersion(SafariVersion.elevenDotOne);
134+
sauceSession.withMacOsMojave().withBrowserVersion(SafariVersion.elevenDotOne);
136135
String safariVersion = sauceSession.getCapabilities().getVersion();
137136
assertThat(safariVersion, IsEqualIgnoringCase.equalToIgnoringCase("11.1"));
138137
}
@@ -178,6 +177,6 @@ public void withOs_mac_allowsOnlyChromeOrFfOrSafari()
178177
public void withSafari_versionChangedToInvalid_shouldNotBePossible()
179178
{
180179
//TODO it should not be possible to set an invalid version
181-
sauceSession.withSafari().withBrowserVersion("1234");
180+
sauceSession.withMacOsMojave().withBrowserVersion("1234");
182181
}
183182
}

0 commit comments

Comments
 (0)