private List <AppiumWebElement> GetCategoryElement(string name) { AndroidElement categoryMainPageElement = AppAccess.GetElementByClassNameAndText("android.widget.TextView", new string[] { "首页" }, true, true, true); categoryMainPageElement.Click(); AndroidElement categoryMenuElement = AppAccess.GetElementByClassNameAndText("android.widget.TextView", new string[] { "生鲜分类" }, true, true, true); categoryMenuElement.Click(); AndroidElement listElement = AppAccess.GetElementByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout", false); //ReadOnlyCollection<AndroidElement> allC3Elements = AppAccess.GetElementsByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.GridView/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TextView", false); ReadOnlyCollection <AppiumWebElement> allC3Elements = listElement.FindElementsByClassName("android.widget.TextView"); List <AppiumWebElement> foundElements = new List <AppiumWebElement>(); for (int i = 0; i < allC3Elements.Count; i++) { AppiumWebElement element = allC3Elements[i]; if (element.Text == name) { foundElements.Add(element); /* * if (i + 3 >= allC3Elements.Count) * { * return GetCategoryElementAfterFound(name); * } * else * { * return element; * }*/ } } if (foundElements.Count > 0) { return(foundElements); } try { try { Size winSize = AppAccess.GetWindowSize(); AppAccess.Swipe(new Point(300, 600), new Point(300, 300), 1000); } catch (Exception ex1) { } Thread.Sleep(1000); return(this.GetCategoryElement(name)); } catch (Exception ex) { throw new Exception("找不到分类元素, name = " + name, ex); } }
private void SelectCategory(NcpAppElement categoryElement) { AndroidElement categoryMenuElement = AppAccess.GetElementByClassNameAndText("android.widget.TextView", new string[] { "分类" }, true, true, true); categoryMenuElement.Click(); AppiumWebElement cagtegoryElement = this.GetCategoryElement(categoryElement.Name, categoryElement.Attributes["count"]); cagtegoryElement.Click(); }
private void ToSelectLocationPage(string locationName) { try { AndroidElement locationInputElement = AppAccess.GetElementByClassNameAndText("android.widget.EditText", new string[] { "写字楼、小区、学校" }, true, true, true); locationInputElement.Click(); locationInputElement.SendKeys(locationName); AndroidElement locationOptionElement = AppAccess.GetElementByClassNameAndText("android.widget.TextView", new string[] { locationName }, true, true, true); locationOptionElement.Click(); } catch (Exception ex) { throw new Exception("打开选择location页面, locatoinName = " + locationName, ex); } }
private List <AppiumWebElement> GetCategoryElementAfterFound(string name) { //出现过找到元素了,但是点击不到的情况,那么再滑屏一下,使得元素暴露的更明显 try { AppAccess.Swipe(new Point(300, 600), new Point(300, 10), 2000); } catch (Exception ex) { //出错,但是界面看起来没问题,所以忽略了 } AndroidElement categoryMainPageElement = AppAccess.GetElementByClassNameAndText("android.widget.TextView", new string[] { "首页" }, true, true, true); categoryMainPageElement.Click(); AndroidElement categoryMenuElement = AppAccess.GetElementByClassNameAndText("android.widget.TextView", new string[] { "生鲜分类" }, true, true, true); categoryMenuElement.Click(); AndroidElement listElement = AppAccess.GetElementByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout", false); //ReadOnlyCollection<AndroidElement> allC3Elements = AppAccess.GetElementsByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.GridView/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TextView", false); ReadOnlyCollection <AppiumWebElement> allC3Elements = listElement.FindElementsByClassName("android.widget.TextView"); List <AppiumWebElement> foundElements = new List <AppiumWebElement>(); foreach (AppiumWebElement element in allC3Elements) { if (element.Text == name) { foundElements.Add(element); } } if (foundElements.Count == 0) { throw new Exception("GetCategoryElementAfterFound执行出错!"); } else { return(foundElements); } }
private void GetCategoryInfo(string locationName) { try { AndroidElement categoryMenuElement = AppAccess.GetElementByClassNameAndText("android.widget.TextView", new string[] { "分类" }, true, true, true); categoryMenuElement.Click(); CategoryElementList = new NcpAppElementList(); Size winSize = AppAccess.GetWindowSize(); AppAccess.SwipeDisplayElements(new Point(winSize.Width - 20, winSize.Height - 20), new Point(winSize.Width - 20, 200), 2000, 5000, GetCategoryItems); NcpAppElementList sortedCategoryElements = CategoryElementList.SortByPosition(); //分级处理 List <NcpAppElement> level1Elements = new List <NcpAppElement>(); NcpAppElement lastLevel1Element = null; for (int i = 1; i < sortedCategoryElements.Count; i++) { NcpAppElement element = sortedCategoryElements[i]; if (element.TypeName == "Level1") { level1Elements.Add(element); lastLevel1Element = element; } else { lastLevel1Element.Children.Add(element); } } //保存到文件 string[] categoryColumns = new string[] { "id", "category1Name", "category2Name", "needGrab", "goodsCount", "locationName" }; Dictionary <string, int> categoryColumnDic = CommonUtil.InitStringIndexDic(categoryColumns); string categoryFilePath = this.GetCategoryFilePath(locationName); ExcelWriter categoryEW = new ExcelWriter(categoryFilePath, "List", categoryColumnDic, null); for (int i = 0; i < level1Elements.Count; i++) { NcpAppElement element = level1Elements[i]; string category1Name = element.Name; string needGrab = element.Children.Count == 0 ? "是" : "否"; string goodsCount = element.Attributes["count"]; Dictionary <string, string> row = new Dictionary <string, string>(); row.Add("id", element.Id); row.Add("category1Name", category1Name); row.Add("category2Name", ""); row.Add("needGrab", needGrab); row.Add("goodsCount", goodsCount); row.Add("locationName", locationName); categoryEW.AddRow(row); if (element.Children.Count > 0) { for (int j = 0; j < element.Children.Count; j++) { NcpAppElement childElement = element.Children[j]; string category2Name = childElement.Name; string childGoodsCount = childElement.Attributes["count"]; Dictionary <string, string> childRow = new Dictionary <string, string>(); childRow.Add("id", childElement.Id); childRow.Add("category1Name", category1Name); childRow.Add("category2Name", category2Name); childRow.Add("needGrab", "是"); childRow.Add("goodsCount", childGoodsCount); childRow.Add("locationName", locationName); categoryEW.AddRow(childRow); } } } categoryEW.SaveToDisk(); } catch (Exception ex) { throw new Exception("获取分类信息失败.", ex); } }
private void GotoYonghuiShopPage() { AndroidElement yonghuiElement = AppAccess.GetElementByClassNameAndText("android.widget.TextView", new string[] { "永辉超市" }, true, true, true); yonghuiElement.Click(); }