Skip to content

Instantly share code, notes, and snippets.

@bernii
Last active December 18, 2015 00:29
Show Gist options
  • Save bernii/5697073 to your computer and use it in GitHub Desktop.
Save bernii/5697073 to your computer and use it in GitHub Desktop.
Sample Webdriver Java example with custom-data / rest call public job: https://saucelabs.com/tests/bfb53744b3474162bf3cf3f6f4b1cb8d
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.remote.DesiredCapabilities;
public class WebdriverSample {
public WebDriver driver;
public Wait<WebDriver> wait;
public static String username = "SAUCE-USERNAME";
public static String key = "SAUCE-KEY";
public String jobId;
public void setUp() throws MalformedURLException{
DesiredCapabilities caps = DesiredCapabilities.firefox();
caps.setCapability("name", "Java ♥");
caps.setCapability("platform", "Linux");
caps.setCapability("version", "21");
this.driver = new RemoteWebDriver(
new URL("http://" + username + ":" + key + "@ondemand.saucelabs.com:80/wd/hub"),
caps);
this.wait = new WebDriverWait(driver, 30);
this.jobId = ((RemoteWebDriver) this.driver).getSessionId().toString();
}
public static void main(String[] args) throws MalformedURLException {
WebdriverSample sample = new WebdriverSample();
sample.setUp();
sample.driver.get("http://www.google.com/");
boolean result;
try {
result = sample.firstPageContainsSauce();
} catch(Exception e) {
e.printStackTrace();
result = false;
} finally {
sample.driver.quit();
}
System.out.println("Test " + (result? "passed." : "failed."));
sample.updateJobData(result);
if (!result) {
System.exit(1);
}
}
public boolean firstPageContainsSauce() {
//type search query
this.driver.findElement(By.name("q")).sendKeys("sauce labs\n");
// click search
this.driver.findElement(By.name("btnG")).click();
// Wait for search to complete
this.wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver webDriver) {
System.out.println("Searching ...");
return webDriver.findElement(By.id("resultStats")) != null;
}
});
// Look for saucelabs.com in the results
return driver.findElement(By.tagName("body")).getText().contains("saucelabs.com");
}
public void updateJobData(boolean result) {
try {
URL url = new URL("http://saucelabs.com/rest/v1/" + username + "/jobs/" + this.jobId);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("PUT");
conn.setRequestProperty("Content-Type", "application/json");
byte[] authEncBytes = Base64.encodeBase64((username + ":" + key).getBytes());
conn.setRequestProperty("Authorization", "Basic " + new String(authEncBytes));
String input = "{\"passed\":" + result +
", \"custom-data\": {\"java-test\": {\"failed\": 0, \"total\": 5, \"passed\": 5, \"runtime\": 20}}}";
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment