/// <summary> /// Create a TestBase for your specific project (see TestBaseStarAppIos, for example) and call this method. /// The Setup annotation was removed to prevent it being called when there is a project specific Setup (see above). /// </summary> public static void Setup() { // Log Before Action Log.BeforeAction(); // Perform the action try { // Create a variable to serve as a flag for if a new session needs to be created or not bool createNewSession = false; // Create a variable to hold the Test's WebDriver AppiumDriver <AppiumWebElement> driver = null; // Is the global WebDriver varaiable set? if (Session.driver != null) { // Create a variable to hold the Session Info Dictionary <string, object> sessionInfo = null; // Try to get the Session Info (which only works if there is a valid session) from the global variable // Note: We need to use the same Appium session (driver) for all test or we could lose the device in the cloud. try { sessionInfo = Session.driver.SessionDetails; } catch { /* do nothing */ } // Check the Session Info variable has some content if (sessionInfo != null) { // Use the existing session Log.WriteLine(" [INFO] Using the existing session."); driver = Session.driver; } else { // Set the flag to create a new session to true createNewSession = true; } } // Should we create a new session? if (createNewSession == true) { // Wait 60 seconds to timeout any existing sessions (in SauceLab's TestObject) if (ConfigurationManager.AppSettings["appConfig"].ToString().Contains("SauceLabs")) { Sleep.Milliseconds(60000, "Waiting 60 seconds to kill the current session (in SauceLab's TestObject)."); } // Create a new session Log.WriteLine(" [INFO] Creating a new session."); driver = Session.Create(); } // Save a reference to the current Test's driver (session) in its TestContext TestContext.Set("driver", driver); // Reset the app AppBase.ResetApp(); // Logging - After action success Log.Success(); } catch (Exception e) { // Logging - After action exception Log.Failure(e.Message); // Fail current test Assert.Fail(e.Message); } finally { // Logging - After action Log.Finally(); } }
public void Setup() { // Save a reference to the current Test's log in its TestContext TestContext.Set("log", ""); // Get the Test's custom Attributes IEnumerable <CustomAttributeData> customAttributeDatas = new StackTrace().GetFrame(1).GetMethod().CustomAttributes; // Log the custom Attributes Log.CustomAttributes(customAttributeDatas); // Write an end-line Log.WriteLine(); // Figure out the padding (if any) to prepend to the log line LogPadding logPadding = new LogPadding(new StackTrace().GetFrame(1).GetMethod().ReflectedType); // Logging - Before action StringBuilder sb = new StringBuilder(); sb.AppendLine(logPadding.Padding + "TestBase.Setup()"); //sb.AppendLine(logPadding.InfoPadding + "[STACK] Caller: " + new StackTrace().GetFrame(1).GetMethod().ReflectedType + "." + new StackTrace().GetFrame(1).GetMethod().Name + "()"); Log.Write(sb.ToString()); // Perform the action try { // Create a variable to serve as a flag for if a new session needs to be created or not bool createNewSession = false; // Create a variable to hold the Test's WebDriver AppiumDriver <AppiumWebElement> driver = null; // Is the global WebDriver varaiable set? if ((Session.driver != null)) { // Is there session info? Dictionary <string, object> sessionInfo = null; try { sessionInfo = Session.driver.SessionDetails; } catch { /* do nothing */ } if (sessionInfo != null) { // Use the existing session Log.WriteLine(logPadding.InfoPadding + "[INFO] Using the existing session."); driver = Session.driver; } else { // Set the flag to create a new session to true createNewSession = true; } } // Should we create a new session? if (createNewSession == true) { // Wait 60 seconds to kill the current session (the default TimeOut for Appium) Sleep.Milliseconds(60000, "Waiting 60 seconds to kill the current session."); // Create a new session Log.WriteLine(logPadding.InfoPadding + "[INFO] Creating a new session."); driver = Session.Create(); } // Save a reference to the current Test's driver in its TestContext TestContext.Set("driver", driver); // Reset the app AppBase.ResetApp(); // Logging - After action success Log.Success(logPadding.Padding); } catch (Exception e) { // Logging - After action exception sb = Log.Exception(sb, e); // Fail current Test Assert.Fail(sb.ToString()); } finally { // Logging - After action Log.Finally(logPadding.Padding); } }