Example #1
0
        public void TestSearch_SearchQueries_ScreenshotsCreated()
        {
            // Get titles.
            string[] titles = GetTitlesFromResponseDump();

            // Get random titles or range of titles from collection.
            List <string> searchQueries = (RandomRegimeOn) ? GetRandomTitles(titles) : GetRangeOfTitles(titles);

            // Click on the Search button and wait until it expands.
            vcPageObj.SearchButton.Click();
            Thread.Sleep(WaitSearchMenuExpandingMs);

            action = new ActionsExtension(Driver);

            // Regular expression to remove invalid filename characters of screenshots.
            string invalidFileNameChars = new string(Path.GetInvalidFileNameChars());
            Regex  regex = new Regex(string.Format("[{0}]", Regex.Escape(invalidFileNameChars)));

            // For alert handling.
            IAlert alert         = null;
            bool   alertIsActive = false;

            // Enter search queries and click on them.
            foreach (string query in searchQueries)
            {
                alertIsActive = false;

                vcPageObj.EnterSearchQuery(query);
                Thread.Sleep(WaitResultsMs);

                // Move mouse arrow to result and click on it.
                WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromMilliseconds(WaitTimeOutMs));

                try
                {
                    IWebElement result = wait.Until <IWebElement>((d) =>
                    {
                        // Return dynamically created search result.
                        return(Driver.FindElement(By.XPath(SearchResultXPath)));
                    });

                    action.Click(result).Perform();
                    vcPageObj.WaitAnimation();
                }
                catch (TimeoutException)
                {
                    Console.WriteLine("NOT FOUND: " + query);
                }

                // Handling of alert messages.
                try
                {
                    alert         = Driver.SwitchTo().Alert();
                    alertIsActive = true;
                    alert.Accept();
                    Console.WriteLine("ALERT: " + query);
                }
                catch (NoAlertPresentException)
                {
                    Console.WriteLine("OK: " + query);
                }

                // Save screenshot of search result. If alert had appeared, then add special string to filename of screenshot.
                // NOTE: Uses user environment variable SELENIUM_SCR.
                if (alertIsActive)
                {
                    WebDriverScreenshotMaker.SaveScreenshot(Driver, "TestSearch", alertString + regex.Replace(query, ""), ImageFormat.Png, true);
                }
                else
                {
                    WebDriverScreenshotMaker.SaveScreenshot(Driver, "TestSearch", regex.Replace(query, ""), ImageFormat.Png, true);
                }

                action.SetDefault();
                vcPageObj.ClearSearchTextBox();
            }
        }