コード例 #1
0
        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);
            }
        }
コード例 #2
0
        private void GetAllGoodsInCategory(NcpAppElement category3Element)
        {
            try
            {
                hasNoneNewElementTime = 0;
                GoodsElementList      = new NcpAppElementList();
                Size winSize = AppAccess.GetWindowSize();
                AppAccess.SwipeDisplayElements(new Point(300, 1000),
                                               new Point(300, 300),
                                               1000,
                                               4000,
                                               GetCategoryGoodsItems);

                //保存到文件
                string[] goodsColumns = new string[] { "name",
                                                       "price",
                                                       "unit",
                                                       "category1Name",
                                                       "category2Name",
                                                       "category3Name" };
                Dictionary <string, int> goodsColumnDic = CommonUtil.InitStringIndexDic(goodsColumns);
                string      goodsFilePath = GetCategoryGoodsFilePath(category3Element.Id);
                ExcelWriter goodsEW       = new ExcelWriter(goodsFilePath, "List", goodsColumnDic, null);

                string category1Name = category3Element.Attributes["category1Name"];
                string category2Name = category3Element.Attributes["category2Name"];
                string category3Name = category3Element.Name;

                for (int i = 0; i < GoodsElementList.Count; i++)
                {
                    NcpAppElement element           = GoodsElementList[i];
                    string        name              = element.Name;
                    string        price             = element.Attributes["price"];
                    string        unit              = element.Attributes["unit"];
                    Dictionary <string, string> row = new Dictionary <string, string>();
                    row.Add("name", name);
                    row.Add("price", price);
                    row.Add("unit", unit);
                    row.Add("category1Name", category1Name);
                    row.Add("category2Name", category2Name);
                    row.Add("category3Name", category3Name);
                    goodsEW.AddRow(row);
                }
                goodsEW.SaveToDisk();
            }
            catch (Exception ex)
            {
                throw new Exception("获取商品信息失败, categoryId = " + category3Element.Id, ex);
            }
        }
コード例 #3
0
        private AppiumWebElement GetCategoryElement(string name, string count)
        {
            ReadOnlyCollection <AndroidElement> allRelativeLayouts = AppAccess.GetElementsByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.View/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout", true);

            foreach (AppiumWebElement element in allRelativeLayouts)
            {
                ReadOnlyCollection <AppiumWebElement> textElements = element.FindElementsByClassName("android.widget.TextView");
                if (textElements != null && textElements.Count >= 2)
                {
                    AppiumWebElement nameElement  = textElements[0];
                    AppiumWebElement countElement = textElements[1];
                    if (nameElement.Text == name && countElement.Text == "(" + count + ")")
                    {
                        return(element);
                    }
                }
            }

            ReadOnlyCollection <AndroidElement> allLinearLayouts = AppAccess.GetElementsByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.view.View/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.support.v7.widget.RecyclerView/android.widget.LinearLayout", true);

            foreach (AppiumWebElement element in allLinearLayouts)
            {
                ReadOnlyCollection <AppiumWebElement> textElements = element.FindElementsByClassName("android.widget.TextView");
                if (textElements != null && textElements.Count >= 2)
                {
                    AppiumWebElement nameElement  = textElements[0];
                    AppiumWebElement countElement = textElements[1];
                    if (nameElement.Text == name && countElement.Text == "(" + count + ")")
                    {
                        return(element);
                    }
                }
            }

            try
            {
                Size winSize = AppAccess.GetWindowSize();
                AppAccess.Swipe(new Point(winSize.Width - 20, winSize.Height - 20),
                                new Point(winSize.Width - 20, 200),
                                2000);
                Thread.Sleep(3000);
                return(this.GetCategoryElement(name, count));
            }
            catch (Exception ex)
            {
                throw new Exception("找不到分类元素, name = " + name + ", count = " + count, ex);
            }
        }
コード例 #4
0
        private void GetAllGoodsInCategory(string locationName, NcpAppElement categoryElement)
        {
            try
            {
                hasNoneNewElementTime = 0;
                GoodsElementList      = new NcpAppElementList();
                Size winSize = AppAccess.GetWindowSize();
                AppAccess.SwipeDisplayElements(new Point(winSize.Width - 20, winSize.Height - 200),
                                               new Point(winSize.Width - 20, 400),
                                               1000,
                                               10000,
                                               GetCategoryGoodsItems);

                //保存到文件
                int      goodsCount   = int.Parse(categoryElement.Attributes["count"]);
                string[] goodsColumns = new string[] { "id",
                                                       "name",
                                                       "price",
                                                       "categoryName",
                                                       "locationName" };
                Dictionary <string, int> goodsColumnDic = CommonUtil.InitStringIndexDic(goodsColumns);
                string      goodsFilePath = (goodsCount == GoodsElementList.Count ? this.GetCategoryGoodsFilePath(locationName, categoryElement.Id) : this.GetCategoryGoodsFilePath("_Error_" + locationName, categoryElement.Id));
                ExcelWriter goodsEW       = new ExcelWriter(goodsFilePath, "List", goodsColumnDic, null);

                for (int i = 0; i < GoodsElementList.Count; i++)
                {
                    NcpAppElement element           = GoodsElementList[i];
                    string        id                = element.Id;
                    string        name              = element.Name;
                    string        price             = element.Attributes["price"];
                    Dictionary <string, string> row = new Dictionary <string, string>();
                    row.Add("id", element.Id);
                    row.Add("name", name);
                    row.Add("categoryName", categoryElement.Name);
                    row.Add("price", price);
                    row.Add("locationName", locationName);
                    goodsEW.AddRow(row);
                }
                goodsEW.SaveToDisk();
            }
            catch (Exception ex)
            {
                throw new Exception("获取商品信息失败, locationName = " + locationName + ", categoryId = " + categoryElement.Id, ex);
            }
        }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
        private void GetCategoryInfo()
        {
            string categoryFilePath = this.GetCategoryFilePath();

            if (!File.Exists(categoryFilePath))
            {
                try
                {
                    //获取一级分类信息
                    NcpAppElementList allC1Elements = this.GetCategory1Items();

                    foreach (NcpAppElement c1Element in allC1Elements)
                    {
                        //AndroidElement c1Node = AppAccess.GetElementByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.ListView/android.widget.RelativeLayout/android.widget.TextView[1]", new string[] { c1Element.Name }, true);
                        AndroidElement c1Node = AppAccess.GetElementByXPath("//android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.LinearLayout[1]/android.widget.ListView/android.widget.RelativeLayout/android.widget.RelativeLayout/android.widget.TextView[1]", new string[] { c1Element.Name }, true);
                        c1Node.Click();

                        CategoryElementList = new NcpAppElementList();
                        Size winSize = AppAccess.GetWindowSize();
                        AppAccess.SwipeDisplayElements(new Point(300, 1000),
                                                       new Point(300, 300),
                                                       2000,
                                                       2000,
                                                       GetSubCategoryItems);

                        //分级处理
                        List <NcpAppElement> level2Elements    = new List <NcpAppElement>();
                        NcpAppElement        lastLevel2Element = null;
                        for (int i = 0; i < CategoryElementList.Count; i++)
                        {
                            NcpAppElement element = CategoryElementList[i];
                            if (element.TypeName == "Level2")
                            {
                                level2Elements.Add(element);
                                lastLevel2Element = element;
                            }
                            else
                            {
                                lastLevel2Element.Children.Add(element);
                            }
                        }
                        c1Element.Children.AddRange(level2Elements);
                    }



                    //保存到文件

                    string[] categoryColumns = new string[] { "category1Name",
                                                              "category2Name",
                                                              "category3Name" };
                    Dictionary <string, int> categoryColumnDic = CommonUtil.InitStringIndexDic(categoryColumns);
                    ExcelWriter categoryEW = new ExcelWriter(categoryFilePath, "List", categoryColumnDic, null);

                    for (int i = 0; i < allC1Elements.Count; i++)
                    {
                        NcpAppElement c1Element     = allC1Elements[i];
                        string        category1Name = c1Element.Name;
                        if (c1Element.Children.Count > 0)
                        {
                            for (int j = 0; j < c1Element.Children.Count; j++)
                            {
                                NcpAppElement c2Element     = c1Element.Children[j];
                                string        category2Name = c2Element.Name;
                                if (c2Element.Children.Count > 0)
                                {
                                    for (int k = 0; k < c2Element.Children.Count; k++)
                                    {
                                        NcpAppElement c3Element     = c2Element.Children[k];
                                        string        category3Name = c3Element.Name;

                                        Dictionary <string, string> row = new Dictionary <string, string>();
                                        row.Add("category1Name", category1Name);
                                        row.Add("category2Name", category2Name);
                                        row.Add("category3Name", category3Name);
                                        categoryEW.AddRow(row);
                                    }
                                }
                            }
                        }
                    }
                    categoryEW.SaveToDisk();
                }
                catch (Exception ex)
                {
                    throw new Exception("获取分类信息失败.", ex);
                }
            }
        }