How to configure Selenium WebDriver with Spring Boot for UI testing? How to configure Selenium WebDriver with Spring Boot for UI testing? selenium selenium

How to configure Selenium WebDriver with Spring Boot for UI testing?


Finally this is what I tried and it worked.

pom.xml

 <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.seleniumhq.selenium</groupId>            <artifactId>selenium-java</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.seleniumhq.selenium</groupId>            <artifactId>selenium-api</artifactId>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.seleniumhq.selenium</groupId>            <artifactId>selenium-chrome-driver</artifactId>            <scope>test</scope>        </dependency>      <!--  <dependency>            <groupId>io.github.bonigarcia</groupId>            <artifactId>webdrivermanager</artifactId>            <version>2.2.3</version>            <scope>test</scope>        </dependency>-->    </dependencies>

src/test/resources (manual download of exe required)

chromedriver.exe 

BaseSeleniumTests.java

public abstract class BaseSeleniumTests {    private static final String CHROMEDRIVER_EXE = "chromedriver.exe";    protected WebDriver driver;    @Before    public void setUp() {        String driverFile = findFile();        DesiredCapabilities capabilities = DesiredCapabilities.chrome();        ChromeDriverService service = new ChromeDriverService.Builder()                .usingDriverExecutable(new File(driverFile))                .build();        ChromeOptions options = new ChromeOptions();        options.addArguments("--no-sandbox"); // Bypass OS security model, MUST BE THE VERY FIRST OPTION        options.addArguments("--headless");        options.setExperimentalOption("useAutomationExtension", false);        options.addArguments("start-maximized"); // open Browser in maximized mode        options.addArguments("disable-infobars"); // disabling infobars        options.addArguments("--disable-extensions"); // disabling extensions        options.addArguments("--disable-gpu"); // applicable to windows os only        options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems        options.merge(capabilities);        this.driver = new ChromeDriver(service, options);    }    private String findFile() {        ClassLoader classLoader = getClass().getClassLoader();        URL url = classLoader.getResource(CHROMEDRIVER_EXE);        return url.getFile();    }    @After    public void tearDown() {        if (driver != null) {            driver.quit();        }    }

GoogleSearchPageTraditionalSeleniumTests.java

@RunWith(SpringRunner.class)@SpringBootTestpublic class GoogleSearchPageTraditionalSeleniumTests extends BaseSeleniumTests {    @Test    public void getSearchPage() {        this.driver.get("https://www.google.com");        WebElement element = this.driver.findElement(By.name("q"));        assertNotNull(element);    }}


Please see my answer here Use Webdriver Manager on how to invoke browser without the need to download the binary files (You will need to add a dependecy to your maven) then the rest is simple , so your code will looks like this:

    @Test    public void getSearchPage() {    WebDriverManager.firefoxdriver().setup();    WebDriver driver = new FirefoxDriver();    driver.webDriver.get("https://www.google.com");    WebElement element = driver.webDriver.findElement(By.name("q"));    assertNotNull(element);}


You will need to

  • add a Maven dependency to the selenium-ie-driver
  • Download the Internet Explorer Webdriver
  • Set the system property webdriver.ie.driver to the path of the executable

Selenium starts this as a background process and communicates via network connection. The webdriver takes control of the Internet Explorer.