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'.
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.
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'
After that it will display a screen as below which is responsible to create the POM.xml file as well.
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.
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.
Comments
Hi,
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
Doubt
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?
Use wait method
Hi,
If its printing the output correctly then the browser is closing automatically very fastly.Try to use the wait method or thread,sleep() method before println().it will help you to wait the browser to close.
reply
You are not giving URL properly .Try to give "https" .
like "https://www.google.co.in/".If you give the only "www" it will not open any URL
maven dependencies are not working.
Hi. I successfully created java project and converted it into maven project also. But when i am trying to add maven dependencies, there is an error occurs in pom.xml saying " Failure to transfer xalan:serializer:jar:2.7.2". Searched for it but nothing worked correctly.
Add new comment