예제 #1
0
        protected override void Execute(NativeActivityContext context)
        {
            // Gather fields
            string apkPath          = ApkPath.Get(context);
            string deviceName       = DeviceName.Get(context);
            string version          = AndroidVersion.Get(context);
            string package          = AppPackage.Get(context);
            string activity         = AppActivity.Get(context);
            string locale           = Locale.Get(context) ?? "US";
            string language         = Language.Get(context) ?? "en";
            int    waitTime         = WaitTime.Get(context);
            string screenshotPath   = ScreenshotPath.Get(context) ?? "";
            string chromedriverPath = ChromedriverPath.Get(context) ?? "";

            // Initialize Driver and Appium Server
            AndroidDriver <AndroidElement> driver;
            AppiumLocalService             server;

            // Start Appium Server on any open port
            server = new AppiumServiceBuilder().UsingAnyFreePort().Build();
            server.Start();

            // Set additional capabilities
            var options = new AppiumOptions();

            //Device capabilities
            options.AddAdditionalCapability(MobileCapabilityType.DeviceName, deviceName);
            options.AddAdditionalCapability(MobileCapabilityType.PlatformName, "Android");
            options.AddAdditionalCapability(MobileCapabilityType.PlatformVersion, version);
            options.AddAdditionalCapability(MobileCapabilityType.Locale, locale);
            options.AddAdditionalCapability(MobileCapabilityType.Language, language);
            // Use UIAutomator2 for better performance
            options.AddAdditionalCapability(MobileCapabilityType.AutomationName, AutomationName.AndroidUIAutomator2);
            // Launch App or Browser depending on user input
            switch (LaunchType)
            {
            case LaunchType.App:
                // App capabilities
                if (!String.IsNullOrEmpty(apkPath))
                {
                    options.AddAdditionalCapability(MobileCapabilityType.App, apkPath);
                }
                options.AddAdditionalCapability(AndroidMobileCapabilityType.AppPackage, package);
                options.AddAdditionalCapability(AndroidMobileCapabilityType.AppActivity, activity);
                break;

            case LaunchType.Browser:
                // Browser capabilities
                switch (BrowserType)
                {
                case BrowserType.Chrome:
                    options.AddAdditionalCapability(MobileCapabilityType.BrowserName, MobileBrowserType.Chrome);
                    if (!String.IsNullOrEmpty(chromedriverPath))
                    {
                        options.AddAdditionalCapability(AndroidMobileCapabilityType.ChromedriverExecutable, chromedriverPath);
                    }
                    break;

                default:
                    throw new ArgumentNullException("Missing Argument: BrowserType.");
                }
                break;

            default:
                throw new ArgumentNullException("Missing Argument: LaunchType.");
            }
            // Default Screenshot Path
            options.AddAdditionalCapability(AndroidMobileCapabilityType.AndroidScreenshotPath, screenshotPath);

            driver = new AndroidDriver <AndroidElement>(server, options);
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(waitTime);

            // Export driver
            Driver.Set(context, driver);

            // Schedule Activities
            if (Body != null)
            {
                context.ScheduleAction <AndroidDriver <AndroidElement> >(Body, driver, OnCompleted, OnFaulted);
            }
        }