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.

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.37.1</version>
</dependency>

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.get("http://mail.google.com/");
driver.findElement(By.id("Email")).sendKeys("randomUsername"+new Date().getSeconds());
driver.findElement(By.id("Passwd")).sendKeys("hello123");
driver.findElement(By.id("signIn")).click();

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