public static void CheckTheCheckbox(this IWebElement element, bool isWantChecked) { if (isWantChecked) { if (!element.Selected) element.Click(); } else { if (element.Selected) element.Click(); } }
public static void ClickAndWait(this IWebElement element, double timeoutInSeconds) { if (!(timeoutInSeconds > 0)) return; var timeout = timeoutInSeconds * 1000; element.Click(); Thread.Sleep(Convert.ToInt32(timeout)); }
public static void ClickLink(this IWebElement link) { if (link == null) throw new ArgumentNullException("link"); bool exceptionCaught; do { exceptionCaught = false; try { // sometimes, clicking a button or hyperlink does not work in IE / Chrome // solution is to click only for FF, and use enter key for IE / Chrome if (link.Browser().IsInternetExplorer() || link.Browser().IsFirefox()) { link.SendKeys(Keys.Enter); } else { link.Click(); } } catch (WebDriverException) { exceptionCaught = true; } } while (exceptionCaught); }
public static void ClickButton(this IWebElement button) { if (button == null) throw new ArgumentNullException("button"); bool exceptionCaught; do { exceptionCaught = false; try { // sometimes, clicking a button or hyperlink does not work in IE / Chrome // solution is to click only for FF, and use enter key for IE / Chrome if (!button.Browser().IsInternetExplorer()) { 200.WaitThisManyMilleseconds(); button.Click(); } else { button.SendKeys(Keys.Enter); } } catch (WebDriverException) { exceptionCaught = true; } } while (exceptionCaught); }
public static IWebElement ClickAndWait(this IWebElement element, TimeSpan? time = null) { time = time ?? TimeSpan.FromSeconds(1); element.Click(); Thread.Sleep(time.Value); return element; }
public static void WaitUntilElementBecomeClickableAndClick(this IWebElement element) { var driver = TestEnvironment.Instance.WebDriver; var wait = new WebDriverWait(driver, new TimeSpan(0, 0, TestEnvironment.Instance.WaitLongRequestTimeout)); wait.Until(ExpectedConditions.ElementToBeClickable(element)); element.Click(); }
/// <summary> /// Clicks the link. /// </summary> /// <param name="me">Me.</param> /// <param name="subDomain">Sub domain.</param> static void ClickLink(this IWebElement me, string subDomain = null, bool addParams = false) { // // We check the href on me and modify it before clicking (ie. www.google.com -> stage.google.com). // string href = me.GetAttribute("href"); Console.WriteLine("ClickLink before '{0}'", href); if (subDomain != null) { if (href != null) { Uri uri = new Uri(href); string oldSubDomain = uri.Host.Split('.')[0]; string[] notReplaceSubDomains = new string[] { "secure" }; if (!href.Contains("#") && !(oldSubDomain.Equals(subDomain)) && !notReplaceSubDomains.Contains(oldSubDomain)) { Console.WriteLine("Replacing subdomain '{0}' with '{1}'", oldSubDomain, subDomain); string newHref = href.Replace(oldSubDomain, subDomain); if (addParams) { newHref = HttpExtensions.AddQuery(newHref, "param", "value"); } IWebDriver driver = Factory.Instance; IJavaScriptExecutor js = ((IJavaScriptExecutor)Factory.Instance); js.ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2]);", me, "href", newHref); } } } href = me.GetAttribute("href"); Console.WriteLine("ClickLink after '{0}'", href); me.Click(); }
public static void click(this IWebElement field) { if (!field.Enabled && !field.Displayed) { throw new ElementNotVisibleException("The element passed is not displayed/enabled"); } field.Click(); }
public static void LogOn(this INativeActionSyntaxProvider I, string userName, string password) { I.Open(UrlHelper.BaseUrl + "users/account/LogOn"); I.Expect.Url(x => x.LocalPath.Contains("LogOn")); I.Enter(EnvironmentSettings.TestAccountName).In("#SignIn_UserNameOrEmail"); I.Enter(EnvironmentSettings.TestAccountPassword).In("#SignIn_Password"); I.Click("#signin-link"); }
public static Link click(this Link link, int miliseconds) { if (link != null) { link.Click(); miliseconds.sleep(); } return link; }
public static IWebElement BrowserSpecificCheck(this IWebElement element, IWebDriver webDriver) { if (webDriver is InternetExplorerDriver) { element.SendKeys(Keys.Space); } else { element.Click(); } return element; }
/// <summary> /// Uses jquery to trigger a mouseover event. Then clicks the submenu link. /// </summary> /// <param name="cssLocator">Css locator of the parent menu.</param> public static void ClickSubMenu(this IWebElement element, string cssLocator) { var script = String.Format(@"$('{0}').trigger('mouseover');", cssLocator); IJavaScriptExecutor js = (IJavaScriptExecutor)Browser.Driver; js.ExecuteScript(script); element.Click(); script = String.Format(@"$('{0}').trigger('mouseout');", cssLocator); js.ExecuteScript(script); }
public static void UploadPackageUsingUI(this INativeActionSyntaxProvider I, string fullPackagePath) { // Navigate to the Upload Package page. This will fail if the user never uploaded the previous package, hence the error handling. I.Open(String.Format(UrlHelper.UploadPageUrl)); try { I.Expect.Url(x => x.AbsoluteUri.Contains("/packages/Upload")); } catch { I.Click("a[class='cancel']"); } // Upload the package. I.Click("input[name='UploadFile']"); I.Wait(5); I.Type(fullPackagePath); I.Press("{ENTER}"); I.Wait(5); I.Click("input[value='Upload']"); }
/// <summary> /// Click on the element using Actions API. 'Click' method is known to not work in same strange cases on Edge, /// Use this method instead /// </summary> public static void ClickUsingMouse(this IWebElement element, IWebDriver driver) { ICapabilities capabilities = ((RemoteWebDriver) driver).Capabilities; if (capabilities.BrowserName == "Firefox") { element.Click(); } else { new Actions(driver).Click(element).Build().Perform(); //does not work with Firefox 48, Selenium 3.0.0-beta2, GeckoDriver.exe 0.10.0 } }
public static void CostCenterComboBoxSelector(this ComboBox comboBox, string elementToSelect) { if (comboBox != null) { comboBox.Click(); comboBox.Select(elementToSelect); Console.WriteLine(elementToSelect + " Selected"); } else { Console.WriteLine(elementToSelect + " not found"); } }
public static bool ClickSafe(this IWebElement webElement) { try { webElement.Click(); } catch { return false; } return true; }
public static void Click(this IWebElement element, int timeoutInSeconds) { if (timeoutInSeconds > 0) { var cnt = 0; do { try { element.Click(); return; } catch { Thread.Sleep(100); cnt += 100; } } while (cnt < (timeoutInSeconds * 1000)); } else { element.Click(); } }
public static void SupperClick(this IWebElement selectContainer) { var needClick = true; while (needClick) { try { selectContainer.Click(); needClick = false; } finally { Thread.Sleep(100); } } }
public static void ScrollElementAndClick(this IWebElement element) { Thread.Sleep(500); JavaScriptExecutorHelper.ExecuteScript("window.scrollTo(0," + element.Location.Y + ");"); element.Click(); }
public static void ExtendedMouseClick(this HtmlControl control) { if(control.OwnerBrowser.BrowserType != BrowserType.Chrome) { control.MouseClick(); } else { control.Focus(); control.Click(); } }
public static void ClickOn(this IWebElement element) { element.Click(); }
/// <summary> /// Click on a desired element within a document (with reference to Internet Explorer). /// </summary> /// <param name="element"></param> /// <param name="browser">Current webdriver instance.</param> public static void ForceClick(this IWebElement element, IWebDriver browser) { if (typeof(InternetExplorerDriver) == browser.GetType()) { element.SendKeys(Keys.Enter); } else { element.Click(); } }
private static void ClickRadioOrCheckBox(this IWebElement webElement) { if (webElement == null) throw new ArgumentNullException("webElement"); bool exceptionCaught; do { exceptionCaught = false; try { webElement.Click(); } catch (WebDriverException) { exceptionCaught = true; } } while (exceptionCaught); }
/// <summary> /// Clicks on a link that is expected to navigate to a new URL, returning /// the HTTP status code of the navigation. /// </summary> /// <param name="element">The element clicked on to perform the navigation.</param> /// <param name="timeout">A <see cref="TimeSpan"/> structure for the time out of the navigation.</param> /// <param name="printDebugInfo"><see langword="true"/> to print debugging information to the console; /// otherwise, <see langword="false"/>.</param> /// <returns>The HTTP status code of the navigation.</returns> public static int ClickNavigate(this IWebElement element, TimeSpan timeout, bool printDebugInfo) { if (element == null) { throw new ArgumentNullException("element", "Element cannot be null."); } int responseCode = 0; string targetUrl = string.Empty; SessionStateHandler responseHandler = delegate(Session targetSession) { // For the first session of the click, the URL should be the URL // requested by the the element click. if (string.IsNullOrEmpty(targetUrl)) { targetUrl = targetSession.fullUrl; if (printDebugInfo) { Console.WriteLine("DEBUG: Element click navigating to {0}", targetUrl); } } // This algorithm could be much more sophisticated based on your needs. // In our case, we'll only look for responses where the content type is // HTML, and that the URL of the session matches our current target URL // Note that we also only set the response code if it's not already been // set. if (targetSession.oResponse["Content-Type"].Contains("text/html") && targetSession.fullUrl == targetUrl && responseCode == 0) { // If the response code is a redirect, get the URL of the redirect, // so that we can look for the next response from the session for that // URL. if (targetSession.responseCode >= 300 && targetSession.responseCode < 400) { targetUrl = targetSession.GetRedirectTargetURL(); if (printDebugInfo) { Console.WriteLine("DEBUG: Navigation redirected with code of {0} from {1} to {2}", targetSession.responseCode, targetSession.fullUrl, targetUrl); } } else { responseCode = targetSession.responseCode; if (printDebugInfo) { Console.WriteLine("DEBUG: Got final status code of {0} for URL {1}", targetSession.responseCode, targetUrl); } } } }; // Attach the event handler, click the element, and wait for the // status code to be non-zero, or to timeout. Then detach the // event handler and return the response code. // Note that we're using the ResponseHeadersAvailable event so // as to avoid a race condition with the browser (per Eric // Lawrence). FiddlerApplication.ResponseHeadersAvailable += responseHandler; DateTime endTime = DateTime.Now.Add(timeout); targetUrl = element.GetAttribute("href"); element.Click(); while (responseCode == 0 && DateTime.Now < endTime) { System.Threading.Thread.Sleep(100); } FiddlerApplication.ResponseHeadersAvailable -= responseHandler; return responseCode; }
/// <summary> /// Tries to click an element and if any error occures returns false /// Else returns true /// </summary> /// <param name="element"></param> /// <returns></returns> public static bool TrySelect(this IWebElement element) { try { element.Click(); } catch (System.Reflection.TargetInvocationException) { return false; } catch (NoSuchElementException) { return false; } return true; }
public static void ClickAutoCompleteItem(this IWebElement autoCompleteItem) { if (autoCompleteItem == null) throw new ArgumentNullException("autoCompleteItem"); while (autoCompleteItem.Displayed) { bool exceptionCaught; do { exceptionCaught = false; try { // IE requires multiple clicks before // autocomplete dropdown item disappears autoCompleteItem.Click(); } catch (WebDriverException) { exceptionCaught = true; } } while (exceptionCaught); } }
public static ISelenium ClickAndWait(this ISelenium selenium, string locator) { selenium.Click(locator); selenium.WaitForPageToLoad(PageLoadTimeout.ToString()); return selenium; }
public static void ClickWithWait(this IWebElement element) { element.Click(); Thread.Sleep(250); }
public static void SetCheckbox(this PurpleCheckBox box, bool isChecked) { if (box.Checked != isChecked) box.Click(); }
public static void Type(this IWebElement e, string text) { e.Click(); e.Clear(); e.SendKeys(text); }