Skip to content

Instantly share code, notes, and snippets.

@nadvolod
Created June 3, 2020 15:58
Show Gist options
  • Save nadvolod/48a6abbbb6449bae7b651c6c54798109 to your computer and use it in GitHub Desktop.
Save nadvolod/48a6abbbb6449bae7b651c6c54798109 to your computer and use it in GitHub Desktop.
How to add test status using Sauce Labs with JUnit 4
package com.hilton.v2;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class SauceTestWatcher extends TestWatcher {
private WebDriver driver;
public void setDriver(WebDriver driver) {
this.driver = driver;
}
protected void succeeded(Description description) {
if(driver == null){return;}
((JavascriptExecutor) driver).executeScript("sauce:job-result=passed");
driver.quit();
}
protected void failed(Description description) {
if(driver == null)
return;
((JavascriptExecutor) driver).executeScript("sauce:job-result=failed");
driver.quit();
}
}
package com.hilton.v2;
import io.appium.java_client.MobileBy;
import io.appium.java_client.ios.IOSDriver;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.runners.Parameterized.Parameter;
public class TestBase {
private WebDriver driver;
@Rule
public SauceTestWatcher testWatcher = new SauceTestWatcher();
@Rule
public TestName name = new TestName() {
public String getMethodName() {
return String.format("%s", super.getMethodName());
}
};
@Before
public void setUp() throws MalformedURLException {
MutableCapabilities capabilities = new MutableCapabilities();
capabilities.setCapability("appiumVersion", "1.16.0");
capabilities.setCapability("autoAcceptAlerts", "true");
capabilities.setCapability("idleTimeout", "30");
capabilities.setCapability("noReset", "true");
capabilities.setCapability("newCommandTimeout", "30");
capabilities.setCapability("language", "en");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", platformVersion);
capabilities.setCapability("deviceName", deviceName);
capabilities.setCapability("name", name.getMethodName());
capabilities.setCapability("app", "sauce-storage:HHonors-sim-05202020.zip");
driver = new IOSDriver(
new URL("https://" + System.getenv("SAUCE_USERNAME") + ":" +
System.getenv("SAUCE_ACCESS_KEY") +
"@ondemand.saucelabs.com:443" +"/wd/hub"),
capabilities);
testWatcher.setDriver(driver);
var wait = new WebDriverWait(driver, 20);
wait.until(
ExpectedConditions.visibilityOfElementLocated(
MobileBy.AccessibilityId("Close"))).click();
}
public WebDriver getDriver() {
return driver;
}
@Parameters(name = "{0},{1}")
public static Collection<Object[]> crossBrowserData() {
return Arrays.asList(new Object[][] {
//{ "13.2", "iPhone 8 Plus Simulator" },
//{ "13.2", "iPhone 7 Simulator" },
//{ "13.2", "iPhone XR Simulator" },
{ "13.2", "iPhone XS Simulator" },
{ "13.2", "iPhone XS Max Simulator" },
{ "13.2", "iPhone X Simulator" },
{ "13.2", "iPhone SE Simulator" },
{ "13.2", "iPhone 8 Simulator" },
{ "13.2", "iPhone 7 Plus Simulator" },
{ "13.2", "iPhone XS Simulator" },
{ "13.2", "iPhone XS Max Simulator" },
{ "13.2", "iPhone X Simulator" },
{ "13.2", "iPhone SE Simulator" },
{ "13.2", "iPhone 8 Simulator" },
{ "13.2", "iPhone 7 Plus Simulator" },
});
}
@Parameter // first data value (0) is default
public String platformVersion;
@Parameter(1)
public String deviceName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment