public void CheckAbilityToSetNodeArguments()
        {
            var service = new AppiumServiceBuilder()
                          .WithStartUpTimeOut(TimeSpan.FromMilliseconds(500)) // we expect the Appium startup to fail, so fail quickly
                          .WithNodeArguments("--version")                     // show Node version and exit
                          .Build();

            var nodeOutput = new StringBuilder();

            try
            {
                service.OutputDataReceived += (o, args) => nodeOutput.AppendLine(args.Data);
                service.Start();
            }
            catch (AppiumServerHasNotBeenStartedLocallyException)
            {
                // expected exception, ignore
            }
            finally
            {
                service.Dispose();
            }

            Assert.That(nodeOutput.ToString(), Does.Match(@"v\d+\.\d+\.\d+"));
        }
        public void CheckAbilityToStartServiceUsingCapabilitiesAndFlags()
        {
            DesiredCapabilities capabilities = new DesiredCapabilities();

            capabilities.SetCapability(MobileCapabilityType.PlatformName, "Android");
            capabilities.SetCapability(MobileCapabilityType.FullReset, true);
            capabilities.SetCapability(MobileCapabilityType.NewCommandTimeout, 60);
            capabilities.SetCapability(AndroidMobileCapabilityType.AppPackage, "io.appium.android.apis");
            capabilities.SetCapability(AndroidMobileCapabilityType.AppActivity, ".view.WebView1");

            OptionCollector args = new OptionCollector().AddCapabilities(capabilities)
                                   .AddArguments(GeneralOptionList.CallbackAddress(testIP))
                                   .AddArguments(GeneralOptionList.OverrideSession());
            AppiumLocalService service = null;

            try
            {
                service = new AppiumServiceBuilder().WithArguments(args).Build();
                service.Start();
                Assert.IsTrue(service.IsRunning);
            }
            finally
            {
                if (service != null)
                {
                    service.Dispose();
                }
            }
        }
        public void CheckStartingOfAServiceWithNonLocalhostIP()
        {
            IPHostEntry host;
            string      localIp  = "?";
            string      hostName = Dns.GetHostName();

            host = Dns.GetHostEntry(hostName);
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIp = ip.ToString();
                    break;
                }
            }
            Console.WriteLine(localIp);

            AppiumLocalService service = new AppiumServiceBuilder().WithIPAddress(localIp).UsingPort(4000).
                                         Build();

            try
            {
                service.Start();
                Assert.IsTrue(service.IsRunning);
            }
            finally
            {
                service.Dispose();
            }
        }
        public void CheckAbilityToStartServiceUsingCapabilities()
        {
            AppiumOptions capabilities = new AppiumOptions();

            capabilities.AddAdditionalCapability(MobileCapabilityType.PlatformName, "Android");
            capabilities.AddAdditionalCapability(MobileCapabilityType.FullReset, true);
            capabilities.AddAdditionalCapability(MobileCapabilityType.NewCommandTimeout, 60);
            capabilities.AddAdditionalCapability(AndroidMobileCapabilityType.AppPackage, "io.appium.android.apis");
            capabilities.AddAdditionalCapability(AndroidMobileCapabilityType.AppActivity, ".view.WebView1");

            OptionCollector    args    = new OptionCollector().AddCapabilities(capabilities);
            AppiumLocalService service = null;

            try
            {
                service = new AppiumServiceBuilder().WithArguments(args).Build();
                service.Start();
                Assert.IsTrue(service.IsRunning);
            }
            finally
            {
                if (service != null)
                {
                    service.Dispose();
                }
            }
        }
        public void CheckTheAbilityToDefineTheDesiredLogFile()
        {
            FileInfo           log     = new FileInfo("Log.txt");
            AppiumLocalService service = new AppiumServiceBuilder().WithLogFile(log).Build();

            try
            {
                service.Start();
                Assert.IsTrue(log.Exists);
                Assert.IsTrue(log.Length > 0); //There should be Appium greeting messages
            }
            finally
            {
                service.Dispose();
                if (log.Exists)
                {
                    File.Delete(log.FullName);
                }
                service.Dispose();
            }
        }
        public void CheckStartingOfAServiceWithNonLocalhostIP()
        {
            AppiumLocalService service = new AppiumServiceBuilder().WithIPAddress(testIP).UsingPort(4000).Build();

            try
            {
                service.Start();
                Assert.IsTrue(service.IsRunning);
            }
            finally
            {
                service.Dispose();
            }
        }
        public void CheckAbilityToStartServiceOnAFreePort()
        {
            AppiumLocalService service = new AppiumServiceBuilder().UsingAnyFreePort().Build();

            try
            {
                service.Start();
                Assert.IsTrue(service.IsRunning);
            }
            finally
            {
                service.Dispose();
            }
        }
        public void CheckStartingOfTheServiceDefinedExternally()
        {
            AppiumLocalService service = new AppiumServiceBuilder().WithAppiumJS(new FileInfo(PathToCustomizedAppiumJS)).Build();

            try
            {
                service.Start();
                Assert.IsTrue(service.IsRunning);
            }
            finally
            {
                service.Dispose();
            }
        }
        public void CheckAbilityToBuildServiceUsingNodeDefinedExplicitly()
        {
            AppiumLocalService service = null;

            try
            {
                service = new AppiumServiceBuilder().WithAppiumJS(new FileInfo(_pathToCustomizedAppiumJs)).Build();
                service.Start();
                Assert.AreEqual(true, service.IsRunning);
            }
            finally
            {
                service?.Dispose();
            }
        }
        public void CheckStartingOfAServiceWithNonDefaultArguments()
        {
            OptionCollector    args    = new OptionCollector().AddArguments(GeneralOptionList.LogNoColors());
            AppiumLocalService service = new AppiumServiceBuilder().UsingPort(4000).WithArguments(args).Build();

            try
            {
                service.Start();
                Assert.IsTrue(service.IsRunning);
            }
            finally
            {
                service.Dispose();
            }
        }
        public void CheckStartingOfTheServiceDefinedExternallyWithNonDefaultArguments()
        {
            OptionCollector    args    = new OptionCollector().AddArguments(GeneralOptionList.LogNoColors());
            AppiumLocalService service = new AppiumServiceBuilder().WithAppiumJS(new FileInfo(PathToCustomizedAppiumJS)).
                                         UsingPort(4000).WithArguments(args).Build();

            try
            {
                service.Start();
                Assert.IsTrue(service.IsRunning);
            }
            finally
            {
                service.Dispose();
            }
        }
        public void CheckAbilityToStartServiceUsingFlags()
        {
            AppiumLocalService service = null;
            var args = new OptionCollector().AddArguments(GeneralOptionList.CallbackAddress(_testIp))
                       .AddArguments(GeneralOptionList.OverrideSession());

            try
            {
                service = new AppiumServiceBuilder().WithArguments(args).Build();
                service.Start();
                Assert.IsTrue(service.IsRunning);
            }
            finally
            {
                service?.Dispose();
            }
        }
        public void CheckAbilityToStartServiceOnAFreePort()
        {
            AppiumLocalService service = null;

            try
            {
                service = new AppiumServiceBuilder().UsingAnyFreePort().Build();
                service.Start();
                Assert.AreEqual(true, service.IsRunning);
            }
            finally
            {
                if (service != null)
                {
                    service.Dispose();
                }
            }
        }
        public void CheckAbilityToStartAndShutDownFewServices()
        {
            AppiumLocalService service1 = new AppiumServiceBuilder().UsingAnyFreePort().Build();
            AppiumLocalService service2 = new AppiumServiceBuilder().UsingAnyFreePort().Build();
            AppiumLocalService service3 = new AppiumServiceBuilder().UsingAnyFreePort().Build();
            AppiumLocalService service4 = new AppiumServiceBuilder().UsingAnyFreePort().Build();

            service1.Start();
            service2.Start();
            service3.Start();
            service4.Start();
            service1.Dispose();
            Thread.Sleep(1000);
            service2.Dispose();
            Thread.Sleep(1000);
            service3.Dispose();
            Thread.Sleep(1000);
            service4.Dispose();
            Assert.IsTrue(!service1.IsRunning);
            Assert.IsTrue(!service2.IsRunning);
            Assert.IsTrue(!service3.IsRunning);
            Assert.IsTrue(!service4.IsRunning);
        }
        public void CheckStartingOfTheServiceDefinedByProperty()
        {
            try
            {
                string definedNode = PathToCustomizedAppiumJS;
                Environment.SetEnvironmentVariable(AppiumServiceConstants.AppiumBinaryPath, definedNode);
                AppiumLocalService service = new AppiumServiceBuilder().Build();

                try
                {
                    service.Start();
                    Assert.IsTrue(service.IsRunning);
                }
                finally
                {
                    service.Dispose();
                }
            }
            finally
            {
                Environment.SetEnvironmentVariable(AppiumServiceConstants.AppiumBinaryPath, string.Empty);
            }
        }