Skip to content

Instantly share code, notes, and snippets.

@manisha-A
Created September 4, 2018 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manisha-A/09cd491b9a478546ae6baf56099d1c72 to your computer and use it in GitHub Desktop.
Save manisha-A/09cd491b9a478546ae6baf56099d1c72 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
public class DemoTestWatcher {
@Before
public void setUp() {
/**
* Do something before test
*/
}
@After
public void tearDown() {
/**
* Do something after test
*/
}
@Test
public void firstTest(){
/**
* Run this test on browserstack which will attach a session id to browserstack execution.
* That session Id will be updated with the testRule.
*/
}
@Test
public void failTest(){
failTest();
}
@Rule
public TestRule testRule = new TestWatcher() {
@Override
protected void succeeded(Description description) {
System.out.println("Test passed: " + description.getMethodName());
}
@Override
protected void failed(Throwable e, Description description) {
System.out.println("Test failed: " + description.getMethodName());
}
@Override
protected void finished(Description description) {
try {
changeSessionIdToTestName(description);
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
};
/**
* This method will update the session id to test
* name after test execution is finished
* @param description
*/
private void changeSessionIdToTestName(Description description) throws IOException, URISyntaxException {
URI uri = new URI("https://USERNAME:ACCESSKEY@api.browserstack.com/automate/sessions/SESSION_ID.json");
HttpPut putRequest = new HttpPut(uri);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add((new BasicNameValuePair("name", description.getMethodName())));
putRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClientBuilder.create().build().execute(putRequest);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment