11package com .frameworkium .tests .internal ;
22
33import java .lang .reflect .Method ;
4- import java .sql .Driver ;
54import java .util .ArrayList ;
6- import java .util .Collections ;
75import java .util .List ;
86
7+ import com .frameworkium .config .DriverSetup ;
98import com .frameworkium .config .DriverType ;
109import org .apache .logging .log4j .LogManager ;
1110import org .apache .logging .log4j .Logger ;
1211import org .openqa .selenium .JavascriptExecutor ;
13- import org .openqa .selenium .WebDriver ;
14- import org .openqa .selenium .remote .RemoteWebDriver ;
1512import org .openqa .selenium .remote .SessionId ;
16- import org .openqa .selenium .remote .SessionNotFoundException ;
17- import org .testng .annotations .AfterSuite ;
18- import org .testng .annotations .BeforeMethod ;
19- import org .testng .annotations .BeforeSuite ;
20- import org .testng .annotations .Listeners ;
13+ import org .testng .annotations .*;
2114
2215import ru .yandex .qatools .allure .annotations .Issue ;
2316import ru .yandex .qatools .allure .annotations .TestCaseId ;
3528import com .saucelabs .common .SauceOnDemandSessionIdProvider ;
3629import com .saucelabs .testng .SauceOnDemandAuthenticationProvider ;
3730
38- import static com .frameworkium .config .DriverSetup .returnDesiredDriverType ;
39- import static com .frameworkium .config .DriverSetup .useRemoteDriver ;
40- import static com .frameworkium .config .DriverType .isMobile ;
41- import static com .frameworkium .config .SystemProperty .MAXIMISE ;
42-
4331@ Listeners ({CaptureListener .class , ScreenshotListener .class , MethodInterceptor .class , SauceLabsListener .class ,
4432 TestListener .class , ResultLoggerListener .class })
4533
@@ -48,8 +36,7 @@ public abstract class BaseTest implements SauceOnDemandSessionIdProvider, SauceO
4836 private static ThreadLocal <Boolean > requiresReset ;
4937 private static ThreadLocal <ScreenshotCapture > capture ;
5038 private static ThreadLocal <DriverType > driverType ;
51- private static List <DriverType > activeDriverTypes
52- = Collections .synchronizedList (new ArrayList <DriverType >());
39+ private static List <DriverType > activeDriverTypes = new ArrayList <>();
5340 private static Logger logger = LogManager .getLogger (BaseTest .class );
5441
5542 public static String userAgent ;
@@ -61,12 +48,13 @@ public abstract class BaseTest implements SauceOnDemandSessionIdProvider, SauceO
6148 * - Initialise the screenshot capture
6249 * - Configure the browser based on paramaters (maximise window, session resets, user agent)
6350 */
64- @ BeforeSuite ( alwaysRun = true )
51+ @ BeforeSuite
6552 public static void instantiateDriverObject () {
6653 driverType = new ThreadLocal <DriverType >() {
6754 @ Override
6855 protected DriverType initialValue () {
69- DriverType driverType = returnDesiredDriverType ();
56+ DriverType driverType = new DriverSetup ()
57+ .returnDesiredDriverType ();
7058 driverType .instantiate ();
7159 activeDriverTypes .add (driverType );
7260 return driverType ;
@@ -84,16 +72,29 @@ protected ScreenshotCapture initialValue() {
8472 return null ;
8573 }
8674 };
75+ }
76+
77+ /**
78+ * The methods which configure the browser once a test runs
79+ * - Maximises browser based on the driver type
80+ * - Initialises screenshot capture if needed
81+ * - Clears the session if another test ran prior
82+ * - Sets the user agent of the browser
83+ *
84+ * @param testMethod - The test method name of the test
85+ */
86+ @ BeforeMethod
87+ public static void configureBrowserBeforeTest (Method testMethod ) {
8788 configureDriverBasedOnParams ();
89+ initialiseNewScreenshotCapture (testMethod );
8890 }
8991
9092 /**
9193 * Initialise the screenshot capture and link to issue/test case id
9294 *
9395 * @param testMethod - Test method passed from the test script
9496 */
95- @ BeforeMethod
96- public static void initialiseNewScreenshotCapture (Method testMethod ) {
97+ private static void initialiseNewScreenshotCapture (Method testMethod ) {
9798 if (ScreenshotCapture .isRequired ()) {
9899 String testID = "n/a" ;
99100 try {
@@ -111,7 +112,6 @@ public static void initialiseNewScreenshotCapture(Method testMethod) {
111112 } catch (NullPointerException e ) {
112113 logger .debug ("No Test Case ID defined." );
113114 }
114-
115115 capture .set (new ScreenshotCapture (testID , driverType .get ().getDriver ()));
116116 }
117117 }
@@ -144,14 +144,14 @@ private static void setUserAgent() {
144144 /**
145145 * Loops through all active driver types and tears down the driver object
146146 */
147- @ AfterSuite ( alwaysRun = true )
147+ @ AfterSuite
148148 public static void closeDriverObject () {
149- for ( DriverType driverType : activeDriverTypes ) {
150- try {
149+ try {
150+ for ( DriverType driverType : activeDriverTypes ) {
151151 driverType .tearDownDriver ();
152- } catch (Exception e ) {
153- logger .warn ("Session quit unexpectedly." , e );
154152 }
153+ } catch (Exception e ) {
154+ logger .warn ("Session quit unexpectedly." , e );
155155 }
156156 }
157157
@@ -177,7 +177,7 @@ public String getSessionId() {
177177 */
178178 private static String getUserAgent () {
179179 String ua ;
180- JavascriptExecutor js = ( JavascriptExecutor ) getDriver ();
180+ JavascriptExecutor js = getDriver ();
181181 try {
182182 ua = (String ) js .executeScript ("return navigator.userAgent;" );
183183 } catch (Exception e ) {
0 commit comments