コード例 #1
0
        /// Injects a link on the current page and then clicks on that link (which opens a new tab).
        /// </summary>
        public static void OpenUrlInNewTab(string url)
        {
            // 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 + "AppBase.OpenUrlInNewTab(url)");
            sb.AppendLine(logPadding.InfoPadding + "[PARAM] URL: " + url);
            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
            {
                // Inject the link
                string      script          = "var d=document,a=d.createElement('a');a.target='_blank';a.href='" + url + "';a.innerHTML='.';d.body.appendChild(a);return a;";
                IWebElement returnedElement = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(script);
                // Save the reference to the injected element
                WebElement element = new WebElement(returnedElement);
                // Click the link
                element.ClickViaJavaScript();
                // Switch to the new tab
                driver.SwitchTo().Window(driver.WindowHandles.Last());
                // 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);
            }
        }
コード例 #2
0
        /// <summary>
        /// Waits (until the timeout) for the current URL to contain the given string.
        /// </summary>
        /// <param name="driver">The IWebDriver under test</param>
        /// <param name="text">The string to look for</param>
        public static void WaitForUrlToContain(string text)
        {
            // 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 + "AppBase.WaitForUrlToContain(text)");
            sb.AppendLine(logPadding.InfoPadding + "[INFO] Text to look for: " + text);
            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
            {
                // Setup the wait
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(TestBase.defaultTimeoutInSeconds));
                // Wait until condition is met
                wait.Until <Func <IWebDriver, bool> >(
                    d => {
                    return((driver) => { return driver.Url.ToLowerInvariant().Contains(text.ToLowerInvariant()); });
                }
                    );
                // 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);
            }
        }
コード例 #3
0
        /// <summary>
        /// Intantiates a WebDriver (creating a session) using the current App.config.
        /// </summary>
        public static AppiumDriver <AppiumWebElement> Create()
        {
            #region Properties

            // Get settings from the App.config
            string app            = ConfigurationManager.AppSettings["app"];
            string appConfig      = ConfigurationManager.AppSettings["appConfig"];
            string automationName = ConfigurationManager.AppSettings["automationName"];
            string deviceName     = ConfigurationManager.AppSettings["deviceName"];
            Uri    hubUri         = new Uri(ConfigurationManager.AppSettings["hubUri"]);
            string platformName   = ConfigurationManager.AppSettings["platformName"];

            #endregion Properties

            // Declare a return value
            AppiumDriver <AppiumWebElement> returnValue = null;
            // 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 + "Session.Create()");
            sb.AppendLine(logPadding.InfoPadding + "[INFO] Automation Name: " + automationName);
            sb.AppendLine(logPadding.InfoPadding + "[INFO] Platform Name: " + platformName);
            sb.AppendLine(logPadding.InfoPadding + "[INFO] Device Name: " + deviceName);
            sb.AppendLine(logPadding.InfoPadding + "[INFO] App: " + app);
            Log.Write(sb.ToString());
            // Perform the action
            try
            {
                // Define the common "Desired Capabilities"
                DesiredCapabilities capabilities = new DesiredCapabilities();
                if (automationName.Length > 0)
                {
                    capabilities.SetCapability("automationName", automationName);
                }
                if (platformName.Length > 0)
                {
                    capabilities.SetCapability("platformName", platformName);
                }
                if (deviceName.Length > 0)
                {
                    capabilities.SetCapability("deviceName", deviceName);
                }
                if (app.Length > 0)
                {
                    capabilities.SetCapability("app", app);
                }
                capabilities.SetCapability("noReset", true);
                // Initialize the driver
                if (platformName == "Andorid")
                {
                    returnValue = new AndroidDriver <AppiumWebElement>(hubUri, capabilities, TimeSpan.FromMinutes(TestBase.sessionTimeoutInMinutes));
                }
                else if (platformName == "iOS")
                {
                    returnValue = new IOSDriver <AppiumWebElement>(hubUri, capabilities, TimeSpan.FromMinutes(TestBase.sessionTimeoutInMinutes));
                }
                else
                {
                    throw new NotImplementedException("Platform '" + platformName + "' not setup in Session.Create();");
                }
                // Set the global driver variable
                driver = returnValue;
                // Logging - After action success
                Log.Success(logPadding.Padding);
                Log.WriteLine(logPadding.InfoPadding + "[INFO] Created a new session.");
            }
            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);
            }
            // Return the return value
            return(returnValue);
        }
コード例 #4
0
        /// <summary>
        /// Intantiates a WebDriver (creating a session) using the current App.config.
        /// </summary>
        public static IWebDriver Create()
        {
            #region Properties

            // Define the current App.config (from the App.config)
            string appConfig = ConfigurationManager.AppSettings["appConfig"];
            // Declare a string for the Base URL
            string baseUrl = ConfigurationManager.AppSettings["baseUrl"];
            // Define the Browser (from the App.config)
            string browser = ConfigurationManager.AppSettings["browser"] ?? "chrome";
            // Define the Grid Hub URI (from the App.config)
            Uri hubUri = new Uri(ConfigurationManager.AppSettings["hubUri"] ?? "http://*****:*****@"--disable-extensions");
                    // Use Remote Chrome
                    try
                    {
                        // Create a remote session using the Grid Hub URI (from the App.config)
                        returnValue = new RemoteWebDriver(hubUri, options);
                    }
                    // Use Local Chrome
                    catch
                    {
                        // Write result to log
                        Log.WriteLine(logPadding.InfoPadding + "[WARNING] Unable to create a remote session, so creating a local one.");
                        // Create a local session
                        returnValue = new ChromeDriver(options);
                    }
                }
                else if (browser.Equals("edge"))
                {
                    // Create an options object to specify command line arguments for the Edge web driver
                    EdgeOptions options = new EdgeOptions();
                    // Use Remote Edge
                    try
                    {
                        // Create a remote session using the Grid Hub URI (from the App.config)
                        returnValue = new RemoteWebDriver(hubUri, options);
                    }
                    // Use Local Edge
                    catch
                    {
                        // Write result to log
                        Log.WriteLine(logPadding.InfoPadding + "[WARNING] Unable to create a remote session, so creating a local one.");
                        // Create a local session
                        returnValue = new EdgeDriver(options);
                    }
                }
                else if (browser.Equals("firefox"))
                {
                    // Create an options object to specify command line arguments for the Firefox web driver
                    FirefoxOptions options = new FirefoxOptions();
                    // Use Remote Firefox
                    try
                    {
                        // Create a remote session using the Grid Hub URI (from the App.config)
                        returnValue = new RemoteWebDriver(hubUri, options);
                    }
                    // Use Local Firefox
                    catch
                    {
                        // Write result to log
                        Log.WriteLine(logPadding.InfoPadding + "[WARNING] Unable to create a remote session, so creating a local one.");
                        try
                        {
                            // Create a local session
                            returnValue = new FirefoxDriver(options);
                        }
                        catch
                        {
                            // Write result to log
                            Log.WriteLine(logPadding.InfoPadding + "[WARNING] Unable to create a local session using the default location.");
                            // Load Firefox From Specific Location
                            options.BrowserExecutableLocation = @"C:\Program Files\Mozilla Firefox\firefox.exe";
                            Log.WriteLine(logPadding.InfoPadding + "[INFO] Browser Executable Location: " + options.BrowserExecutableLocation);
                            // Create a local session
                            returnValue = new FirefoxDriver(options);
                        }
                    }
                }
                else if (browser.Equals("safari"))
                {
                    // Create an options object to specify command line arguments for the Safari web driver
                    SafariOptions options = new SafariOptions();
                    // Create a remote session using the Grid Hub URI (from the App.config)
                    returnValue = new RemoteWebDriver(hubUri, options);
                }
                else
                {
                    throw new NotImplementedException(browser + " not handled in Session.Create() method.");
                }

                // Save a reference to the current Test's driver in its TestContext
                TestContext.Set("driver", returnValue);
                // Save a reference to the current Test's base url in its TestContext
                TestContext.Set("baseUrl", baseUrl);
                // Save a reference to the current Test's screenshot folder in its TestContext
                TestContext.Set("screenshotFolder", screenshotFolder);

                // Logging - After action
                Log.Success(logPadding.Padding);
                Log.Finally(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);
            }
            // Return the return value
            return(returnValue);
        }
コード例 #5
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);
            }
        }