Beispiel #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);
            }
        }
        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);
            }
        }
Beispiel #3
0
        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);
            }
        }