|
3 | 3 | import com.saucelabs.saucebindings.DataCenter;
|
4 | 4 | import com.saucelabs.saucebindings.SauceSession;
|
5 | 5 | import com.saucelabs.saucebindings.options.SauceOptions;
|
| 6 | +import java.lang.reflect.Method; |
| 7 | +import java.util.Arrays; |
6 | 8 | import org.openqa.selenium.remote.RemoteWebDriver;
|
7 | 9 | import org.testng.ITestResult;
|
8 | 10 | import org.testng.annotations.AfterMethod;
|
9 | 11 | import org.testng.annotations.BeforeMethod;
|
10 | 12 |
|
11 |
| -import java.lang.reflect.Method; |
12 |
| -import java.util.Arrays; |
13 |
| - |
14 | 13 | public class SauceBaseTest {
|
15 |
| - private static ThreadLocal<SauceSession> session = new ThreadLocal<>(); |
16 |
| - private static ThreadLocal<DataCenter> dataCenterThreadLocal = new ThreadLocal<>(); |
| 14 | + private static ThreadLocal<SauceSession> session = new ThreadLocal<>(); |
| 15 | + private static ThreadLocal<DataCenter> dataCenterThreadLocal = new ThreadLocal<>(); |
17 | 16 |
|
18 |
| - public RemoteWebDriver getDriver() { |
19 |
| - return getSession().getDriver(); |
20 |
| - } |
| 17 | + public RemoteWebDriver getDriver() { |
| 18 | + return getSession().getDriver(); |
| 19 | + } |
21 | 20 |
|
22 |
| - public SauceSession getSession() { |
23 |
| - return session.get(); |
24 |
| - } |
| 21 | + public SauceSession getSession() { |
| 22 | + return session.get(); |
| 23 | + } |
25 | 24 |
|
26 |
| - public DataCenter getDataCenter() { |
27 |
| - return dataCenterThreadLocal.get(); |
28 |
| - } |
| 25 | + public DataCenter getDataCenter() { |
| 26 | + return dataCenterThreadLocal.get(); |
| 27 | + } |
29 | 28 |
|
30 |
| - /** |
31 |
| - * This is designed to be able to be overridden in a subclass |
32 |
| - * |
33 |
| - * @return default instance of SauceOptions |
34 |
| - */ |
35 |
| - protected SauceOptions createSauceOptions() { |
36 |
| - return new SauceOptions(); |
37 |
| - } |
| 29 | + /** |
| 30 | + * This is designed to be able to be overridden in a subclass |
| 31 | + * |
| 32 | + * @return default instance of SauceOptions |
| 33 | + */ |
| 34 | + protected SauceOptions createSauceOptions() { |
| 35 | + return new SauceOptions(); |
| 36 | + } |
38 | 37 |
|
39 |
| - /** |
40 |
| - * This method ensures a test name is set by default, and then starts the session |
41 |
| - * It creates a session and a driver |
42 |
| - * |
43 |
| - * @param method the default parameter for BeforeMethod |
44 |
| - */ |
45 |
| - @BeforeMethod |
46 |
| - protected void setup(Method method) { |
47 |
| - SauceOptions sauceOptions = createSauceOptions(); |
48 |
| - if (sauceOptions.sauce().getName() == null) { |
49 |
| - sauceOptions.sauce().setName(method.getName()); |
50 |
| - } |
51 |
| - session.set(new SauceSession(sauceOptions)); |
52 |
| - DataCenter dataCenter = getDataCenter(); |
53 |
| - if (dataCenter != null) { |
54 |
| - getSession().setDataCenter(getDataCenter()); |
55 |
| - } |
56 |
| - getSession().start(); |
| 38 | + /** |
| 39 | + * This method ensures a test name is set by default, and then starts the session It creates a |
| 40 | + * session and a driver |
| 41 | + * |
| 42 | + * @param method the default parameter for BeforeMethod |
| 43 | + */ |
| 44 | + @BeforeMethod |
| 45 | + protected void setup(Method method) { |
| 46 | + SauceOptions sauceOptions = createSauceOptions(); |
| 47 | + if (sauceOptions.sauce().getName() == null) { |
| 48 | + sauceOptions.sauce().setName(method.getName()); |
57 | 49 | }
|
| 50 | + session.set(new SauceSession(sauceOptions)); |
58 | 51 |
|
59 |
| - @AfterMethod |
60 |
| - protected void teardown(ITestResult result) { |
61 |
| - if (result.isSuccess()) { |
62 |
| - getSession().stop(true); |
63 |
| - } else { |
64 |
| - Throwable e = result.getThrowable(); |
65 |
| - getDriver().executeScript("sauce:context=Failure Reason: " + e.getMessage()); |
| 52 | + DataCenter dataCenter = getDataCenter(); |
| 53 | + if (dataCenter != null) { |
| 54 | + getSession().setDataCenter(getDataCenter()); |
| 55 | + } |
| 56 | + getSession().start(); |
| 57 | + } |
66 | 58 |
|
67 |
| - for (Object trace : Arrays.stream(e.getStackTrace()).toArray()) { |
68 |
| - if (trace.toString().contains("sun")) { |
69 |
| - break; |
70 |
| - } |
71 |
| - getDriver().executeScript("sauce:context=Backtrace: " + trace); |
72 |
| - } |
| 59 | + @AfterMethod |
| 60 | + protected void teardown(ITestResult result) { |
| 61 | + if (result.isSuccess()) { |
| 62 | + getSession().stop(true); |
| 63 | + } else { |
| 64 | + Throwable e = result.getThrowable(); |
| 65 | + getDriver().executeScript("sauce:context=Failure Reason: " + e.getMessage()); |
73 | 66 |
|
74 |
| - getSession().stop(false); |
| 67 | + for (Object trace : Arrays.stream(e.getStackTrace()).toArray()) { |
| 68 | + if (trace.toString().contains("sun")) { |
| 69 | + break; |
75 | 70 | }
|
| 71 | + getDriver().executeScript("sauce:context=Backtrace: " + trace); |
| 72 | + } |
| 73 | + |
| 74 | + getSession().stop(false); |
76 | 75 | }
|
| 76 | + } |
77 | 77 | }
|
0 commit comments