/// <summary> /// Wait until the page element is displayed /// Resutn the selected page element /// </summary> /// <param name="browser"></param> /// <param name="searchType"></param> /// <param name="searchString"></param> /// <param name="retrys"></param> /// <returns>IWebElement element /// element = Displayed and Enabled /// </returns> public static IWebElement GetPageElement(IWebDriver browser, RunTimeVars.ELEMENTSEARCH searchType, string searchString, int retrys) { var controlWaitTime = retrys; IWebElement element = null; while (controlWaitTime > 0) { try { if (searchType == RunTimeVars.ELEMENTSEARCH.ClassName) { element = browser.FindElement(By.ClassName(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.CssSelector) { element = browser.FindElement(By.CssSelector(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.ID) { element = browser.FindElement(By.Id(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.LinkText) { element = browser.FindElement(By.LinkText(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.PartialLinkText) { element = browser.FindElement(By.PartialLinkText(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.NAME) { element = browser.FindElement(By.Name(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.TagName) { element = browser.FindElement(By.TagName(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.XPATH) { element = browser.FindElement(By.XPath(searchString)); } if (element.Displayed && element.Enabled) { break; } else { //if(the current page not done loading System.Threading.Thread.Sleep(1 * 1000); //Wait 1-sec controlWaitTime--; } } catch { //if (the cuurrent page status is not avaiable yet System.Threading.Thread.Sleep(1 * 1000); //Wait 1-sec controlWaitTime--; } } //Check if(Page displayed <= controlWaitTime) if (controlWaitTime <= 0) { throw new Exception("Web Page Search-" + element + " Was Not Found"); } return(element); }
//******************************* End Fluent Wait Section ******************************************** /// <summary> /// Is Page Element Displayed with NO Timeout Parameter /// </summary> /// <param name="browser"></param> /// <param name="searchType"></param> /// <param name="searchString"></param> /// <returns></returns> public static bool IsPageElementDisplayed(IWebDriver browser, RunTimeVars.ELEMENTSEARCH searchType, string searchString) { var isFound = false; IWebElement element = null; try { if (searchType == RunTimeVars.ELEMENTSEARCH.ClassName) { element = browser.FindElement(By.ClassName(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.CssSelector) { element = browser.FindElement(By.CssSelector(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.ID) { element = browser.FindElement(By.Id(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.LinkText) { element = browser.FindElement(By.LinkText(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.PartialLinkText) { element = browser.FindElement(By.PartialLinkText(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.NAME) { element = browser.FindElement(By.Name(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.TagName) { element = browser.FindElement(By.TagName(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.XPATH) { element = browser.FindElement(By.XPath(searchString)); } if (element.Displayed) { isFound = true; } else { isFound = false; } } catch { isFound = false; } return(isFound); }
// FluenttWait Wait and Find element // This method behavior is exactly the same as the Explicit Wait Method /// set the browser Implict wait time = Explict wait time * 2 ///to prevent browser nosuchelement exception being displayed before the Impliict wait time expires /// <summary> /// Is Page Element Displayed with a Timeout Parameter /// </summary> /// <param name="browser"></param> /// <param name="searchType"></param> /// <param name="searchString"></param> /// <param name="seconds"></param> /// <returns></returns> public static bool FluenttWaitIsPageElementDisplayed(IWebDriver browser, RunTimeVars.ELEMENTSEARCH searchType, string searchString, int seconds) { //Date - 12/21/18 Can't read current browser.ImplicitWait time, get not implemented //set the browser Implict wait time = Explict wait time * 2 //to prevent Explict waits nosuchelement exception being displayed before the Impliict wait time expires browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(seconds * 2); //Fluent wait initialize DefaultWait <IWebDriver> fluentWait = new DefaultWait <IWebDriver>(browser); fluentWait.Timeout = TimeSpan.FromSeconds(seconds); fluentWait.PollingInterval = TimeSpan.FromMilliseconds(500); fluentWait.IgnoreExceptionTypes(typeof(NoSuchElementException)); var isFound = false; IWebElement element = null; try { if (searchType == RunTimeVars.ELEMENTSEARCH.ClassName) { element = fluentWait.Until(x => x.FindElement(By.ClassName(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.CssSelector) { element = fluentWait.Until(x => x.FindElement(By.CssSelector(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.ID) { element = fluentWait.Until(x => x.FindElement(By.Id(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.LinkText) { element = fluentWait.Until(x => x.FindElement(By.LinkText(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.PartialLinkText) { element = fluentWait.Until(x => x.FindElement(By.PartialLinkText(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.NAME) { element = fluentWait.Until(x => x.FindElement(By.Name(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.TagName) { element = fluentWait.Until(x => x.FindElement(By.TagName(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.XPATH) { element = fluentWait.Until(x => x.FindElement(By.XPath(searchString))); } //include this in the try section if (element.Displayed) { isFound = true; } else { isFound = false; } } catch { isFound = false; } //restore the browser Implict wait time = initial wait time browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(RunTimeVars.WEBDRIVERWAIT); return(isFound); }
/// <summary> /// Wait until the page element is displayed /// Return the selected page element /// /// set the browser Implict wait time = Explict wait time * 2 ///to prevent browser nosuchelement exception being displayed before the Impliict wait time expires /// // Modify the method to accept an optional parameter // passed in Boolean clickElement = false // if (passed in method Boolean clickElement = true /// then if(element is found) thne click the element /// </summary> /// <param name="browser"></param> /// <param name="searchType"></param> /// <param name="searchString"></param> /// <param name="seconds"></param> /// <returns>IWebElement element /// element = Displayed and Enabled /// </returns> public static IWebElement ExplicitWaitGetPageElement(IWebDriver browser, RunTimeVars.ELEMENTSEARCH searchType, string searchString, int seconds, Boolean clickElement = false) { //Date - 12/21/18 Can't read current browser.ImplicitWait time, get not implemented //set the browser Implict wait time = Explict wait time * 2 //to prevent Explict waits nosuchelement exception being displayed before the Impliict wait time expires browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(seconds * 2); //Explict waits WebDriverWait wait = new WebDriverWait(browser, TimeSpan.FromSeconds(seconds)); IWebElement element = null; try { if (searchType == RunTimeVars.ELEMENTSEARCH.ClassName) { element = wait.Until <IWebElement>(d => d.FindElement(By.ClassName(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.CssSelector) { element = wait.Until <IWebElement>(d => d.FindElement(By.CssSelector(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.ID) { element = wait.Until <IWebElement>(d => d.FindElement(By.Id(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.LinkText) { element = wait.Until <IWebElement>(d => d.FindElement(By.LinkText(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.PartialLinkText) { element = wait.Until <IWebElement>(d => d.FindElement(By.PartialLinkText(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.NAME) { element = wait.Until <IWebElement>(d => d.FindElement(By.Name(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.TagName) { element = wait.Until <IWebElement>(d => d.FindElement(By.TagName(searchString))); } if (searchType == RunTimeVars.ELEMENTSEARCH.XPATH) { element = wait.Until <IWebElement>(d => d.FindElement(By.XPath(searchString))); } //include this in the try section //if (the element not Displayed And not Enabled) // then return element = null if (!(element.Displayed && element.Enabled)) { element = null; } } catch { element = null; } // Check the passed in optional parameter Boolean clickElement // passed in Boolean clickElement = true if (element == null) { throw new Exception("Web Page Search-" + "('" + searchString + "')" + " Was Not Found"); } else if (clickElement) { element.Click(); } return(element); }
// ImplicitWait Wait and Find element /// <summary> /// Wait until the page element is displayed /// Return the selected page element /// </summary> /// <param name="browser"></param> /// <param name="searchType"></param> /// <param name="searchString"></param> /// <param name="retrys"></param> /// <returns>IWebElement element /// element = Displayed and Enabled /// </returns> public static IWebElement ImplicitWaitGetPageElement(IWebDriver browser, RunTimeVars.ELEMENTSEARCH searchType, string searchString, int seconds, Boolean clickElement = false) { IWebElement element = null; //Divide the passed in time(seconds/2) to compensate for the 1-sec // System.Threading.Thread.Sleep(1 * 1000); //Wait 1-sec var controlWaitTime = seconds / 2; if (controlWaitTime == 0) { controlWaitTime = 1; } //Date - 12/21/18 Can't read current browser.ImplicitWait time, get not implemented //set the browser Implict wait time = 1-sec browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1); while (controlWaitTime > 0) { try { if (searchType == RunTimeVars.ELEMENTSEARCH.ClassName) { element = browser.FindElement(By.ClassName(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.CssSelector) { element = browser.FindElement(By.CssSelector(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.ID) { element = browser.FindElement(By.Id(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.LinkText) { element = browser.FindElement(By.LinkText(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.PartialLinkText) { element = browser.FindElement(By.PartialLinkText(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.NAME) { element = browser.FindElement(By.Name(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.TagName) { element = browser.FindElement(By.TagName(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.XPATH) { element = browser.FindElement(By.XPath(searchString)); } if (element.Displayed && element.Enabled) { break; } else { //if(the current page not done loading System.Threading.Thread.Sleep(1 * 1000); //Wait 1-sec controlWaitTime--; } } catch { //if (the cuurrent page status is not avaiable yet System.Threading.Thread.Sleep(1 * 1000); //Wait 1-sec controlWaitTime--; } } // Check the passed in optional parameter Boolean clickElement // passed in Boolean clickElement = true if (element == null) { throw new Exception("Web Page Search-" + "('" + searchString + "')" + " Was Not Found"); } else if (clickElement) { element.Click(); } return(element); }
//*********************************************************************************************** // Implicit Wait Section //*********************************************************************************************** //******************************* Begin Implicit Wait Wait Section ********************************************* // ImplicitWait Wait and Find element // /// Is Page Element Displayed with a Timeout Parameter // I tried uisng Explict waits //WebDriverWait wait = new WebDriverWait(browser, TimeSpan.FromSeconds(seconds)); //IWebElement myDynamicElement = wait.Until<IWebElement>(d => d.FindElement(By.ClassName(searchString))); //but there is no way to exclude nosuchelement exception // //so implement a user defined method that does 3-things // 1. reset the browser Implict wait time = 1-sec // 2. Search for the web element // 3. restore the browser Implict wait time = initial wait time public static bool ImplicitWaitIsPageElementDisplayed(IWebDriver browser, RunTimeVars.ELEMENTSEARCH searchType, string searchString, int seconds) { var isFound = false; IWebElement element = null; //Divide the passed in time(seconds/2) to compensate for the 1-sec // System.Threading.Thread.Sleep(1 * 1000); //Wait 1-sec var controlWaitTime = seconds / 2; if (controlWaitTime == 0) { controlWaitTime = 1; } //Date - 12/21/18 Can't read current browser.ImplicitWait time, get not implemented //set the browser Implict wait time = 1-sec browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1); while (controlWaitTime > 0) { try { if (searchType == RunTimeVars.ELEMENTSEARCH.ClassName) { element = browser.FindElement(By.ClassName(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.CssSelector) { element = browser.FindElement(By.CssSelector(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.ID) { element = browser.FindElement(By.Id(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.LinkText) { element = browser.FindElement(By.LinkText(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.PartialLinkText) { element = browser.FindElement(By.PartialLinkText(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.NAME) { element = browser.FindElement(By.Name(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.TagName) { element = browser.FindElement(By.TagName(searchString)); } if (searchType == RunTimeVars.ELEMENTSEARCH.XPATH) { element = browser.FindElement(By.XPath(searchString)); } if (element.Displayed) { isFound = true; break; } else { //if(the current page not done loading System.Threading.Thread.Sleep(1 * 1000); //Wait 1-sec controlWaitTime--; } } catch { //if (the cuurrent page status is not avaiable yet System.Threading.Thread.Sleep(1 * 1000); //Wait 1-sec controlWaitTime--; } } //Check if(Page displayed <= controlWaitTime) if (controlWaitTime <= 0) { isFound = false; } //restore the browser Implict wait time = initial wait time browser.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(RunTimeVars.WEBDRIVERWAIT); return(isFound); }