top of page

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 system property, or by importing an existing class that does the work for you, named SystemUtils.

Write your own checks with

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


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: And this is what you would type in your pom.xml file, in case no other library is already importing it:


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:

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

  if (IS_OS_WINDOWS) { doSomething; }
Importing it into the class:
import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
 if (IS_OS_LINUX) { doSomething; }
Importing it into the class:
import static org.apache.commons.lang3.SystemUtils.IS_OS_MAC;
  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.

Recent Posts

See All

Creating an Architecture for Your Automated Tests Writing Automated Tests in Small Increments 4 Times Your Automation Passed While Bugs Were Present Now That You’ve Created Automated Tests, Run Them!

This week my newest article was released, this time on how to use Log4j to log relevant test automation information:

bottom of page