在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
原创 - C#与Ranorex自动化公用方法
利用c#在Ranorex上写自动化已经有很长的一段时间了,总结发现常用的方法不外乎如下几种: 1、打开浏览器;或者app 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); }
2、删除以前的值,输入一个值; //输入框中内容先删除后填写 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; // } }
3、 下拉列表框中选择或者输入一个值 //输入内容 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; } } } }
4、通过按Down按钮或则Up来再下拉列表框中选择一个值 Keyboard.Press("{Down 7}{Up 4}{Enter}"); 5、点击一个按钮 adapter1.Click(); 6、点击按钮直到某个控件出现为止 //等待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; } } }
7、通过xpath来找到某个控件(或判断某个控件是否存在) 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);
8、通过坐标来点击控件 |
请发表评论