Beispiel #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);
            }
        }
Beispiel #2
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)
        {
            // Log Before Action
            Log.BeforeAction(new OrderedDictionary()
            {
                { "Given URL", url }
            });

            // 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();
            }
            catch (Exception e)
            {
                // Logging - After action exception
                Log.Failure(e.Message);
                // Fail current test
                Assert.Fail(e.Message);
            }
            finally
            {
                // Logging - After action
                Log.Finally();
            }
        }