/// <summary>
        /// Check that all given elements are present and have some dimensions greater than 0
        /// </summary>
        public static string CheckAllElementsArePresent(List <WebElement> elementList)
        {
            // Initialize the error counter
            int errorCount = 0;
            // Initialize the error string builder
            StringBuilder sb = new StringBuilder();

            // Loop over each element in the list
            foreach (WebElement element in elementList)
            {
                // Find the element(s)
                IList <WebElement> result = AppBase.FindElements(element.by);
                // Handle the result
                if (result.Count == 0)
                {
                    sb.AppendLine("[ERROR] Could not find element");
                    if (element.by != null)
                    {
                        sb.AppendLine("    [DEBUG] Element Description: " + element.description);
                    }
                    if (element.by != null)
                    {
                        sb.AppendLine("    [DEBUG] Element Finds By: " + element.by);
                    }
                    errorCount++;
                }
            }
            return(sb.ToString());
        }
        /// <summary>
        /// Wait function that will repeatly query the app until a matching element is not found.
        /// </summary>
        public void WaitForElementNotDisplayed()
        {
            // Log Before Action
            Log.BeforeAction(new OrderedDictionary()
            {
                { "webElement.description", this.description },
                { "webElement.by", this.by }
            });

            // Perform the action
            try
            {
                if (this.by != null)
                {
                    AppBase.WaitForElementToBeNotDisplayed(this.by);
                }
                else
                {
                    AppBase.WaitForElementToBeNotDisplayed(this.element);
                }
                // Logging - After action success
                Log.Success();
            }
            catch (Exception e)
            {
                // Logging - After action exception
                Log.Failure(e.Message);
                // Fail current test
                Assert.Fail(e.Message
                            + Environment.NewLine
                            + "webElement.description : " + this.description
                            + Environment.NewLine
                            + "webElement.by" + this.by
                            );
            }
            finally
            {
                // Logging - After action
                Log.Finally();
            }
        }
Example #3
0
        /// <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();
            }
        }
Example #4
0
        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);
            }
        }