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 :-
package EnumUsage; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class WithoutEnum { private WebDriver launchBrowser(String browserName) { if (browserName.equalsIgnoreCase("Chrome")) { WebDriverManager.chromedriver().setup(); return new ChromeDriver(); } else if (browserName.equalsIgnoreCase("Firefox")) { WebDriverManager.firefoxdriver().setup(); return new FirefoxDriver(); } else { System.out.println("Incorrect browser name passed."); return null; } } @Test public void googleTestChrome() { WebDriver driver = launchBrowser("Chrome"); driver.get("https://www.google.com"); driver.close(); } @Test public void googleTestFirefox() { WebDriver driver = launchBrowser("Firefox"); driver.get("https://www.google.com"); driver.close(); } }
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 :-
package EnumUsage; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; // Define all browser names you are supporting in framework enum BrowserNames { CHROME,FIREFOX } public class WithEnum { // Comparing value using if else private WebDriver launchBrowser(BrowserNames browserName) { WebDriver driver = null; if (browserName == BrowserNames.CHROME) { WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); } else if (browserName == BrowserNames.FIREFOX) { WebDriverManager.firefoxdriver().setup(); driver = new FirefoxDriver(); } return driver; } // Comparing value as switch private WebDriver launchBrowserSwitch(BrowserNames browserName) { WebDriver driver = null; switch (browserName) { case CHROME: WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); break; case FIREFOX: WebDriverManager.firefoxdriver().setup(); driver = new FirefoxDriver(); break; } return driver; } @Test public void googleTestChrome() { WebDriver driver = launchBrowser(BrowserNames.CHROME); driver.get("https://www.google.com"); driver.close(); } @Test public void googleTestFirefox() { WebDriver driver = launchBrowserSwitch(BrowserNames.FIREFOX); driver.get("https://www.google.com"); driver.close(); } }
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.
nice blog