Created
April 29, 2012 17:00
-
-
Save testingbot/2551920 to your computer and use it in GitHub Desktop.
example sending test status back with java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import junit.framework.TestCase; | |
import org.openqa.selenium.*; | |
import org.openqa.selenium.remote.*; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.concurrent.TimeUnit; | |
import junit.framework.AssertionFailedError; | |
import junit.framework.Test; | |
import junit.framework.TestListener; | |
import junit.framework.TestResult; | |
import org.apache.commons.codec.binary.Base64; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpPut; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.params.HttpConnectionParams; | |
public class TestGoogle extends TestCase { | |
private RemoteWebDriver driver; | |
private SessionId sessionId; | |
/** | |
* @return the driver | |
*/ | |
public WebDriver getDriver() { | |
return driver; | |
} | |
/** | |
* @return the sessionId | |
*/ | |
public SessionId getSessionId() { | |
return sessionId; | |
} | |
/** | |
* @param sessionId the sessionId to set | |
*/ | |
public void setSessionId(SessionId sessionId) { | |
this.sessionId = sessionId; | |
} | |
class MyListener implements TestListener { | |
private TestCase test; | |
private ArrayList<String> errors = new ArrayList<String>(); | |
public MyListener(TestCase test) { | |
this.test = test; | |
} | |
@Override | |
public void addError(Test test, Throwable t) { | |
this.errors.add(t.getMessage()); | |
} | |
@Override | |
public void addFailure(Test test, AssertionFailedError t) { | |
this.errors.add(t.getMessage()); | |
} | |
@Override | |
public void endTest(Test test) { | |
System.out.println("End a test: " + ((TestGoogle) test).getSessionId().toString()); | |
String success = (this.errors.size() == 0) ? "1" : "0"; | |
String data = "test[success]=" + success + "&test[name]=" + this.test.getName(); | |
if (this.errors.size() > 0) | |
{ | |
data += "&test[status_message]=" + this.errors.toString(); | |
} | |
this._sendApi("/tests/" + ((TestGoogle) test).getSessionId().toString(), data); | |
} | |
@Override | |
public void startTest(Test test) { | |
System.out.println("Start a test."); | |
} | |
private void _sendApi(String command, String data) { | |
try { | |
command += "?" + data; | |
final HttpClient httpClient = new DefaultHttpClient(); | |
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000); | |
HttpPut httpPut = new HttpPut("http://api.testingbot.com/v1" + command); | |
String credentials = "key:secret"; | |
String encodedAuthorization = Base64.encodeBase64String(credentials.getBytes()); | |
httpPut.addHeader("Authorization", "Basic " + encodedAuthorization); | |
HttpResponse response = httpClient.execute(httpPut); | |
int statusCode = response.getStatusLine().getStatusCode(); | |
System.out.println(String.valueOf(statusCode)); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
@Override | |
public void setUp() throws Exception { | |
DesiredCapabilities capabillities = DesiredCapabilities.firefox(); | |
capabillities.setCapability("version", "11"); | |
capabillities.setCapability("platform", Platform.WINDOWS); | |
capabillities.setCapability("name", "Testing Selenium 2"); | |
this.driver = new RemoteWebDriver( | |
new URL("http://key:[email protected]:4444/wd/hub"), | |
capabillities); | |
this.setSessionId(this.driver.getSessionId()); | |
getDriver().manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); | |
} | |
@Override | |
public void run(TestResult tr) { | |
MyListener l = new MyListener(this); | |
tr.addListener(l); | |
super.run(tr); | |
} | |
public void testSimple() throws Exception { | |
this.getDriver().get("http://www.google.com"); | |
assertEquals("Google", this.getDriver().getTitle()); | |
} | |
@Override | |
public void tearDown() throws Exception { | |
this.getDriver().quit(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment