We’ll start by adding the maven dependency for selenium which contains most of the web-drivers and we’ll be using Firefox web driver for testing.


Let’s put selenium into action. Basic idea is to find the DOM elements and interact with them and here’s a simple snippet which shows how we can test login failure to gmail.

        WebDriver driver = new FirefoxDriver();
        driver.findElement(By.id("Email")).sendKeys("randomUsername"+new Date().getSeconds());

        String message = driver.findElement(By.id("errormsg_0_Passwd")).getText();
        assert message.contains("The email or password you entered is incorrect.");