|
| 1 | +package com.saucelabs.saucebindings.junit4; |
| 2 | + |
| 3 | +import com.saucelabs.saucebindings.DataCenter; |
| 4 | +import com.saucelabs.saucebindings.SauceSession; |
| 5 | +import com.saucelabs.saucebindings.options.SauceOptions; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.logging.Logger; |
| 11 | +import lombok.Getter; |
| 12 | +import org.junit.rules.TestWatcher; |
| 13 | +import org.junit.runner.Description; |
| 14 | +import org.openqa.selenium.Capabilities; |
| 15 | +import org.openqa.selenium.NoSuchSessionException; |
| 16 | +import org.openqa.selenium.WebDriver; |
| 17 | + |
| 18 | +@Getter |
| 19 | +public class SauceBindingsWatcher extends TestWatcher { |
| 20 | + private static final Logger logger = Logger.getLogger(SauceBindingsWatcher.class.getName()); |
| 21 | + private final SauceOptions sauceOptions; |
| 22 | + private final DataCenter dataCenter; |
| 23 | + private SauceSession session; |
| 24 | + private WebDriver driver; |
| 25 | + |
| 26 | + public SauceBindingsWatcher() { |
| 27 | + this(new SauceOptions(), DataCenter.US_WEST); |
| 28 | + } |
| 29 | + |
| 30 | + public SauceBindingsWatcher(SauceOptions sauceOptions) { |
| 31 | + this(sauceOptions, DataCenter.US_WEST); |
| 32 | + } |
| 33 | + |
| 34 | + public SauceBindingsWatcher(SauceOptions sauceOptions, DataCenter dataCenter) { |
| 35 | + this.sauceOptions = sauceOptions; |
| 36 | + this.dataCenter = dataCenter; |
| 37 | + } |
| 38 | + |
| 39 | + public SauceBindingsWatcher(Capabilities capabilities) { |
| 40 | + this(capabilities, DataCenter.US_WEST); |
| 41 | + } |
| 42 | + |
| 43 | + public SauceBindingsWatcher(Capabilities capabilities, DataCenter dataCenter) { |
| 44 | + this.sauceOptions = new SauceOptions(); |
| 45 | + Map<String, Object> capabilitiesMap = new HashMap<>(capabilities.asMap()); |
| 46 | + Optional.ofNullable(capabilitiesMap.get("sauce:options")) |
| 47 | + .filter(Map.class::isInstance) |
| 48 | + .map(Map.class::cast) |
| 49 | + .ifPresent( |
| 50 | + sauceOptionsMap -> { |
| 51 | + capabilitiesMap.put("sauce", sauceOptionsMap); |
| 52 | + capabilitiesMap.remove("sauce:options"); |
| 53 | + }); |
| 54 | + this.sauceOptions.mergeCapabilities(capabilitiesMap); |
| 55 | + this.dataCenter = dataCenter; |
| 56 | + } |
| 57 | + |
| 58 | + public SauceBindingsWatcher(DataCenter dataCenter) { |
| 59 | + this(new SauceOptions(), dataCenter); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void starting(Description description) { |
| 64 | + if (sauceOptions.sauce().getName() == null) { |
| 65 | + sauceOptions.sauce().setName(description.getMethodName()); |
| 66 | + } |
| 67 | + |
| 68 | + session = new SauceSession(sauceOptions); |
| 69 | + session.setDataCenter(dataCenter); |
| 70 | + driver = session.start(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void succeeded(Description description) { |
| 75 | + try { |
| 76 | + session.stop(true); |
| 77 | + } catch (NoSuchSessionException e) { |
| 78 | + logger.severe( |
| 79 | + "Driver quit prematurely; Remove calls to `driver.quit()` to allow" |
| 80 | + + " SauceBindingsExtension to stop the test"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected void failed(Throwable e, Description description) { |
| 86 | + if (session != null) { |
| 87 | + try { |
| 88 | + session.annotate("Failure Reason: " + e.getMessage()); |
| 89 | + |
| 90 | + Arrays.stream(e.getStackTrace()) |
| 91 | + .map(StackTraceElement::toString) |
| 92 | + .filter(line -> !line.contains("sun")) |
| 93 | + .forEach(session::annotate); |
| 94 | + |
| 95 | + session.stop(false); |
| 96 | + } catch (NoSuchSessionException ex) { |
| 97 | + logger.severe( |
| 98 | + "Driver quit prematurely; Remove calls to `driver.quit()` to allow SauceBindingsExtension" |
| 99 | + + " to stop the test"); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments