Skip to content

Instantly share code, notes, and snippets.

@pulse00
Created June 19, 2018 09:31
Show Gist options
  • Save pulse00/ca7ac8d84c37bb90527e14e2c4fd45c5 to your computer and use it in GitHub Desktop.
Save pulse00/ca7ac8d84c37bb90527e14e2c4fd45c5 to your computer and use it in GitHub Desktop.
package spock
import org.openqa.selenium.remote.RemoteWebDriver
@Singleton
class BrowserInstance {
def RemoteWebDriver driver
}
class BrowserStackListener extends AbstractRunListener {
void error(ErrorInfo error) {
if (!BrowserInstance.instance.driver) {
return
}
def username = getenv("BROWSERSTACK_USER")
def accessKey = getenv("BROWSERSTACK_ACCESSKEY")
def sessionId = BrowserInstance.instance.driver.getSessionId().toString()
def reportUrl = "https://${username}:${accessKey}@api.browserstack.com/automate/sessions/${sessionId}.json"
HttpPut putRequest = new HttpPut(new URI(reportUrl))
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>()
nameValuePairs.add((new BasicNameValuePair("status", "failed")))
nameValuePairs.add((new BasicNameValuePair("reason", error.error.toString())))
putRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs))
print "Reporting failed test to browserStack with sessionId ${sessionId}"
HttpClientBuilder.create().build().execute(putRequest)
}
}
/// ...
browserStack {
driver = {
def driver = browserStackDriver()
BrowserInstance.instance.driver = driver
return driver
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment