/*---------------------------------------------------------------------------- * %%Function:FSetSelectControlValue * %%Qualified:ArbWeb.ArbWebControl_Selenium.FSetSelectControlValue * ----------------------------------------------------------------------------*/ public static bool FSetSelectedOptionValueForControlName(IWebDriver driver, IStatusReporter srpt, string sName, string sValue) { srpt.LogData($"FSetSelectControlValue for name {sName}", 5, MSGT.Body); SelectElement select = new SelectElement(driver.FindElement(By.Name(sName))); string sOriginal = select.SelectedOption.GetProperty("value"); bool fChanged = false; if (String.Compare(sOriginal, sValue, true) != 0) { try { select.SelectByValue(sValue); } catch { return(false); } fChanged = true; } srpt.LogData($"Return: {fChanged}", 5, MSGT.Body); return(fChanged); }
/*---------------------------------------------------------------------------- * %%Function:FSetSelectControlText * %%Qualified:ArbWeb.ArbWebControl_Selenium.FSetSelectControlText * ----------------------------------------------------------------------------*/ public static bool FSetSelectedOptionTextForControlId(IWebDriver driver, IStatusReporter srpt, string sid, string sValue) { srpt.LogData($"FSetSelectControlText for id {sid}", 5, MSGT.Body); SelectElement select = new SelectElement(driver.FindElement(By.Id(sid))); string sOriginal = select.SelectedOption.Text; bool fChanged = false; if (String.Compare(sOriginal, sValue, true) != 0) { select.SelectByText(sValue); fChanged = true; } srpt.LogData($"Return: {fChanged}", 5, MSGT.Body); return(fChanged); }
/*---------------------------------------------------------------------------- * %%Function:GetOptionsTextValueMappingFromControlId * %%Qualified:ArbWeb.ArbWebControl_Selenium.GetOptionsTextValueMappingFromControlId * ----------------------------------------------------------------------------*/ public static Dictionary <string, string> GetOptionsTextValueMappingFromControlId(IWebDriver driver, IStatusReporter srpt, string sid) { MicroTimer timer = new MicroTimer(); Dictionary <string, string> mp = GetOptionsTextValueMappingFromControl(driver.FindElement(By.Id(sid)), srpt); timer.Stop(); srpt.LogData($"GetOptionsTextValueMappingFromControlId({sid}) elapsed: {timer.MsecFloat}", 1, MSGT.Body); return(mp); }
/*---------------------------------------------------------------------------- * %%Function:WaitForPageLoad * %%Qualified:ArbWeb.ArbWebControl_Selenium.WaitForPageLoad * ----------------------------------------------------------------------------*/ public static void WaitForPageLoad(IStatusReporter srpt, IWebDriver driver, int maxWaitTimeInSeconds) { MicroTimer timer = new MicroTimer(); string state = string.Empty; try { WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(maxWaitTimeInSeconds)); //Checks every 500 ms whether predicate returns true if returns exit otherwise keep trying till it returns ture wait.Until(d => { // use d instead of driver below? try { state = ((IJavaScriptExecutor)driver).ExecuteScript(@"return document.readyState").ToString(); } catch (InvalidOperationException) { //Ignore } catch (NoSuchWindowException) { //when popup is closed, switch to last windows driver.SwitchTo().Window(driver.WindowHandles[driver.WindowHandles.Count - 1]); } //In IE7 there are chances we may get state as loaded instead of complete return(state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase)); }); } catch (TimeoutException) { //sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase)) { throw; } } catch (NullReferenceException) { //sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase)) { throw; } } catch (WebDriverException) { if (driver.WindowHandles.Count == 1) { driver.SwitchTo().Window(driver.WindowHandles[0]); } state = ((IJavaScriptExecutor)driver).ExecuteScript(@"return document.readyState").ToString(); if (!(state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase))) { throw; } } timer.Stop(); srpt.LogData($"WaitForPageLoad elapsed: {timer.MsecFloat}", 1, MSGT.Body); }