Make Selenium Easy

Usage of Enum in Selenium WebDriver Scripts

An Enum in Java is a type which is used to define a collection of constants. For an example – We have fixed number of days in a week. We can use an Enum type in Java to store these days.

In our Selenium WebDriver scripts, we may pass a browser name as a string from scripts and then we compare passed browser names i.e. if it is chrome, launch chrome and so on. Let’s see an example code :-

Above Selenium script runs fine but what if you pass browser name as “Chrome Browser” or “CH” or “Google Chrome” if you are not aware of correct format? It will give you message as “Incorrect browser name passed.”. Some may raise questions that you have written code you must know or train others how to pass browser name. I believe that any code snippet should minimize its incorrect usage. And here it is just an example of passing browser names, you may face many such scenarios as above and you can not expect everyone to remember exact values.

Above problem can be resolved using Enum – A set of constants. We can restrict automation tester to choose from a predefined set of constants. Let’s rewrite above code using Enum :-

Same approach you can use wherever you need to work on specific data.

You can find my Selenium WebDriver Java GIT repo here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Table of Contents

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “Usage of Enum in Selenium WebDriver Scripts

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.