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

Commit 92e334f

Browse files
add tests for working with safari
1 parent 6080f4e commit 92e334f

File tree

4 files changed

+57
-24
lines changed

4 files changed

+57
-24
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.saucelabs.remotedriver;
2+
3+
public class SafariVersion {
4+
5+
public static String elevenDotOne = "11.1";
6+
}

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.openqa.selenium.chrome.ChromeOptions;
66
import org.openqa.selenium.firefox.FirefoxOptions;
77
import org.openqa.selenium.remote.RemoteWebDriver;
8+
import org.openqa.selenium.safari.SafariOptions;
89

910
import java.net.MalformedURLException;
1011
import java.net.URL;
@@ -16,7 +17,7 @@ public class SauceSession {
1617
static String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
1718

1819
//todo there is some weird bug when this is set to Linux, the session can't be started
19-
String platformName = "Windows 10";
20+
String operatingSystem = "Windows 10";
2021
String browserName = "Chrome";
2122
String testName;
2223
Boolean useSauce = true;
@@ -33,6 +34,7 @@ public class SauceSession {
3334
private RemoteDriverInterface remoteDriverManager;
3435
private MutableCapabilities browserOptions;
3536
private WebDriver webDriver;
37+
private SafariOptions safariOptions;
3638

3739
public SauceSession(){
3840
capabilities = new MutableCapabilities();
@@ -44,7 +46,7 @@ public SauceSession(RemoteDriverInterface remoteManager) {
4446
capabilities = new MutableCapabilities();
4547
}
4648

47-
public RemoteWebDriver getInstance() throws MalformedURLException
49+
public RemoteWebDriver getWebDriver() throws MalformedURLException
4850
{
4951
seleniumServer = getSeleniumServer();
5052
capabilities = getCapabilities();
@@ -105,7 +107,7 @@ public SauceSession withFirefox()
105107

106108
public SauceSession withPlatform(String platformName)
107109
{
108-
this.platformName = platformName;
110+
this.operatingSystem = platformName;
109111

110112
return this;
111113
}
@@ -127,7 +129,7 @@ public MutableCapabilities getCapabilities() {
127129

128130
capabilities.setCapability(sauceOptionsTag, sauceOptions);
129131
capabilities.setCapability("browserName", browserName);
130-
capabilities.setCapability("platformName", platformName);
132+
capabilities.setCapability("operatingSystem", operatingSystem);
131133
capabilities.setCapability("browserVersion", browserVersion);
132134

133135
return capabilities;
@@ -179,11 +181,23 @@ public MutableCapabilities getSauceOptionsCapability(){
179181
}
180182

181183
public SauceSession start() throws MalformedURLException {
182-
webDriver = getInstance();
184+
webDriver = getWebDriver();
183185
return this;
184186
}
185187

186188
public WebDriver getDriver() {
187189
return webDriver;
188190
}
191+
192+
//TODO How do we want to handle this?
193+
//1. withSafari(OperatingSystem.MacOs1014), aka, force the user to pass in a mac version
194+
//2. throw an exception for withSafari() used without withMac();
195+
//3. this is the method I chose below
196+
public SauceSession withSafari(String browserVersion) {
197+
operatingSystem = "macOS 10.14";
198+
browserName = "Safari";
199+
this.browserVersion = browserVersion;
200+
safariOptions = new SafariOptions();
201+
return this;
202+
}
189203
}

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

Lines changed: 8 additions & 1 deletion
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;
@@ -29,8 +30,14 @@ public void startSession_noSauceOptionsSet_returnsDriver() throws MalformedURLEx
2930

3031
@Test
3132
public void getInstance_nonDefaultCapabilities_returnsCorrectDriver() throws MalformedURLException {
32-
webDriver = new SauceSession().withFirefox().getInstance();
33+
webDriver = new SauceSession().withFirefox().getWebDriver();
3334
String actualBrowser = (((RemoteWebDriver) webDriver).getCapabilities()).getBrowserName();
3435
assertThat(actualBrowser, IsEqualIgnoringCase.equalToIgnoringCase("firefox"));
3536
}
37+
@Test
38+
public void safari_osNotSet_returnsValidSafariSession() throws MalformedURLException {
39+
webDriver = new SauceSession().withSafari(SafariVersion.elevenDotOne).getWebDriver();
40+
String actualBrowser = (((RemoteWebDriver) webDriver).getCapabilities()).getBrowserName();
41+
assertThat(actualBrowser, IsEqualIgnoringCase.equalToIgnoringCase("safari"));
42+
}
3643
}

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

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
import org.hamcrest.text.IsEqualIgnoringCase;
44
import org.junit.Before;
5-
import org.junit.Ignore;
65
import org.junit.Test;
7-
import org.openqa.selenium.Capabilities;
86
import org.openqa.selenium.MutableCapabilities;
9-
import org.openqa.selenium.WebDriver;
10-
import org.openqa.selenium.remote.RemoteWebDriver;
117

128
import java.net.MalformedURLException;
139

1410
import static org.hamcrest.CoreMatchers.instanceOf;
1511
import static org.junit.Assert.*;
16-
import static org.mockito.Mockito.*;
12+
import static org.mockito.Mockito.mock;
1713

1814
public class SauceSessionTest {
1915

@@ -40,8 +36,8 @@ public void defaultConstructor_instantiated_setsConcreteDriverManager()
4036

4137
@Test
4238
public void getInstance_serverNotSet_setsSauceSeleniumServer() throws MalformedURLException {
43-
RemoteDriverInterface stubRemoteManager = mock(RemoteDriverInterface.class);
44-
sauceSession = new SauceSession(stubRemoteManager);
39+
RemoteDriverInterface fakeRemoteDriver = mock(RemoteDriverInterface.class);
40+
sauceSession = new SauceSession(fakeRemoteDriver);
4541
sauceSession.getInstanceOld();
4642
String expectedServer = "https://ondemand.saucelabs.com/wd/hub";
4743
assertEquals(expectedServer, sauceSession.seleniumServer);
@@ -73,18 +69,28 @@ public void sauceOptions_defaultConfiguration_setsSauceOptions()
7369
}
7470

7571
@Test
76-
@Ignore("Having trouble testing. How do we actually validate that what came back is" +
77-
"what we want. Or is checking the set properties enough")
78-
public void getInstance_default_returnsChromeBad() throws MalformedURLException {
79-
RemoteDriverInterface stubRemoteManager = mock(RemoteDriverInterface.class);
80-
Capabilities stubCaps = mock(Capabilities.class);
81-
RemoteWebDriver remoteWebDriver = new RemoteWebDriver(stubCaps);
82-
when(stubRemoteManager.getRemoteWebDriver(anyString(),anyObject())).thenReturn(remoteWebDriver);
83-
sauceSession = new SauceSession(stubRemoteManager);
72+
public void defaultSafari_notSet_returnsLatestVersion()
73+
{
74+
RemoteDriverInterface fakeRemoteDriver = mock(RemoteDriverInterface.class);
75+
sauceSession = new SauceSession(fakeRemoteDriver);
76+
sauceSession.withSafari(SafariVersion.elevenDotOne);
8477

85-
WebDriver driver = sauceSession.getInstance();
78+
String safariVersion = sauceSession.getCapabilities().getVersion();
8679

87-
String actualBrowser = (((RemoteWebDriver) driver).getCapabilities()).getBrowserName();
88-
assertThat(actualBrowser, IsEqualIgnoringCase.equalToIgnoringCase("chrome"));
80+
assertThat(safariVersion, IsEqualIgnoringCase.equalToIgnoringCase("latest"));
81+
}
82+
@Test
83+
public void withSafari_browserName_setToSafari()
84+
{
85+
sauceSession.withSafari(SafariVersion.elevenDotOne);
86+
String actualBrowserName = sauceSession.getCapabilities().getBrowserName();
87+
assertThat(actualBrowserName, IsEqualIgnoringCase.equalToIgnoringCase("safari"));
88+
}
89+
@Test
90+
public void safariVersion_changed_returnsCorrespondingVersion()
91+
{
92+
sauceSession.withSafari(SafariVersion.elevenDotOne);
93+
String safariVersion = sauceSession.getCapabilities().getVersion();
94+
assertThat(safariVersion, IsEqualIgnoringCase.equalToIgnoringCase("11.1"));
8995
}
9096
}

0 commit comments

Comments
 (0)