Sunteți pe pagina 1din 19

What Is Maven?

Why Do We Use Maven


In Selenium?

What Is Maven?
Apache Maven Is Powerful Java project management and build management
tool. That means we can manage java project builds very easily using maven.
Maven can helps you to minimize your project and build management time and
efforts. It Is fine to manage project manually If It Is small. But If project Is very
large or there are many projects then It Is very hard for developer to manage each
of them manually.
Using maven build tool, You can setup all those things which required to run your
project code Independently. Maven provides common platform to generate source
code, compiling and packaging code.
Main objectives of maven build tool are as bellow.

It makes project build process easy.

It provides easy and uniform build system.

It provides quality project document Information.

Managing project dependencies.

Provides guild lines for better project management practices.

Facilitate easy and transparent migration to new features.

It allows to build project using project object model (POM).

It downloads required dependency's jar files automatically from Maven


central repositories.
Why To Use Maven In Selenium Project?
As Maven Is build management tool, It will helps you to manage your selenium
project's build easily. If you are thinking that you will get some extra functions
from maven to write selenium automate test cases then you are thinking wrong. It
Is build management tool and It will manage your selenium test project's build
compilation, documentation and other related project tasks It self. It will helps you
to create right project structure, add and manage jar files In project's build path
etc..
Maven uses POM.xml configuration file which kept all project configuration related
Information. For selenium, You need to provide selenium webdriver version
related Information In POM.xml file and then It will download all required jar files
automatically and store It In local repository called m2. Later on If you wants to
change version of selenium webdriver then you need to modify version In
POM.xml file only. Then maven will download new version jar files automatically
and store In local repository. That means If you upgrade any dependency's version
In POM.xml file, First It will check that version's jar files are available or not In local

repository. If available then fine else It will download them from maven central
repository.
We will learn more about usage of maven In selenium project In my upcoming
posts. You will understand Its actual usage and usefulness once you will use It In
your project.

How To Download And Install Maven In


Eclipse Step By Step

We learnt what Is maven and why to use It In selenium project In PREVIOUS POST.
Now Its time to Install maven In eclipse. Follow the steps as described bellow to
Install Maven In Eclipse.
Prerequisites :
Java should be Installed and JAVA_HOME Environment variable should be
set.READ THIS POST to know more on this.
Eclipse should be configured and Installed In your system. READ MORE on
this page for more detail on how to Install eclipse.
Installing Maven In Eclipse
Step 1 : Open eclipse IDE and go to Help -> Install New Software menu as shown
In bellow Image. It will open new software Installation window.

Step 2 : Add bellow given URL In Work with text field of eclipse and press keyboard
ENTER button. It will search for maven software to Install.
URL = http://download.eclipse.org/technology/m2e/releases

Step 3 : On search completion, It will show "Maven Integration For Eclipse"


check box as shown In bellow Image.

Select that check box and click on Next button as shown In above Image. It will
start processing as bellow. It can take 1 to 2 minutes.

Step 4 : When above process completed, Install Remediation page will display as
bellow. Click on Next On that screen.

Step 5 : Next screen will be Install Detail. Click on Next on It.

Step 6 : Next screen will be Review licenses. Select "I accept the terms of the
licenses agreement." radio button and click on Finish.

It will start Installing maven In eclipse as bellow and It can take 5 to 20 minutes to
complete Installation. You can run It In background too.

Step 7 : At the end of maven Installation, It will show you message to restart
eclipse. Click on Yes button as shown bellow. It will restart your eclipse.

How to confirm Maven Is Installed In your eclipse


To confirm that maven Is Installed properly or not In your eclipse, You need to
open eclipse preferences window from menu Item -> Window -> preferences as
bellow. It will show you Maven In tree as bellow on preferences window. It means
maven Is Installed

Create New Maven Project In Eclipse


For Selenium WebDriver + TestNG

Before creating maven project In eclipse IDE for selenium webdriver, You
must be aware about maven and It should be Installed properly In your eclipse
IDE. Earlier we learnt about what Is maven In THIS POST and how to download and
Install maven In THIS POST. So If maven Is Installed In eclipse then you are ready
to create new maven project as described In bellow given steps.

Steps to create new maven project


Step 1 : First of all, Open eclipse IDE and go for creating new project from New ->
Other as shown In bellow Image.

It will open new project creation window.


Step 2 : Expand Maven folder In new project creation wizard window and select
Maven Project and then click on Next button as shown In bellow Image.

It will take you to project name and location selection screen.


Step 3 : On project name and location selection screen, You can choose work
space location for maven project by clicking on browse button. If wants to use
default work space location then select that check box and click on Next button as
bellow.

It will take you to Archetype selection screen.


Steps 4 : On Archetype selection screen, Select Maven-archetype-quickstart
option as bellow and click on Next button.

Step 5 : On next screen, Enter Group Id = STTA, Artifact Id = MavenProject and


Package = STTA.MavenProject as shown In bellow Image. Then click on Finish
button.

It will create new maven project In eclipse as bellow.

Now your maven project Is created. You can see there Is pom.cml file under your
project as shown above.
Step 6 : Now you need to add selenium webdriver and TestNG dependencies with
latest version In pom.xml file. Open pom.xml file and copy-paste bellow given code
In It and then save It.

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>STTA</groupId>
<artifactId>MavenProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MavenProject</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.44.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Current latest version of selenium webdriver Is 2.44.0. TestNG latest version Is


6.8.8. You can change It If version updated version release In future.
Step 7 : Delete existing AppTest.java file from src/testjava folder package and
create new class file under same package with name = WebDriverTest.java as
bellow.

Now copy-paste bellow given code In that WebDriverTest.java file and save It.
package STTA.MavenProject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class WebDriverTest {
WebDriver driver;
@Test
public void verifySearch() {
driver = new FirefoxDriver();
driver.get("http://only-testing-blog.blogspot.in/");
driver.quit();
}
}

Above file will show you errors as we do not have Included selenium and testng jar
files In project's build path. Here maven will do It automatically for us.

When you save It, It will start building work space by downloading required jar files
from maven central repository and store In local repository based on your
selenium webdriver and TestNG versions as bellow. It can take 5 to 20 minutes
based on your Internet speed.

Running webdriver project from pom.xml file


When above process get completed, You can run your project from pom.xml file.
To run It, Right click on pom.xml file and select Run As -> Maven test as shown In
bellow Image.

It will start executing webdriver test WebDriverTest.java file. This Is the way to
create and run webdriver test In eclipse using maven and testng.

Closing All Tabs Using Robot Class In


Selenium WebDriver

Earlier we learnt how to work with multiple window tabs In single browser window
In selenium WebDriver In THIS POST. Now supposing there are multiple tabs open
In browser window and you wants to close all of them In one shot then how you
will do It? Simple way Is using driver.quit();
method of selenium WebDriver. Can we do It using any other way? Yes.
Earlier we have used java robot class to save Image from page In THIS POST. Robot
class Is useful to send key-press and key-release events. We will use same thing
here to perform keyboard ALT + SPACE + "c" (Shortcut key) key-press events to
close all tabs of browser. We can perform this key-press event sequence very easily
using Robot class. I am suggesting you to use always WebDriver's driver.quit();
method to close all tabs of browser. Intention of this post Is to make you more
aware about Robot class usage with selenium WebDriver so you can perform such
tricky actions easily whenever required.
Bellow given example has used Robot class In @AfterTest method to close browser
tabs using ALT + SPACE + 'c' key-press event sequence. Run It In your eclipse IDE to
check how It works.

package Testing_Pack;
import
import
import
import
import
import
import
import
import
import
import

java.awt.AWTException;
java.awt.Robot;
java.awt.event.KeyEvent;
java.util.concurrent.TimeUnit;
org.openqa.selenium.By;
org.openqa.selenium.Keys;
org.openqa.selenium.WebDriver;
org.openqa.selenium.firefox.FirefoxDriver;
org.testng.annotations.AfterTest;
org.testng.annotations.BeforeTest;
org.testng.annotations.Test;

public class Tabs {

WebDriver driver;
Robot rb;
@BeforeTest
public void setup() throws Exception {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://only-testing-blog.blogspot.in/2014/04/calc.html");
}
@Test
public void openTab() {
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
driver.get("http://only-testing-blog.blogspot.in/2014/05/form.html");
switchToTab();
driver.findElement(By.xpath("//input[@id='6']")).click();
driver.findElement(By.xpath("//input[@id='plus']"));
driver.findElement(By.xpath("//input[@id='3']"));
driver.findElement(By.xpath("//input[@id='equals']"));
switchToTab();
driver.findElement(By.xpath("//input[@name='FirstName']")).sendKeys("hi");
driver.findElement(By.xpath("//input[@name='LastName']")).sendKeys("test");
switchToTab();
String str =
driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value")
;
System.out.println("Sum result Is -> "+str);
}
public void switchToTab() {
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
driver.switchTo().defaultContent();
}
@AfterTest
public void closeTabs() throws AWTException {
//Used Robot class to perform ALT + SPACE + 'c' keypress event.
rb =new Robot();
rb.keyPress(KeyEvent.VK_ALT);

rb.keyPress(KeyEvent.VK_SPACE);
rb.keyPress(KeyEvent.VK_C);
}
}

This way we can use Robot class to perform keyboard shortcut key operations.

Install WebDriver Element Locator


Add-On To Get XPath Of Element
Easily

As you know, XPath Is most Important element locator to automate you web
application using selenium WebDriver. There are many other locators are also
available In WebDriver but If you don't know about XPath locator then It will be
very hard for you to prepare WebDriver test cases. Earlier we learnt how to find
XPath of element manually In THIS POST.
Finding XPath manually Is time consuming process and WebDriver Element Locator
Firefox add-on can help you to get element's XPath very easily. If you will use this
add on then you do not need to Inspect element using firebug but you will get It In
just two clicks. I am using this add-on from very long time and It helps me very
much so let me tell you how to Install and use It If any one of you want It.
Steps to download and Install WebDriver Element Locator Add-On
If you have Installed any firefox add-on before then you will be well aware about
Installation process. Ok.. let me tell you how to Install It.

Open Firefox Browser.


Go to https://addons.mozilla.org/en-US/firefox/. You will sind add-ons
search box at top right corner of that page.
Search for add on using "WebDriver Element Locator" keyword. It will show
you list of add on In search result.
Click on "Add to Firefox" button of "WebDriver Element Locator" add on as
shown In bellow Image.

It will show you popup to Install "WebDriver Element Locator" add on. Click
on Install Now button as bellow. It will start Installing add on.

At the end of Installation, It will show you add on successfully Installed


message as bellow. That means add on Is Installed successfully. You can
view Installed add on at Firefox browser menu -> Tools -> Add ons ->
Extension section.

How to get Element's XPath using add on

Now you can get any element's XPath In just two clicks. Follow the steps given
bellow to get XPath of Google search box.
Open http://www.google.com/ In Firefox browser.
Right click on Google search text box. It will show you multiple language
options (C# Locators, Java Locators, Python Locators, Ruby Locators etc.) to
get xpath syntax of Google search box as bellow. You can select language as
per your requirement. I am using Java so I need XPath string In java
compatible format.

As sown In above Image, It will show you many XPath syntax for Google
searchbox when move mouse on Java Locators. XPath syntax marked with
YES sign will work properly In your script so click on any of the XPath syntax
to copy It.
Now you can paste It In your eclipse IDE directly and use It In your script.

This way you can get any web element's XPath In just two clicks as shown above
using WebDriver Element Locator Add-On.