public static HtmlElement FindByXpath(Instance instance, string xpath)
{
Tab tab = instance.ActiveTab;
if ((tab.IsVoid) || (tab.IsNull)) throw new Exception("Guia encontrada");
if (tab.IsBusy) tab.WaitDownloading();
// find html element by tag
HtmlElement he = tab.FindElementByXPath(xpath, 0);
if (he.IsVoid || he.IsNull) throw new Exception("Objeto não encontrado");
return he;
}
private static void WaitDownloading(Tab tab)
{
if (tab.IsBusy) tab.WaitDownloading();
}
public static string FindElementAndExecuteAction(Instance instance, string xpath, string action, string attrValue="")
{
Tab tab = instance.ActiveTab;
if ((tab.IsVoid) || (tab.IsNull)) throw new Exception("Guia encontrada");
var he = CommonCode.FindByXpath(instance, xpath);
if (!action.Contains("|")) throw new ArgumentException ("Formato incorreto da ação");
var tmp = action.ToLower().Split(new [] {'|'}, 2);
var mainAction = tmp[0];
var subAction = tmp[1];
switch (mainAction)
{
case "get":
switch(subAction)
{
case "value":
string attributeValue = he.GetValue();
if (string.IsNullOrEmpty(attributeValue))
throw new Exception("Significado em branco");
return attributeValue;
case "selecteditems":
string selectedItems = he.GetSelectedItems();
if (string.IsNullOrEmpty(selectedItems))
throw new Exception("SelectedItens não pode ser vazio");
return selectedItems;
default:
string attributeText = he.GetAttribute(subAction);
if (string.IsNullOrEmpty(attributeText))
throw new Exception(string.Format("Atributo {0} em branco", subAction));
return attributeText;
}
case "rise":
switch(subAction)
{
case "scroll":
he.ScrollIntoView();
break;
default:
he.RiseEvent(subAction, instance.EmulationLevel);
break;
}
WaitDownloading(tab);
return "ok";
case "set":
switch(subAction)
{
case "value":
he.SetValue(attrValue, instance.EmulationLevel, he.TagName=="select");
break;
case "selecteditems":
const string selecteditems_regexp = "Regex:";
if (attrValue.StartsWith(selecteditems_regexp))
{
attrValue = attrValue.Substring(selecteditems_regexp.Length, attrValue.Length - selecteditems_regexp.Length);
var regex = new Regex(attrValue);
var items = he.GetAttribute("items").Split(new [] { ";" }, StringSplitOptions.RemoveEmptyEntries);
var matchedItems = string.Join(";", items.Where(item => regex.Match(item).Success));
he.SetAttribute("selecteditems", matchedItems);
}
else
{
he.SetSelectedItems(attrValue);
}
break;
default:
he.SetAttribute(subAction, attrValue);
break;
}
WaitDownloading(tab);
return "ok";
default:
throw new Exception ("Ação desconhecida");
}
return "ok";
}
}