Beispiel #1
0
        public void AddNewItemWithNewCategory()
        {
            System.Environment.SetEnvironmentVariable("ANDROID_HOME", @"C:\Program Files (x86)\Android\android-sdk");
            System.Environment.SetEnvironmentVariable("JAVA_HOME", @"C:\Program Files\Android\jdk\microsoft_dist_openjdk_1.8.0.25\bin");

            var capabilities = new AppiumOptions();

            // automatic start of the emulator if not running
            capabilities.AddAdditionalCapability(AndroidMobileCapabilityType.Avd, "demo_device");
            capabilities.AddAdditionalCapability(AndroidMobileCapabilityType.AvdArgs, "-no-boot-anim -no-snapshot-load");
            capabilities.AddAdditionalCapability(MobileCapabilityType.FullReset, true);
            // connecting to a device or emulator
            capabilities.AddAdditionalCapability(MobileCapabilityType.DeviceName, "2471736c36037ece");
            capabilities.AddAdditionalCapability(MobileCapabilityType.AutomationName, "UiAutomator2");
            // specifyig which app we want to install and launch
            var currentPath = Directory.GetCurrentDirectory();

            Console.WriteLine($"Current path: {currentPath}");
            var packagePath = Path.Combine(currentPath, @"..\..\..\AppsToTest\com.fluentbytes.carvedrock-x86.apk");

            packagePath = Path.GetFullPath(packagePath);
            Console.WriteLine($"Package path: {packagePath}");
            capabilities.AddAdditionalCapability(MobileCapabilityType.App, packagePath);

            capabilities.AddAdditionalCapability(AndroidMobileCapabilityType.AppPackage, "com.fluentbytes.carvedrock");
            capabilities.AddAdditionalCapability(AndroidMobileCapabilityType.AppActivity, "crc641782d5af3c9cf50a.MainActivity");

            var _appiumLocalService = new AppiumServiceBuilder().UsingAnyFreePort().Build();

            _appiumLocalService.Start();;
            var driver = new AndroidDriver <AppiumWebElement>(_appiumLocalService, capabilities);

            // Create new Category item first
            var categoryButton = driver.FindElement(MobileBy.AccessibilityId("AddCategory"));

            categoryButton.Click();

            // fill out the form for a new category
            var categoryName = driver.FindElement(MobileBy.AccessibilityId("categoryName"));

            categoryName.Clear();
            categoryName.SendKeys("New category from automation");

            //save category
            var saveCategory = driver.FindElement(MobileBy.AccessibilityId("Save"));

            saveCategory.Click();

            var el1 = driver.FindElementByAccessibilityId("Add");

            el1.Click();

            var elItemText = driver.FindElementByAccessibilityId("ItemText");

            elItemText.Clear();
            elItemText.SendKeys("This is a new Item");

            var elItemDetail = driver.FindElementByAccessibilityId("ItemDescription");

            elItemDetail.Clear();
            elItemDetail.SendKeys("These are the details");

            var elItemCategory = driver.FindElement(MobileBy.AccessibilityId("ItemCategory_Container"));

            elItemCategory.Click();

            var picker            = driver.FindElement(By.Id("android:id/contentPanel"));
            var categoryListItems = picker.FindElements(By.ClassName("android.widget.TextView"));

            foreach (var categoryElement in categoryListItems)
            {
                if (categoryElement.Text == "New category from automation")
                {
                    categoryElement.Click();
                }
            }


            var elSave = driver.FindElementByAccessibilityId("Save");

            elSave.Click();

            //wait for progress bar to disapear
            var wait = new DefaultWait <AndroidDriver <AppiumWebElement> >(driver)
            {
                Timeout         = TimeSpan.FromSeconds(60),
                PollingInterval = TimeSpan.FromMilliseconds(500)
            };

            wait.IgnoreExceptionTypes(typeof(NoSuchElementException));

            wait.Until(d => d.FindElement(MobileBy.AccessibilityId("Second item")));


            var listview = driver.FindElementByAccessibilityId("ItemsListView");

            //now use wait to scroll untill we find item

            Func <AppiumWebElement> FindElementAction = () =>
            {
                // find all text views
                // check if the text matches
                var elements = driver.FindElementsByClassName("android.widget.TextView");
                foreach (var textView in elements)
                {
                    if (textView.Text == "This is a new Item")
                    {
                        return(textView);
                    }
                }
                return(null);
            };

            wait = new DefaultWait <AndroidDriver <AppiumWebElement> >(driver)
            {
                Timeout         = TimeSpan.FromSeconds(60),
                PollingInterval = TimeSpan.FromMilliseconds(1000)
            };
            wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
            AppiumWebElement elementfound = null;

            elementfound = wait.Until(d =>
            {
                var input = new PointerInputDevice(PointerKind.Touch);
                ActionSequence FlickUp = new ActionSequence(input);
                FlickUp.AddAction(input.CreatePointerMove(listview, 0, 0, TimeSpan.Zero));
                FlickUp.AddAction(input.CreatePointerDown(MouseButton.Left));

                FlickUp.AddAction(input.CreatePointerMove(listview, 0, -600, TimeSpan.FromMilliseconds(200)));
                FlickUp.AddAction(input.CreatePointerUp(MouseButton.Left));
                driver.PerformActions(new List <ActionSequence>()
                {
                    FlickUp
                });
                return(FindElementAction());
            });

            Assert.IsTrue(elementfound != null);

            driver.CloseApp();
        }