Thanks Markus.
Unfortunately it took a bit more work than it should have, so I will share what I did so others can get benefit from it...
I add the following code inside the Program.cs file, inside the Program class. I can then call it from any module in that project.
The modules that do my select actions would click the selecttag and then have a usercode method that calls this with the specified selecttag and desired optiontag.
If the browser is Chrome, it figures out which element is selected and which element is needed, then presses the UP or DOWN keys the number of times required and then presses ENTER. This will fire the event needed.
If the browser is NOT Chrome, it will just mouse click the optiontag.
NOTE: This assumes that the select tag has already been clicked.
Code: Select all
public static WebDocument GetRootDOM(Element thisElement) {
if (thisElement.PreferredCapability.DisplayName.ToLower().Equals("webdocument")) {
return thisElement;
}
return Program.GetRootDOM(thisElement.Parent);
}
public static void SelectOption(SelectTag selectTag, OptionTag optionTag) {
string browser = GetRootDOM(selectTag).BrowserName;
switch (browser) {
case "Chrome":
SelectOption_CHROME(selectTag, optionTag);
break;
default:
SelectOption_Normal(optionTag);
break;
}
}
public static void SelectOption_Normal(OptionTag optionTag) {
Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item '" + optionTag.GetPath().ToResolvedString() + "' at Center.");
optionTag.Click();
}
public static void SelectOption_CHROME(SelectTag selectTag, OptionTag optionTag) {
string selectedTagValue = selectTag.TagValue;
string desiredTagValue = optionTag.TagValue;
OptionTag selectedOptionTag = selectTag.Find<OptionTag>(".//option[@TagValue='" + selectedTagValue + "']")[0];
//List<OptionTag> options = new List<OptionTag>(selectTag.Find<OptionTag>(".//option"));
List<string> options = new List<string>();
foreach (OptionTag ot in selectTag.Find<OptionTag>(".//option")) {
options.Add(ot.TagValue);
}
int selectedIndex = options.IndexOf(selectedOptionTag.TagValue);
int desiredIndex = options.IndexOf(optionTag.TagValue);
Keyboard.PrepareFocus(selectTag);
if (desiredIndex < selectedIndex) {
for (int i = desiredIndex; i < selectedIndex; i++) {
//up
Report.Log(ReportLevel.Info, "Keyboard", "Key 'Up' Press with focus on '" + selectTag.GetPath() + "'.");
Keyboard.Press(System.Windows.Forms.Keys.Up, Keyboard.DefaultScanCode, Keyboard.DefaultKeyPressTime, 1, true);
}
} else if (selectedIndex < desiredIndex) {
for (int i = selectedIndex; i < desiredIndex; i++) {
//down
Report.Log(ReportLevel.Info, "Keyboard", "Key 'Down' Press with focus on '" + selectTag.GetPath() + "'.");
Keyboard.Press(System.Windows.Forms.Keys.Down, Keyboard.DefaultScanCode, Keyboard.DefaultKeyPressTime, 1, true);
}
}
Report.Log(ReportLevel.Info, "Keyboard", "Key 'Enter' Press with focus on '" + selectTag.GetPath() + "'.");
Keyboard.Press(System.Windows.Forms.Keys.Enter, Keyboard.DefaultScanCode, Keyboard.DefaultKeyPressTime, 1, true);
}
}
Do you guys know if there is a bug tracker item in Chrome for this issue of not providing UIAutomation info for option drop-downs? I can't believe that this isn't just an over-looked issue for them. Most of the other elements work correctly with Chrome.
Shortcuts usually aren't...