Skip to content

Commit 4bf7e90

Browse files
committed
possible idea - single SauceSession class, multiple SauceOption classes
1 parent fa3b39b commit 4bf7e90

15 files changed

+197
-168
lines changed

java/src/main/java/com/saucelabs/saucebindings/Browser.java

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Set;
88

99
public enum Browser {
10+
NONE(""),
1011
CHROME("chrome"),
1112
INTERNET_EXPLORER("internet explorer"),
1213
EDGE("MicrosoftEdge"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.saucelabs.saucebindings;
2+
3+
import lombok.Getter;
4+
5+
public enum DeviceOrientation {
6+
PORTRAIT("portrait"),
7+
LANDSCAPE("landscape");
8+
9+
@Getter private final String value;
10+
11+
DeviceOrientation(String value) {
12+
this.value = value;
13+
}
14+
15+
public String toString() {
16+
return value;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.saucelabs.saucebindings;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.experimental.Accessors;
6+
import org.openqa.selenium.MutableCapabilities;
7+
8+
@Accessors(chain = true)
9+
@Setter @Getter
10+
public class SauceAndroidOptions extends SauceOptions {
11+
12+
// minimal configuration
13+
private DeviceOrientation deviceOrientation;
14+
private String deviceName;
15+
private String platformVersion;
16+
17+
public SauceAndroidOptions(String deviceName, String platformVersion, DeviceOrientation deviceOrientation){
18+
super();
19+
this.platformName = SaucePlatform.ANDROID;
20+
this.browserName = Browser.NONE;
21+
this.browserVersion = "";
22+
this.deviceName = deviceName;
23+
this.platformVersion = platformVersion;
24+
this.deviceOrientation = deviceOrientation;
25+
}
26+
27+
public SauceAndroidOptions(MutableCapabilities capabilities){
28+
super(capabilities);
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.saucelabs.saucebindings;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.experimental.Accessors;
6+
import org.openqa.selenium.MutableCapabilities;
7+
8+
@Accessors(chain = true)
9+
@Setter @Getter
10+
public class SauceIOSOptions extends SauceOptions {
11+
12+
// minimal configuration
13+
private DeviceOrientation deviceOrientation;
14+
private String deviceName;
15+
private String platformVersion;
16+
17+
public SauceIOSOptions(String deviceName, String platformVersion, DeviceOrientation deviceOrientation){
18+
super();
19+
this.platformName = SaucePlatform.IOS;
20+
this.browserName = Browser.NONE;
21+
this.platformVersion = "";
22+
this.deviceName = deviceName;
23+
this.platformVersion = platformVersion;
24+
this.deviceOrientation = deviceOrientation;
25+
}
26+
27+
public SauceIOSOptions(MutableCapabilities capabilities){
28+
super(capabilities);
29+
}
30+
}

java/src/main/java/com/saucelabs/saucebindings/SauceMobileSession.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public AppiumDriver start() {
3333
url = getSauceUrl(username, key);
3434
}
3535

36-
driver = createAppiumDriver(url, capabilities);
36+
//driver = createRemoteWebDriver(url, capabilities);
3737
return driver;
3838
}
3939

java/src/main/java/com/saucelabs/saucebindings/SauceOptions.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ public class SauceOptions {
2525
@Setter(AccessLevel.NONE) private MutableCapabilities seleniumCapabilities;
2626
public TimeoutStore timeout = new TimeoutStore();
2727

28-
// w3c Settings
29-
private Browser browserName = Browser.CHROME;
30-
private String browserVersion = "latest";
31-
private SaucePlatform platformName = SaucePlatform.WINDOWS_10;
28+
// w3c settings - applies to any sauce session
29+
protected Browser browserName = Browser.CHROME;
30+
protected SaucePlatform platformName = SaucePlatform.WINDOWS_10;
31+
protected String browserVersion = "latest";
32+
33+
// w3c settings - applies only to VDC
3234
private PageLoadStrategy pageLoadStrategy;
3335
private Boolean acceptInsecureCerts = null;
3436
private Proxy proxy;
@@ -164,7 +166,7 @@ public Map<Timeouts, Integer> getTimeouts() {
164166
return timeout.getTimeouts();
165167
}
166168

167-
private SauceOptions(MutableCapabilities options) {
169+
protected SauceOptions(MutableCapabilities options) {
168170
seleniumCapabilities = new MutableCapabilities(options.asMap());
169171
if (options.getCapability("browserName") != null) {
170172
setCapability("browserName", options.getCapability("browserName"));

java/src/main/java/com/saucelabs/saucebindings/SaucePlatform.java

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
public enum SaucePlatform {
1010
ANDROID("Android"),
11+
IOS("iOS"),
1112
WINDOWS_10("Windows 10"),
1213
WINDOWS_8_1("Windows 8.1"),
1314
WINDOWS_8("Windows 8"),

java/src/main/java/com/saucelabs/saucebindings/SauceSession.java

+43-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.saucelabs.saucebindings;
22

3+
import io.appium.java_client.AppiumDriver;
4+
import io.appium.java_client.android.AndroidDriver;
5+
import io.appium.java_client.ios.IOSDriver;
36
import lombok.Getter;
47
import lombok.Setter;
58
import org.openqa.selenium.InvalidArgumentException;
@@ -11,22 +14,37 @@
1114

1215
public class SauceSession {
1316
@Getter @Setter protected DataCenter dataCenter = DataCenter.US_LEGACY;
14-
@Getter private final SauceOptions sauceOptions;
17+
@Getter private SauceOptions sauceOptions;
1518
@Setter private URL sauceUrl;
16-
17-
@Getter private RemoteWebDriver driver;
19+
@Getter private RemoteWebDriver webDriver;
20+
@Getter private AppiumDriver appDriver;
1821

1922
public SauceSession() {
2023
this(new SauceOptions());
2124
}
2225

2326
public SauceSession(SauceOptions options) {
24-
sauceOptions = options;
27+
this.sauceOptions = options;
2528
}
2629

2730
public RemoteWebDriver start() {
28-
driver = createRemoteWebDriver(getSauceUrl(), sauceOptions.toCapabilities());
29-
return driver;
31+
String environment = sauceOptions.toCapabilities().getCapability("platformName").toString();
32+
String browserName = sauceOptions.toCapabilities().getBrowserName();
33+
34+
if (browserName.equals("")){
35+
if (environment.toLowerCase().equals("android")){
36+
return createAndroidDriver(getSauceUrl(), sauceOptions.toCapabilities());
37+
}
38+
else if (environment.toLowerCase().equals("ios")) {
39+
return createIOSDriver(getSauceUrl(), sauceOptions.toCapabilities());
40+
}
41+
else {
42+
throw new InvalidArgumentException("Invalid Sauce Labs capabilities. Please set a browser name or set platformName as \"Android\" or \"IOS\".");
43+
}
44+
}
45+
else {
46+
return createRemoteWebDriver(getSauceUrl(), sauceOptions.toCapabilities());
47+
}
3048
}
3149

3250
public URL getSauceUrl() {
@@ -45,6 +63,14 @@ protected RemoteWebDriver createRemoteWebDriver(URL url, MutableCapabilities cap
4563
return new RemoteWebDriver(url, capabilities);
4664
}
4765

66+
protected AppiumDriver createIOSDriver(URL url, MutableCapabilities capabilities) {
67+
return new IOSDriver<>(url, capabilities);
68+
}
69+
70+
protected AppiumDriver createAndroidDriver(URL url, MutableCapabilities capabilities) {
71+
return new AndroidDriver<>(url, capabilities);
72+
}
73+
4874
public void stop(Boolean passed) {
4975
String result = passed ? "passed" : "failed";
5076
stop(result);
@@ -56,12 +82,20 @@ public void stop(String result) {
5682
}
5783

5884
private void updateResult(String result) {
59-
getDriver().executeScript("sauce:job-result=" + result);
85+
if (webDriver != null)
86+
getWebDriver().executeScript("sauce:job-result=" + result);
87+
else {
88+
System.out.println("use API for mobile case");
89+
}
90+
6091
}
6192

6293
private void stop() {
63-
if(getDriver() !=null) {
64-
getDriver().quit();
94+
if(getWebDriver() !=null) {
95+
getWebDriver().quit();
96+
}
97+
if(getAppDriver() != null){
98+
getAppDriver().quit();
6599
}
66100
}
67101
}

java/src/main/java/com/saucelabs/saucebindings/examples/BasicUsage.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static void main(String[] argv){
99

1010
sauce.start();
1111

12-
sauce.getDriver().get("https://www.saucedemo.com/");
12+
sauce.getWebDriver().get("https://www.saucedemo.com/");
1313

1414
sauce.stop(true);
1515
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.saucelabs.saucebindings;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class SauceAndroidOptionsTest {
7+
8+
@Test
9+
public void defaultConstructor() {
10+
SauceAndroidOptions options = new SauceAndroidOptions("Android Emulator", "9.0", DeviceOrientation.PORTRAIT);
11+
12+
Assert.assertEquals(SaucePlatform.ANDROID, options.getPlatformName());
13+
Assert.assertEquals("9.0", options.getPlatformVersion());
14+
Assert.assertEquals("Android Emulator", options.getDeviceName());
15+
Assert.assertEquals(Browser.NONE, options.getBrowserName());
16+
Assert.assertEquals("", options.getBrowserVersion());
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.saucelabs.saucebindings;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class SauceIOSOptionsTest {
7+
8+
@Test
9+
public void defaultConstructor() {
10+
SauceIOSOptions options = new SauceIOSOptions("iPhone X Simulator", "13.0", DeviceOrientation.PORTRAIT);
11+
12+
Assert.assertEquals(SaucePlatform.IOS, options.getPlatformName());
13+
Assert.assertEquals("13.0", options.getPlatformVersion());
14+
Assert.assertEquals("iPhone X Simulator", options.getDeviceName());
15+
Assert.assertEquals(Browser.NONE, options.getBrowserName());
16+
Assert.assertEquals("", options.getBrowserVersion());
17+
18+
}
19+
}

java/src/test/java/com/saucelabs/saucebindings/SauceMobileSessionTest.java

-72
This file was deleted.

0 commit comments

Comments
 (0)