Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Last active May 25, 2017 15:07
Show Gist options
  • Save sauceaaron/61bd2595a616cc232636805ca1f46d4e to your computer and use it in GitHub Desktop.
Save sauceaaron/61bd2595a616cc232636805ca1f46d4e to your computer and use it in GitHub Desktop.
Running a pre-run executable
echo "162.222.75.243 www.google.com" >> /etc/hosts
package saucelabs.examples;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.net.URL;
import java.util.*;
import com.saucelabs.saucerest.SauceREST;
import org.junit.rules.TestName;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import static org.assertj.core.api.Assertions.assertThat;
public class Test_With_Prerun_Executable
{
@Rule
public TestName test = new TestName();
String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");
String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
String SAUCE_URL = "https://" + SAUCE_USERNAME + ":" + SAUCE_ACCESS_KEY + "@ondemand.saucelabs.com:443/wd/hub";
String SAUCE_PRERUN = System.getenv("SAUCE_PRERUN");
DesiredCapabilities capabilities;
DesiredCapabilities desiredCapability;
RemoteWebDriver browser;
SauceREST api;
Boolean passed = true;
@Before
public void setup() throws Exception
{
File prerunFile = new File(SAUCE_PRERUN);
api = new SauceREST(SAUCE_USERNAME, SAUCE_ACCESS_KEY);
api.uploadFile(prerunFile);
HashMap<String, String> prerunParams = new HashMap<String, String>();
prerunParams.put("executable", "sauce-storage:" + prerunFile.getName());
prerunParams.put("background","false");
capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("name", this.getClass().getSimpleName() + " " + test.getMethodName());
capabilities.setCapability("build", System.getenv("BUILD_TAG"));
capabilities.setCapability("prerun", prerunParams);
browser = new RemoteWebDriver(new URL(SAUCE_URL), capabilities);
}
@Test
public void changed_hosts_file() throws Exception
{
try
{
browser.get("http://google.com");
String title = browser.getTitle();
System.out.println("title: " + title);
assertThat(title).contains("Sauce Labs");
}
catch (AssertionError e)
{
System.out.println("test failed");
passed = false;
throw e;
}
}
@After
public void teardown() throws Exception
{
if (passed == true)
{
api.jobPassed(browser.getSessionId().toString());
}
if (passed == false)
{
api.jobFailed(browser.getSessionId().toString());
}
browser.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment