Selenium + Java · Lesson 40 of 44

0/44 done

Module 40: Cross-Browser Testing

40. Dynamic Multi-Browser Driver Factory (Chrome, Firefox, Edge)

16 minlesson

Your task

Implement normalizeBrowserName:

  1. Accept String browser.
  2. If browser is null, return "chrome".
  3. Return browser.trim().toLowerCase().

Write it in the editor, then press Check answer (Ctrl + Enter).

Cross-Browser Driver Factory

java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.edge.EdgeDriver;

public class DriverFactory {
    public static WebDriver createDriver(String browser) {
        switch (browser.toLowerCase()) {
            case "firefox":
                return new FirefoxDriver();
            case "edge":
                return new EdgeDriver();
            case "chrome":
            default:
                return new ChromeDriver();
        }
    }
}

Key Takeaways & Walkthrough Summary

Users interact with your product across various browsers. In this lesson, you will architect a flexible DriverFactory that dynamically instantiates ChromeDriver, FirefoxDriver, or EdgeDriver based on configuration parameters. Let's build a cross-browser factory!

Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Can Safari be automated on Windows?

No, Safari and safaridriver run only on macOS. Cross-browser testing for Safari from Windows requires Selenium Grid or cloud machines.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip