/**
* The underlying {@link Set<String>} instance which contains the
* {@link WebDriver} session id. This listener only expects
* {@link AbstractSessionIds} implemented in all the tests.
*
* @author mbrar
*/
public class SauceMobileSiteListener extends TestListenerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(SauceMobileSiteListener.class);
private static final String SELENIUM_IS_LOCAL = "SELENIUM_IS_LOCAL";
private CounterMobileSite count;
/**
* The instance of the Sauce OnDemand Java REST API client.
*/
private SauceREST sauceREST;
/**
* Treat this test as a local test or run in SauceLabs.
*/
private boolean isLocal = false;
/**
*
* @param testContext
*/
@Override
public void onStart(final ITestContext testContext) {
super.onStart(testContext);
final String local = System.getenv(SELENIUM_IS_LOCAL);
if (local != null && local.isEmpty()) {
isLocal = true;
}
}
/**
* @param result
*/
@Override
public void onTestStart(final ITestResult result) {
super.onTestStart(result);
if (isLocal) {
return;
}
this.sauceREST = SelectorsInputs.identity(SauceREST.class, "sauceRest");
}
private void initlize(final ITestResult result) {
if (result.getInstance() instanceof AbstractSessionIds) {
final AbstractSessionIds sessionIds = (AbstractSessionIds) result.getInstance();
if (count == null) {
this.count = new CounterMobileSite(7, null, 0);
}
final AbstractMobileSite local = new MobileImmutable(null, "", null);
final Set<String> localSet = sessionIds.getImmutableList();
final Boolean validateSize = count.getCount().compareTo(count.getTop()) <= 0 && !localSet.isEmpty();
if (validateSize) {
count = count.withImmutable(local.withSessionId(localSet.iterator().next()));
if (!localSet.isEmpty()) {
localSet.remove(count.getImmutable().getSessionId());
count.increment();
}
}
LOGGER.info("SauceOnDemandSessionID={} job-name={}", count.getImmutable().getSessionId(), result
.getMethod().getMethodName());
}
}
/**
* @param testResult
*/
@Override
public void onTestFailure(final ITestResult testResult) {
super.onTestFailure(testResult);
if (isLocal) {
return;
}
validateNextSuit();
initlize(testResult);
if (this.sauceREST != null
&& count.getImmutable().getSessionId() != null) {
markJobAsFailed();
printPublicJobLink();
}
}
private void validateNextSuit() {
if (count != null && count.score()) {
count.resetCount();
}
}
private void markJobAsFailed() {
final Map<String, Object> updates = new HashMap<String, Object>();
updates.put("passed", false);
Utils.addBuildNumberToUpdate(updates);
sauceREST.updateJobInfo(count.getImmutable().getSessionId(), updates);
}
private void printPublicJobLink() {
final String authLink = this.sauceREST.getPublicJobLink(count
.getImmutable().getSessionId());
LOGGER.info("Test failure public job link: {}", authLink);
}
/**
* @param testResult
*/
@Override
public void onTestSuccess(final ITestResult testResult) {
super.onTestSuccess(testResult);
if (isLocal) {
return;
}
validateNextSuit();
initlize(testResult);
markJobAsPassed();
}
private void markJobAsPassed() {
if (this.sauceREST != null && count.getImmutable().getSessionId() != null) {
final Map<String, Object> updates = new HashMap<String, Object>();
updates.put("passed", true);
Utils.addBuildNumberToUpdate(updates);
sauceREST.updateJobInfo(count.getImmutable().getSessionId(), updates);
}
}
}