-
Notifications
You must be signed in to change notification settings - Fork 187
Setting Up and Running an AppiumDriver Locally for Testing
- Installed Node.js and jdk.
- At least Appium 2.0.0 installed via npm.
- At least appium-dotnet-client (Appium.WebDriver) 6.0.0 installed via dotnet (nuget). Environmental variables:
- node.js: set up the environment variable NODE_BINARY_PATH or NODE_HOME, and its value as well i.e., C:\Program Files\nodejs\node.exe
(!) Here should be a full path to the node.js executable file. This usecase is very useful for platforms which differ from Windows. There are possible unpredictable issues related to user's environmental settings. Also it is useful then the instance of non-default (it is not defined at the PATH, it is the another node.js modification such as N|Solid and so on) node.js is required.
-
JDK: set up variable with name JAVA_HOME and value e.g., C:\Program Files\Java\jdk-23\
-
Android Studio: set up variable with name ANDROID_HOME and Value C:\Users\username\AppData\Local\Android\Sdk. Additionally, add the path to the .\tools and .\platform-tools folders.
-
appium.js or main.js: set up variable with name APPIUM_BINARY_PATH or APPIUM_HOME and value e.g., C:\Users\username\AppData\Roaming\npm\node_modules\appium\build\lib\main.js
-
PATH variable configuration: C:\Program Files\nodejs
C:\Program Files\Java\jdk-23\bin or %JAVA_HOME%\bin C:\Users\username\AppData\Roaming\npm C:\Users\username\AppData\Local\Android\Sdk\tools or %ANDROID_HOME%\tools C:\Users\username\AppData\Local\Android\Sdk\platform-tools or ANDROID_HOME%\platform-tools
It is possible to perform this usecase programmatically this way:
using OpenQA.Selenium.Appium.Service;
using System;
...
Environment.SetEnvironmentVariable(AppiumServiceConstants.NodeBinaryPath, "the full path to node.js executable file");
Environment.SetEnvironmentVariable(AppiumServiceConstants.AppiumBinaryPath, "the full path to appium.js or main.js");
AppiumLocalService service = AppiumServiceBuilder().Build();
service.Start();
...
service.Dispose();
Also, you can add all variables using terminal. E.g., for Windows:
set ANDROID_HOME=C:\Users\username\AppData\Local\Android\Sdk
set PATH=%PATH%;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools
There are abilities and options for starting a local Appium node server. End users can start a local Appium node server and launch their app for testing as follows:
var driverOptions = new AppiumOptions()
{
AutomationName = AutomationName.AndroidUIAutomator2,
PlatformName = "Android",
PlatformVersion = "13",
DeviceName = "your Android Emulator",
App = "your App"
};
// Add an optional Appium parameter, e.g., `newCommandTimeout.`
driverOptions.AddAdditionalAppiumOption("appium:newCommandTimeout", 180);
driver = new AndroidDriver(service.ServiceUrl, driverOptions, TimeSpan.FromSeconds(180));
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
using OpenQA.Selenium.Appium.Service;
...
AppiumLocalService service = new AppiumServiceBuilder().Build();
service.Start();
...
service.Dispose();
using OpenQA.Selenium.Appium.Service;
...
AppiumLocalService service = new AppiumServiceBuilder()
.WithIPAddress("listenableIP").Build();
service.Start();
...
service.Dispose();
using OpenQA.Selenium.Appium.Service;
using System.IO;
...
AppiumLocalService service = new AppiumServiceBuilder()
.WithLogFile(new FileInfo("path\to\log\file")).Build();
service.Start();
...
service.Dispose();
using OpenQA.Selenium.Appium.Service;
...
AppiumLocalService service = new AppiumServiceBuilder()
.UsingPort(intPort).Build();
service.Start();
...
service.Dispose();
or
using OpenQA.Selenium.Appium.Service;
...
AppiumLocalService service = new AppiumServiceBuilder()
.UsingAnyFreePort().Build();
service.Start();
...
service.Dispose();
using OpenQA.Selenium.Appium.Service;
using OpenQA.Selenium.Appium.Service.Options;
...
OptionCollector args = new OptionCollector().AddArguments(GeneralOptionList.LogNoColors());
AppiumLocalService service = new AppiumServiceBuilder().WithArguments(args).Build();
service.Start();
...
service.Dispose();
Actual server flags are listed here. In order to use them easyly they are listed by:
- OpenQA.Selenium.Appium.Service.Options.GeneralOptionList - here are common server options
- OpenQA.Selenium.Appium.Service.Options.AndroidOptionList - here are Android-specific server options
- OpenQA.Selenium.Appium.Service.Options.IOSOptionList - here are iOS-specific server options
Boolean arguments have no parameters. Other server flags require not empty string values.
This feature is compatible with Appium node server v >= 1.5.x. t is possible to do something like that:
var serverCapabilities = new AppiumOptions(){};
//the filling of server default capabilities is going below
var clientCapabilities = new AppiumOptions(){};
//the filling of server default capabilities is going below
OptionCollector argCollector = new OptionCollector().AddCapabilities(serverCapabilities);
AppiumServiceBuilder builder = new AppiumServiceBuilder().WithArguments(argCollector);
AppiumLocalService service = builder.WithArguments(args).Build();
then
AndroidDriver driver = new AndroidDriver(service, clientCapabilities);
or
AndroidDriver driver = new AndroidDriver(builder, clientCapabilities);
or
service.Start();
AndroidDriver driver = new AndroidDriver(service.getUrl(), clientCapabilities);
The full list of capabilities is described here: caps.md. three classes help to define the values of capabilities: OpenQA.Selenium.Appium.Enums.remote.MobileCapabilityType OpenQA.Selenium.Appium.Enums.AndroidMobileCapabilityType OpenQA.Selenium.Appium.Enums.IOSMobileCapabilityType
If it needs to override the default file path to node.js executable and/or the path to appium.js or main.js then
using OpenQA.Selenium.Appium.Service;
...
AppiumLocalService service = new AppiumServiceBuilder()
.WithAppiumJS(new FileInfo("the path to the desired appium.js or main.js"))
.UsingDriverExecutable("the path to the desired node.js executable file")
.Build();
service.Start();
...
service.Dispose();
Driver constructors to initialize a new instance of the AndroidDriver:
// commandExecutor is an object which executes commands for the driver;
// driverOptions is an object containing the Appium options;
public AndroidDriver(ICommandExecutor commandExecutor, DriverOptions driverOptions)
public AndroidDriver(DriverOptions driverOptions)
// commandTimeout is the maximum amount of time to wait for each command;
public AndroidDriver(DriverOptions driverOptions, TimeSpan commandTimeout)
// builder is an object containing settings of the Appium local service which is going to be started;
public AndroidDriver(AppiumServiceBuilder builder, DriverOptions driverOptions)
public AndroidDriver(AppiumServiceBuilder builder, DriverOptions driverOptions, TimeSpan commandTimeout)
// remoteAddress is an URI containing the address of the WebDriver remote server (e.g. http://127.0.0.1:4723);
public AndroidDriver(Uri remoteAddress, DriverOptions driverOptions, TimeSpan commandTimeout)
public AndroidDriver(Uri remoteAddress, DriverOptions driverOptions)
// service is the specified Appium local service;
public AndroidDriver(AppiumLocalService service, DriverOptions driverOptions)
public AndroidDriver(AppiumLocalService service, DriverOptions driverOptions, TimeSpan commandTimeout)
An instance of AppiumLocalService which has passed through constructors will be stopped when
driver.Quit();
If there is need to keep the service alive during a long time then something like that is available
service.Start();
....
new IOSDriver(service.ServiceUrl, capabilities, commandTimeout)