Skip to content Skip to navigation
Check Our New Demo Website!

Basic example program using WebDriver Java Maven

Let us create a very basic example step by step to use Webdriver, Maven with Java.

Why we should use maven is because of many reasons : -

1. We need maven to get all our dependencies automatically, which also allows users to reuse same jars across multiple projects.

2. Each and every engineers in a project use the same jar dependencies due to the centralized POM.

3. It provides the complete structure with naming conventions which is easy to locate and execute tests.

4. We can automate the complete build procedure etc etc

Step 1: First create a Java project. File -> New -> Java Project. And name the Java Project as 'GoogleSearch'.

New Java project

Step 2: Create a Package with name called 'com.pack'. Right Click on the Src folder -> New -> Package.

Step 3: Create a class with name 'GoogleSearchTest'. Right Click on the Package folder -> New -> Class

Now the structure should look like below.

New Java project class

Now we will proceed to write basic selenium script to open Google chrome do some sort of verification and validations. Before doing that we need selenium jar to execute our selenium script.

We need to configure the build path using Right Click On Project folder -> Build Path -> Configure Build path. The you need to add the Jars.

Below the basic script to open Google website and print the title.

package com.pack;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GoogleSearchTest {
	
	public static void main(String...args) {
		
		WebDriver driver  = new FirefoxDriver();
		driver.navigate().to("http://google.com");
		String appTitle = driver.getTitle();
		System.out.println("Application title is :: "+appTitle);
		driver.quit();
	}

}

After executing the above java program we will get the output as below :

Application title is :: Google

Now let us see how we can convert java project to maven project using below steps: -

Step 1: Right Click on Project Folder -> Configure -> Select 'Convert to Maven Project'

Convert To Maven Project

After that it will display a screen as below which is responsible to create the POM.xml file as well.

New Maven Project

Now you need to add the Maven Dependency for selenium under dependencies tag which looks like below. Refer selenium website for more details on selenium Maven dependency : -

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

In order to get all java bindings for all browsers we need to use selenium-java dependency. So then we don't need to change any thing in pom.xml file.

If you want to Selenium Grid to distribute your tests over multiple machines / virtual machines using RemoteWebDriver implementation, we need to download selenium standalone server. To include Selenium server into your maven project, you need to add the following dependency to your pom.xml.

   <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.0.1</version>
    </dependency> 

Now Remove the jar file which we have uploaded manually. Now maven will take care the jar files. It will download all the required jars based on the dependency provided in pom.xml file

Refresh the project and see the jars files that are downloaded under maven dependencies.
New Maven Project

Now you can execute the same java file and the see the same output as earlier. In the next tutorial we will create a Test using'Selenium Webdriver, Maven, Java and TestNG.

Build Tools: 

Comments

Hi,
All the articles are extremely helpful and I am learning so much from them.
Is it possible you could show testing REST API as well? eg: selenium with soap UI

When I ran the above program, it ran without error and opened mozilla but, did not open google and neither did it print the message. This happens every time. Can someone please suggest me for this?

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.