利用c#在Ranorex上寫自動化已經有很長的一段時間了,總結髮現經常使用的方法不外乎以下幾種:html
一、打開瀏覽器;或者appweb
public static void openBrowserMax(){ Report.Log(ReportLevel.Info, "Website", "Opening web site 'https://www.baidu.com' with browser 'IE' in normal mode.", new RecordItemIndex(0)); Host.Local.OpenBrowser("https://www.baidu.com", "Chrome", "", true, true, false, false, false); Delay.Milliseconds(0); }
二、刪除之前的值,輸入一個值;c#
//輸入框中內容先刪除後填寫 public static void deleteAndInput(Ranorex.Adapter adapter, string item, Ranorex.Core.Repository.RepoItemInfo report) { Report.Log(ReportLevel.Info, "清空並輸入", "在"+"【"+report+"】"+"處刪除原有數據,並輸入:【"+item+"】。"); adapter.Click(); //while(!adapter.Element.HasFocus) {adapter.Click("0;0"); adapter.Focus(); }; Keyboard.Press(System.Windows.Forms.Keys.End | System.Windows.Forms.Keys.Shift, Keyboard.DefaultScanCode, Keyboard.DefaultKeyPressTime, 1, true); Keyboard.Press(System.Windows.Forms.Keys.Delete, Keyboard.DefaultScanCode, Keyboard.DefaultKeyPressTime, 1, true); adapter.PressKeys(item); Delay.Milliseconds(500); // if(adapter.Element.GetAttributeValueText("Value")==item || adapter.Element.GetAttributeValueText("Text")==item || adapter.Element.GetAttributeValueText("InnerText")==item) // { // break; // } }
三、 下拉列表框中選擇或者輸入一個值瀏覽器
//輸入內容 public static void selectItem(Ranorex.Adapter adapter, string item, Ranorex.Core.Repository.RepoItemInfo report) { Report.Log(ReportLevel.Info, "選擇下拉選項", "在"+"【"+report+"】"+"處選擇"+"【"+item+"】"+"。"); if (!string.IsNullOrEmpty(item)) { adapter.Focus(); adapter.PressKeys(item);//輸入數據 Delay.Duration(500, false); } } //輸入某一選項 public static void selectList(Ranorex.Adapter adapter,string value, int length) { //IList<LiTag> li = repo.NewFolder.銷售機會新建.ComboboxDropdown.FindDescendants<LiTag>(); //找到控件adapter的LiTag後代 IList<LiTag> li = adapter.FindDescendants<LiTag>(); foreach(LiTag l in li) { string text = l.InnerText; if(text.Length>=2) { Report.Log(ReportLevel.Info,text); string text1 = text.Substring(0,length); if(value==text1) { l.Click(); //也能夠換成l.selected = true; 若是有selected這個屬性的話 break; } } } }
四、經過按Down按鈕或則Up來再下拉列表框中選擇一個值app
Keyboard.Press("{Down 7}{Up 4}{Enter}");dom
五、點擊一個按鈕spa
adapter1.Click();code
六、點擊按鈕直到某個控件出現爲止orm
//等待30s知道某個控件出現 public static void waitfor30sReportExist(Ranorex.Adapter adapter1, Ranorex.Core.Repository.RepoItemInfo adapter2Info){ int count = 0; Report.Log(ReportLevel.Info,"雙擊"+adapter1.ToString()+",等待"+adapter2Info.ToString()+"出現"); while(!adapter2Info.Exists()){ adapter1.Click(); Delay.Milliseconds(5000,false); count = count + 1; if(count >= 6) { Report.Log(ReportLevel.Failure,"在30s內沒有找到控件"+adapter2Info.ToString()); break; } } }
七、經過xpath來找到某個控件(或判斷某個控件是否存在)htm
Element element = Element.FromPath("/dom[@caption='******' and @page='quotation_search.html']//table[#'resultTable']/tbody/tr"); RxPath xpa = new RxPath("/dom[@caption='*****' and @page='quotation_search.html']//table[#'resultTable']/tbody/tr"); IList<Element> ils= element.Find(xpa); Report.Info("ils.count:" + ils.Count);
八、經過座標來點擊控件