Refactoring -2

Let's refactor the given line and change the buildDefaultService(); instead, we will use the buildService() method, which will take some of the server flags discussed earlier:

AppiumDriverLocalService appiumService = AppiumDriverLocalService.buildDefaultService();

Ensure that the path to node and Appium is what you found on your machine. To find the path on your Mac machine, launch Terminal and run the which appium and which node commands to get the path:

Also, the path to the log file can be relative to the project folder. So, now the method should look as shown in the following code:

@Before
public void startAppiumServer() throws IOException {

int port = 4723;

appiumService = AppiumDriverLocalService.buildService(new
AppiumServiceBuilder()
.usingDriverExecutable(new File(("/usr/local/bin/node")))
.withAppiumJS(new File(("/usr/local/bin/appium")))
.withIPAddress("0.0.0.0")
.usingPort(port)
.withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withLogFile(new File("build/appium.log")));
appiumService.start();
}

For a Windows user, we need to get the path of executables of Node JS and Appium. So, instead of having two methods in the same class, we will add a check for the OS type, and we will start the Appium server based on the OS type. So, the preceding code will look as illustrated; dependency is nodeJS_Path, and appiumJS_Path should be defined based on your machine:

@Before
public void startAppiumServer() throws IOException {

int port = 4723;
String nodeJS_Path = "C:/Program Files/NodeJS/node.exe";
String appiumJS_Path = "C:/Program
Files/Appium/node_modules/appium/bin/appium.js";

String osName = System.getProperty("os.name");

if (osName.contains("Mac")) {
appiumService = AppiumDriverLocalService.buildService(new
AppiumServiceBuilder()
.usingDriverExecutable(new
File(("/usr/local/bin/node")))
.withAppiumJS(new File(("/usr/local/bin/appium")))
.withIPAddress("0.0.0.0")
.usingPort(port)
.withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withLogFile(new File("build/appium.log")));
} else if (osName.contains("Windows")) {
appiumService = AppiumDriverLocalService.buildService(new
AppiumServiceBuilder()
.usingDriverExecutable(new File(nodeJS_Path))
.withAppiumJS(new File(appiumJS_Path))
.withIPAddress("0.0.0.0")
.usingPort(port)
.withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withLogFile(new File("build/appium.log")));
}
appiumService.start();
}

In the preceding code, we noticed that the SESSION_OVERRIDE belongs to GeneralServerFlag; apart from that, there are a lot of other flags as well, which can be chosen from this list:

For example, we can choose to pass the ROBOT_ADDRESS flag and pass the device ID or UDID to run the test on a certain device connected to the machine:

.withArgument(GeneralServerFlag.ROBOT_ADDRESS, udid)

We will use the udid feature when we need to execute the test on physical devices or more than one device with similar configurations. We will read about this in detail in the upcoming chapter. Let's move on to the server capabilities.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.152.87