I tried that but it only seems to select the first item in the list, the selected index is set to zero, I have attached an extract of the report below, thanks.
00:01.171 Info Validation Jump to itemEdit in Spy
Validating Exists on item 'SymptomDrivenDiagnostics.SelectedCountry'.
00:03.469 Info User {OptionTag:UK:GB}
00:03.530 Info User UK:GB
00:03.950 Info User 184
00:04.251 Info User 184
00:04.266 Info User 0
00:04.972 Info Keyboard Key 'Enter' Press with focus on '/dom[@domain='localhost:8080']//select[#'selectedCountry']'.
00:05.152 Info User down
00:05.527 Info Keyboard Key 'Down' Press with focus on '/dom[@domain='localhost:8080']//select[#'selectedCountry']'.
00:05.648 Info User down
.
.
Code: Select all
public static void SelectOption(SelectTag selectTag, OptionTag optionTag, Duration dur) {
List<string> options = GetOptionsText(selectTag);
Report.Info(optionTag.ToString());
int desiredIndex = options.IndexOf(optionTag.TagValue);
Report.Info(optionTag.TagValue.ToString());
Report.Info(desiredIndex.ToString());
DoSelectAction(selectTag, desiredIndex, options, dur);
}
private static List<string> GetOptionsText(SelectTag selectTag) {
List<string> options = new List<string>();
foreach (OptionTag ot in selectTag.Find<OptionTag>(".//option")) {
options.Add(ot.TagValue != null ? ot.TagValue : "");
}
return options;
}
private static void DoSelectAction(SelectTag selectTag, int desiredIndex, List<string> options, Duration dur) {
int selectedIndex = 0;
if (selectTag.TagValue != null) {
List<OptionTag> optiontags = new List<OptionTag>(selectTag.Find<OptionTag>(".//optiontag[@TagValue='" + selectTag.TagValue + "']"));
if (options.Count > 0) {
OptionTag selectedOptionTag = optiontags[0];
selectedIndex = options.IndexOf(selectedOptionTag.TagValue);
}
}
Keyboard.PrepareFocus(selectTag);
Duration oldKeyPressTime = Keyboard.DefaultKeyPressTime;
Keyboard.DefaultKeyPressTime = 15;
Duration keyPressDuration = dur;
Report.Info(desiredIndex.ToString());
Report.Info(selectedIndex.ToString());
Report.Log(ReportLevel.Info, "Keyboard", "Key 'Enter' Press with focus on '" + selectTag.GetPath() + "'.");
Keyboard.Press(System.Windows.Forms.Keys.Enter, Keyboard.DefaultScanCode, keyPressDuration, 1, true);//Keyboard.DefaultKeyPressTime, 1, true);
Keyboard.DefaultKeyPressTime = oldKeyPressTime;
if (desiredIndex < selectedIndex) {
for (int i = desiredIndex; i < selectedIndex; i++) {
//up
Report.Info("up");
Report.Log(ReportLevel.Info, "Keyboard", "Key 'Up' Press with focus on '" + selectTag.GetPath() + "'.");
Keyboard.Press(System.Windows.Forms.Keys.Up, Keyboard.DefaultScanCode, keyPressDuration, 1, true);//Keyboard.DefaultKeyPressTime, 1, true);
}
} else if (selectedIndex < desiredIndex) {
for (int i = selectedIndex; i < desiredIndex; i++) {
Report.Info("down");
//down
Report.Log(ReportLevel.Info, "Keyboard", "Key 'Down' Press with focus on '" + selectTag.GetPath() + "'.");
Keyboard.Press(System.Windows.Forms.Keys.Down, Keyboard.DefaultScanCode, keyPressDuration, 1, true);//Keyboard.DefaultKeyPressTime, 1, true);
}
} else if (selectedIndex == desiredIndex) {
Report.Info("match");
Report.Log(ReportLevel.Info, "Keyboard", "Key 'Enter' Press with focus on '" + selectTag.GetPath() + "'.");
Keyboard.Press(System.Windows.Forms.Keys.Enter, Keyboard.DefaultScanCode, keyPressDuration, 1, true);//DefaultKeyPressTime, 1, true);
}
}
public static void RaiseSelectValueChangedEvent(Element element, string eventName)
{
var tag = element;
bool usedTempId = false;
try
{
var selectTag = GetAncestorSelectTag(element);
var id = selectTag["id"] as string;
if (string.IsNullOrEmpty(id))
{
id = "_rxtmp" + new Random().Next(int.MaxValue);
selectTag["id"] = id;
usedTempId = true;
}
var script = @"var _rxtag = document.getElementById('" + id + @"');
window.setTimeout(function(){
var ev = document.createEvent('UIEvents'); " +
(usedTempId ? "_rxtag.removeAttribute('id'); " : "") +
@"ev.initUIEvent('" + eventName + @"', true, true, window, 1);
_rxtag.dispatchEvent(ev);}, 1);";
var host = GetScriptHost(tag);
host.InvokeAction("executescript", script);
}
catch (Exception ex)
{
throw new RanorexException("Failed to raise event '" + eventName + "'. ", ex);
}
}
static Element GetAncestorSelectTag(Element element)
{
var current = element.Parent;
while (current != null)
{
if (("" + current["tagname"]) == "select")
return current;
current = current.Parent;
}
return null;
}
static Element GetScriptHost(Element element)
{
var current = element.Parent;
while (current != null)
{
foreach (var act in current.Actions)
if (act.Name == "executescript")
return current;
current = current.Parent;
}
return null;
}