Identify the OS tests are running on with os.Name or SystemUtils

In order to make your tests OS agnostic, sometimes, you need to create some additional logic to handle OS specifics. For example, if you are running Selenium tests, on a browser, you want the test to run on all OSs, not just one, and start the browser instance corresponding to the OS the tests run on.

For that, you need to first identify what OS the tests run on. That can be done either by creating your own OS detection code, using the os.name system property, or by importing an existing class that does the work for you, named SystemUtils.  

Write your own checks with os.name

An option to write your own methods for checking if the OS the tests run on would be to use the ‘os.name’ system property. Reading this property can be done as follows:

System.getProperty("os.name");

So, with this approach, you can create your own methods to check if you are running on Windows for example, as follows:

if (osName.contains("windows")) { doSomething; }

Using SystemUtils

Using SystemUtils might be a better alternative, as the code is written and maintained by the library committers, with constant additions that you can benefit from by simply updating the version of the library you are importing into your project.

Setup SystemUtils

In order to use the SystemUtils class in your project, if you haven’t already, you need to add a dependency for the Apache commons lang3 package. That means updating your pom.xml file with the latest version of this library. Note that in some cases, when you are already importing some libraries, like Selenium, the dependency is already brought by these libraries. This is where you will find the latest versions of lang3: https://mvnrepository.com/artifact/org.apache.commons/commons-lang3. And this is what you would type in your pom.xml file, in case no other library is already importing it:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>latestVersionNumber</version>
</dependency>

What you are interested in is the SystemUtils class. Here you will have plenty of helpful methods to help identify on what OS the tests are running. Some examples can be found below.

Is OS – Windows, Linux, Mac

In case you are interested in general OS, meaning you only need your tests to understand whether they are running on Windows or Linux or Mac, without knowing which version number of  the OS you have (e.g. Windows 10), you can use the following booleans:

IS_OS_WINDOWS
Importing it into the class:
import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;

Usage:
  if (IS_OS_WINDOWS) { doSomething; }
IS_OS_LINUX
Importing it into the class:
import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
Usage:
 if (IS_OS_LINUX) { doSomething; }
IS_OS_MAC
Importing it into the class:
import static org.apache.commons.lang3.SystemUtils.IS_OS_MAC;
Usage:
  if (IS_OS_MAC) { doSomething; }
Is OS – specific version of Windows, Linux, Mac

If you are interested in checking for a specific version of the OS when running the tests, in SystemUtils you will find plenty of booleans that check for these, like: IS_OS_WINDOWS_7, IS_OS_WINDOWS_XP, IS_OS_MAC_OSX_PUMA. Their usage is identical to that of IS_OS_WINDOWS.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s