/// <summary> /// Selects a select tag option by pressing the DOWN key until the option is selected, starting at the top of the options. /// </summary> /// <param name="selecttag">Ranorex.SelectTag containing the option to select.</param> /// <param name="tagValueToSelect">Value of the option in the Ranorex.SelectTag.</param> private static void SelectTagValueWithKeyboard(Ranorex.SelectTag selecttag, string tagValueToSelect) { if (selecttag.TagValue == tagValueToSelect) { //Current tag value is already the value to select. return; } //Press Down key until current option is requested option, starting at the top option. Duration previousDefaultKeyPressTime = Keyboard.DefaultKeyPressTime; Keyboard.DefaultKeyPressTime = 10; selecttag.Focus(); Keyboard.Press("{HOME}"); IList <OptionTag> tagOptions = selecttag.Options; string lastSelectTagOptionValue = tagOptions[tagOptions.Count - 1].TagValue; bool tagOptionSelected = false; while (!tagOptionSelected) { if (selecttag.TagValue == tagValueToSelect) { tagOptionSelected = true; } else { if (selecttag.TagValue == lastSelectTagOptionValue) { //On last option so tag value not found. break; } //Give selecttag.TagValue time to update after changing the select tag option using the keyboard. const int timeOutMilliseconds = 5000; int timeOutRemainingMilliseconds = timeOutMilliseconds; int delayMilliseconds = 100; string previousTagValue = selecttag.TagValue; Keyboard.Press("{DOWN}"); while (selecttag.TagValue == previousTagValue) { Ranorex.Delay.Milliseconds(delayMilliseconds); timeOutRemainingMilliseconds -= delayMilliseconds; if (timeOutRemainingMilliseconds <= 0) { throw new Exception(string.Format("SelectTag {0} tag value did not change within {1} milliseconds after pressing DOWN key. Current selecttag.TagValue:{2}", selecttag.Name, timeOutMilliseconds.ToString(), selecttag.TagValue)); } } } } if (!tagOptionSelected) { throw new ArgumentException(string.Format("None of the selected SelectTag {0} options had a TagValue of {1}", selecttag.Name, tagValueToSelect), "tagValueToSelect"); } Keyboard.DefaultKeyPressTime = previousDefaultKeyPressTime; }
/// <summary> /// Clicks or sets a web page select tag option. /// </summary> /// <param name="pathToDOM">Ranorex.Core.Repository.RepoGenBaseFolder.BaseBath of a web page DOM.</param> /// <param name="selecttag">Ranorex.SelectTag of a web page select tag.</param> /// <param name="option">option to select in select tag.</param> public static void ChooseSelectTagOption(string pathToDOM, Ranorex.SelectTag selecttag, string option) { WebDocument domWebDocument = pathToDOM; string selectTagValueForOption = null; switch (domWebDocument.BrowserName) { case "Chrome": case "Safari": case "Mozilla": //Using Chrome and Safari select tag with mouse problematic. So select using keyboard. //For Mozilla, OptionTag.Visible does not return the correct value for an option tag that is not visible. So select using keyboard. selectTagValueForOption = FindSelectTagValueForOption(selecttag, option); SelectTagValueWithKeyboard(selecttag, selectTagValueForOption); break; case "IE": //Click option if visible after clicking select tag. String itemToClickRxPath = String.Format("/container[@caption='selectbox']/listitem[@accessiblename='{0}']", option); selecttag.Focus(); selecttag.Click(); ListItem itemToClick; try { itemToClick = itemToClickRxPath; } catch (Exception e) { throw new ArgumentException(string.Format("Unable to find option {0} in SelectTag {1}. Exception:{2}", option, selecttag.Name, e), "option"); } if (itemToClick.Visible) { itemToClick.Click(); break; } //If not visible, select using keyboard. selectTagValueForOption = FindSelectTagValueForOption(selecttag, option); //Close Select Tag box. selecttag.Click(); SelectTagValueWithKeyboard(selecttag, selectTagValueForOption); break; default: throw new Exception(String.Format("Browser not supported: {0}", domWebDocument.BrowserName)); } }