Skip to content

Commit

Permalink
possible idea - single SauceSession class, multiple SauceOption classes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmgrant committed Apr 16, 2020
1 parent fa3b39b commit 4bf7e90
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Set;

public enum Browser {
NONE(""),
CHROME("chrome"),
INTERNET_EXPLORER("internet explorer"),
EDGE("MicrosoftEdge"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.saucelabs.saucebindings;

import lombok.Getter;

public enum DeviceOrientation {
PORTRAIT("portrait"),
LANDSCAPE("landscape");

@Getter private final String value;

DeviceOrientation(String value) {
this.value = value;
}

public String toString() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.saucelabs.saucebindings;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.openqa.selenium.MutableCapabilities;

@Accessors(chain = true)
@Setter @Getter
public class SauceAndroidOptions extends SauceOptions {

This comment has been minimized.

Copy link
@titusfortner

titusfortner Apr 16, 2020

Collaborator

So, we at the very least shouldn't need distinct Android & iOS options, once we get a release with appium/java-client@2c2141e
We want everything specific to Android/iOS handled by Appium code not Sauce code.


// minimal configuration
private DeviceOrientation deviceOrientation;
private String deviceName;
private String platformVersion;

public SauceAndroidOptions(String deviceName, String platformVersion, DeviceOrientation deviceOrientation){
super();
this.platformName = SaucePlatform.ANDROID;
this.browserName = Browser.NONE;
this.browserVersion = "";
this.deviceName = deviceName;
this.platformVersion = platformVersion;
this.deviceOrientation = deviceOrientation;
}

public SauceAndroidOptions(MutableCapabilities capabilities){
super(capabilities);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.saucelabs.saucebindings;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.openqa.selenium.MutableCapabilities;

@Accessors(chain = true)
@Setter @Getter
public class SauceIOSOptions extends SauceOptions {

// minimal configuration
private DeviceOrientation deviceOrientation;
private String deviceName;
private String platformVersion;

public SauceIOSOptions(String deviceName, String platformVersion, DeviceOrientation deviceOrientation){
super();
this.platformName = SaucePlatform.IOS;
this.browserName = Browser.NONE;
this.platformVersion = "";
this.deviceName = deviceName;
this.platformVersion = platformVersion;
this.deviceOrientation = deviceOrientation;
}

public SauceIOSOptions(MutableCapabilities capabilities){
super(capabilities);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AppiumDriver start() {
url = getSauceUrl(username, key);
}

driver = createAppiumDriver(url, capabilities);
//driver = createRemoteWebDriver(url, capabilities);
return driver;
}

Expand Down
12 changes: 7 additions & 5 deletions java/src/main/java/com/saucelabs/saucebindings/SauceOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ public class SauceOptions {
@Setter(AccessLevel.NONE) private MutableCapabilities seleniumCapabilities;
public TimeoutStore timeout = new TimeoutStore();

// w3c Settings
private Browser browserName = Browser.CHROME;
private String browserVersion = "latest";
private SaucePlatform platformName = SaucePlatform.WINDOWS_10;
// w3c settings - applies to any sauce session
protected Browser browserName = Browser.CHROME;
protected SaucePlatform platformName = SaucePlatform.WINDOWS_10;
protected String browserVersion = "latest";

// w3c settings - applies only to VDC
private PageLoadStrategy pageLoadStrategy;
private Boolean acceptInsecureCerts = null;
private Proxy proxy;
Expand Down Expand Up @@ -164,7 +166,7 @@ public Map<Timeouts, Integer> getTimeouts() {
return timeout.getTimeouts();
}

private SauceOptions(MutableCapabilities options) {
protected SauceOptions(MutableCapabilities options) {
seleniumCapabilities = new MutableCapabilities(options.asMap());
if (options.getCapability("browserName") != null) {
setCapability("browserName", options.getCapability("browserName"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

public enum SaucePlatform {
ANDROID("Android"),
IOS("iOS"),
WINDOWS_10("Windows 10"),
WINDOWS_8_1("Windows 8.1"),
WINDOWS_8("Windows 8"),
Expand Down
52 changes: 43 additions & 9 deletions java/src/main/java/com/saucelabs/saucebindings/SauceSession.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.saucelabs.saucebindings;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;

This comment has been minimized.

Copy link
@titusfortner

titusfortner Apr 16, 2020

Collaborator

Are there important things that you need AndroidDriver & IOSDriver for that won't work with AppiumDriver?

import lombok.Getter;
import lombok.Setter;
import org.openqa.selenium.InvalidArgumentException;
Expand All @@ -11,22 +14,37 @@

public class SauceSession {
@Getter @Setter protected DataCenter dataCenter = DataCenter.US_LEGACY;
@Getter private final SauceOptions sauceOptions;
@Getter private SauceOptions sauceOptions;
@Setter private URL sauceUrl;

@Getter private RemoteWebDriver driver;
@Getter private RemoteWebDriver webDriver;
@Getter private AppiumDriver appDriver;

public SauceSession() {
this(new SauceOptions());
}

public SauceSession(SauceOptions options) {
sauceOptions = options;
this.sauceOptions = options;
}

public RemoteWebDriver start() {
driver = createRemoteWebDriver(getSauceUrl(), sauceOptions.toCapabilities());
return driver;
String environment = sauceOptions.toCapabilities().getCapability("platformName").toString();
String browserName = sauceOptions.toCapabilities().getBrowserName();

if (browserName.equals("")){
if (environment.toLowerCase().equals("android")){
return createAndroidDriver(getSauceUrl(), sauceOptions.toCapabilities());
}
else if (environment.toLowerCase().equals("ios")) {
return createIOSDriver(getSauceUrl(), sauceOptions.toCapabilities());
}
else {
throw new InvalidArgumentException("Invalid Sauce Labs capabilities. Please set a browser name or set platformName as \"Android\" or \"IOS\".");
}
}
else {
return createRemoteWebDriver(getSauceUrl(), sauceOptions.toCapabilities());
}
}

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

protected AppiumDriver createIOSDriver(URL url, MutableCapabilities capabilities) {
return new IOSDriver<>(url, capabilities);
}

protected AppiumDriver createAndroidDriver(URL url, MutableCapabilities capabilities) {
return new AndroidDriver<>(url, capabilities);
}

public void stop(Boolean passed) {
String result = passed ? "passed" : "failed";
stop(result);
Expand All @@ -56,12 +82,20 @@ public void stop(String result) {
}

private void updateResult(String result) {
getDriver().executeScript("sauce:job-result=" + result);
if (webDriver != null)
getWebDriver().executeScript("sauce:job-result=" + result);
else {
System.out.println("use API for mobile case");
}

}

private void stop() {
if(getDriver() !=null) {
getDriver().quit();
if(getWebDriver() !=null) {
getWebDriver().quit();
}
if(getAppDriver() != null){
getAppDriver().quit();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static void main(String[] argv){

sauce.start();

sauce.getDriver().get("https://www.saucedemo.com/");
sauce.getWebDriver().get("https://www.saucedemo.com/");

sauce.stop(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.saucelabs.saucebindings;

import org.junit.Assert;
import org.junit.Test;

public class SauceAndroidOptionsTest {

@Test
public void defaultConstructor() {
SauceAndroidOptions options = new SauceAndroidOptions("Android Emulator", "9.0", DeviceOrientation.PORTRAIT);

Assert.assertEquals(SaucePlatform.ANDROID, options.getPlatformName());
Assert.assertEquals("9.0", options.getPlatformVersion());
Assert.assertEquals("Android Emulator", options.getDeviceName());
Assert.assertEquals(Browser.NONE, options.getBrowserName());
Assert.assertEquals("", options.getBrowserVersion());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.saucelabs.saucebindings;

import org.junit.Assert;
import org.junit.Test;

public class SauceIOSOptionsTest {

@Test
public void defaultConstructor() {
SauceIOSOptions options = new SauceIOSOptions("iPhone X Simulator", "13.0", DeviceOrientation.PORTRAIT);

Assert.assertEquals(SaucePlatform.IOS, options.getPlatformName());
Assert.assertEquals("13.0", options.getPlatformVersion());
Assert.assertEquals("iPhone X Simulator", options.getDeviceName());
Assert.assertEquals(Browser.NONE, options.getBrowserName());
Assert.assertEquals("", options.getBrowserVersion());

}
}

This file was deleted.

1 comment on commit 4bf7e90

@titusfortner
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So users would need to cast the result of the start method to be able to use mobile methods...

Android driver = (AndroidDriver) sauceSession.start();

Would it make sense to have different methods for different returns?

Android driver = sauceSession.startAndroid();

Please sign in to comment.