コード例 #1
0
 public SpecialClickData(SpecialClick sc)
 {
     SpecialClick = sc;
 }
コード例 #2
0
        Dictionary <int, SpecialClickData> ClickResults(WebDriver driver)
        {
            string currentTitle        = driver.Title;
            string currentWindowHandle = driver.CurrentWindowHandle;

            // http://stackoverflow.com/questions/24120263/find-element-in-selenium-using-xpath-or-css-selector
            // <div class="rc" data-hveid="128"><h3 class="r"><a href="http://schoolofdents.com/"
            ReadOnlyCollection <IWebElement>   resultLinks = driver.FindElements(ClickOnElementAt);
            Dictionary <int, SpecialClickData> dict_resultId_specialClick = new Dictionary <int, SpecialClickData>();

            int resultId = 0;

            foreach (IWebElement resultLink in resultLinks)
            {
                string href = resultLink.GetAttribute("href");

                // TODO: Determine if the case where there are more than one special click matches - if it's valid, and should be handled
                foreach (SpecialClick specialClick in SpecialClicks)
                {
                    // If user provided a regex and it matches, or the provided Url is a part of it, save the special click info
                    if (Regex.IsMatch(href, specialClick.Url) || href.Contains(specialClick.Url))
                    {
                        dict_resultId_specialClick.Add(resultId, new SpecialClickData(specialClick));
                    }
                }
                resultId++;
            }

            // Go through all of the special clicks first (so that if they open a new window)...
            //      they open it all at once and the last one will be the one used for the regular link and back clicking
            foreach (KeyValuePair <int, SpecialClickData> kv in dict_resultId_specialClick)
            {
                IWebElement      resultLink       = resultLinks[kv.Key];
                SpecialClickData specialClickData = kv.Value;
                SpecialClick     specialClick     = specialClickData.SpecialClick;

                specialClick.Run(driver, resultLink);
                specialClickData.WindowHandle      = driver.WindowHandles.Last();
                specialClickData.WindowCreatedTime = DateTime.Now;
                specialClickData.WindowTitle       = driver.Title;

                // If the special click uses the same window... there might be special wait condition
                //      wait for the specified amount of time, then click back button, and refresh the resultLinks
                if (specialClick.Window == WindowType.Same)
                {
                    resultLinks = WaitForTimeoutThenBackBtn(driver, specialClick.WaitMs, currentTitle);
                }
                else
                {
                    // Switch to the new window/tab/etc.
                    driver.SwitchToWindowHandle(specialClickData.WindowHandle);
                    driver.MinimizeWindowByTitle(driver.Title);
                    specialClickData.WindowTitle = driver.Title;

                    // Switch to the existing window
                    driver.SwitchToWindowHandle(currentWindowHandle);
                    resultLinks = driver.FindElements(ClickOnElementAt);
                }
            }

            int nonSpecialClickCount = 5; // Default, only 5 clicks allowed

#if FULL_VERSION
            nonSpecialClickCount = resultLinks.Count; // Full version, number of clicks follow number of links
#endif
            // Only do the non-special clicks (should be same window)
            for (resultId = 0; resultId < nonSpecialClickCount; resultId++)
            {
                // If the result id is a special click, ignore it
                if (dict_resultId_specialClick.Keys.Contains(resultId))
                {
                    continue;
                }

                IWebElement resultLink = resultLinks[resultId];
                // Is not a special click so we can just click it, wait for ms, then back
                resultLink.Click();
                resultLinks = WaitForTimeoutThenBackBtn(driver, this.WaitMs, currentTitle);
            }

            return(dict_resultId_specialClick);
        }