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.
#ThanksForReading
#HappyLearning

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

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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.

Usage of Enum in Selenium WebDriver Scripts

by Amod Mahajan time to read: 2 min
0
%d bloggers like this: