This post contains the information for configure the Eclipse with Selenium
Webdriver, JUnit and Ant.
Pre-Requisite –
Java JDK
should be installed on system. To verify and set the JAVA_HOME, follow the
below steps –
For
verification of Java installation on System -
C:\Users>java -version
java version "1.7.0_06"
Java(TM) SE Runtime Environment (build
1.7.0_06-b24)
Java HotSpot(TM) Client VM (build
23.2-b09, mixed mode, sharing)
Configure Eclipse -
- Download Eclipse (eclipse-SDK-4.2-win32.zip) from the site www.eclipse.org.
- Extract the zip file and put the eclipse folder in any directory (In my case – D:\eclpse for Windows).
- Launch the eclipse.exe file. Shows below screen.
- Enter the workspace location and Click on Ok button.
- Eclipse editor will launch. Close the Welcome screen. Shows below screen.
Configure JUnit in Eclipse –
Advantages:
- Very good reporting structure is available
- Can generate XML, HTML reports
- There are options available to create test methods, test suites, etc
- Utilize Selenium IDE or Firebug / Firepath to record test scripts
Disadvantages:
- We will not be able to define our own reporting format
Steps to Execute JUnit Scripts
- Install Eclipse (See the section Configure Eclipse) and launch it.
- Download the JUnit jar file from the site - https://github.com/KentBeck/junit/downloads. The latest JUnit jar file is unit-4.10.jar
- In Eclipse, Click File > New > Java Projects.
- Shows below screen. Enter the project name (ex – TestLink) and click on Finish.
- New project will create and shows in Eclipse editor.
- Right click on the project name and Select New->Folder. Create a folder “lib” under the project name.
- Download the below jar files and placed under above created “lib” folder.
selenium-java-2.29.0.jar
selenium-java-standalone-2.29.0.jar
junit-4.10.jar
- Right click on project name and select Properties. Click on Java Build Path.
- Click on Libraries tab.
- Click on Add External Jars. Add all the Jar files (Selenium and junit) from above create “lib” folder. It looks like the below screen –
- Click on Ok button.
- Execute the scripts JUnitExample.java and JUnitExample1.java. For executing this, right click on this script, and select Run As-> JUnit Test.
Scripts Details –
- JUnitExample_SingleTest.java – This JUnit script contains only one test so we have the annotation @Before, @After and @Test.
|
Script Name - JUnitExample_SingleTest.java
|
|
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class JUnitExample_SingleTest
{
WebDriver
driver;
public
static WebElement tlElement;
@Before
public
void setUp() throws Exception
{
driver=
new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(15,
TimeUnit.SECONDS);
driver.get("http://localhost/testlink/login.php");
driver.findElement(By.xpath("//input[@name='tl_login']")).sendKeys("admin");
driver.findElement(By.xpath("//input[@name='tl_password']")).sendKeys("admin");
driver.findElement(By.xpath("//input[@name='login_submit']")).click();
}
@Test
public
void testProject() throws InterruptedException
{
driver.switchTo().frame("mainframe");
driver.findElement(By.xpath("//a[contains(text(),'Test
Project Management')]")).click();
driver.findElement(By.xpath("//input[@name='create']")).click();
Thread.sleep(20);
tlElement=driver.findElement(By.xpath("//input[@name='active']"));
tlElement.click();
}
@After
public
void tearDown() throws Exception
{
driver.switchTo().defaultContent();
driver.switchTo().frame("titlebar");
driver.findElement(By.xpath("//a[contains(text(),'Logout')]")).click();
driver.quit();
}
}
|
- JUnitExample_MultiTests.java - This JUnit script contains more than one tests so we have the annotations @BeforeClass, @AfterClass and @Test. In that case, SetUp and teardown methods will execute only one time i.e before test execution and after test execution completes.
|
Script Name - JUnitExample_MultiTests.java
|
|
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class JUnitExample_MultiTests
{
public
static WebDriver driver;
public
static WebElement tlElement;
@BeforeClass
public
static void setUp() throws Exception
{
driver=
new FirefoxDriver();
driver.get("http://localhost/testlink/login.php");
driver.manage().timeouts().implicitlyWait(15,
TimeUnit.SECONDS);
}
@Test
public
void test1() {
System.out.println("@Test test1()");
driver.findElement(By.xpath("//input[@name='tl_login']")).sendKeys("admin");
driver.findElement(By.xpath("//input[@name='tl_password']")).sendKeys("admin");
driver.findElement(By.xpath("//input[@name='login_submit']")).click();
}
@Test
public
void test2() {
System.out.println("@Test
test2()");
driver.switchTo().frame("mainframe");
driver.findElement(By.xpath("//a[contains(text(),'Test
Project Management')]")).click();
driver.findElement(By.xpath("//input[@name='create']")).click();
tlElement=driver.findElement(By.xpath("//input[@name='active']"));
tlElement.click();
}
@AfterClass
public
static void tearDown() throws Exception
{
driver.switchTo().defaultContent();
driver.switchTo().frame("titlebar");
driver.findElement(By.xpath("//a[contains(text(),'Logout')]")).click();
driver.quit();
}
}
|
-
Configure Eclipse + WebDriver + JUnit + ANT
For Configuring
Ant, see the below steps –
- Download ANT from Apache web site (http://ant.apache.org/bindownload.cgi).
- Unzip the ant and copy to C:\Ant folder (or can choice your any hard disk folder). In my case, I kept under D:\Applications\Ant.
- Configure environment variables: ANT_HOME
Script Execution with Ant –
- Will use the same above created Junit script.
- First, need to create build.xml file. See the below xml file corresponding to JUnitExample_SingleTest.java and JUnitExample_MultiTests.java scripts.
|
<?xml version="1.0" encoding="UTF-8"?>
<project name="TestLink" default="exec"
basedir=".">
<property
name="src" value="./src" /><!-- Change it to your
source folder -->
<property
name="lib" value="./lib" /><!-- Change it to your
lib folder -->
<property
name="bin" value="./bin" /><!-- Change it to your
bin folder -->
<property
name="report" value="./report" /><!-- Change it to
your report folder -->
<path
id="test.classpath"><!-- Creating a classpath for use while
compiling -->
<pathelement
location="${bin}" />
<fileset
dir="${lib}">
<include
name="**/*.jar" />
</fileset>
</path>
<target
name="init"><!-- Initialization target which deletes and
recreates the binary folder.-->
<delete
dir="${bin}" />
<mkdir
dir="${bin}" />
</target>
<!--Section1-->
<target
name="compile" depends="init"><!-- Target for
compiling the source folder -->
<javac
source="1.7" srcdir="${src}" fork="true"
destdir="${bin}" >
<classpath>
<pathelement
path="${bin}">
</pathelement>
<fileset
dir="${lib}">
<include
name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target
name="exec" depends="compile">
<delete
dir="${report}" />
<mkdir
dir="${report}" />
<mkdir
dir="${report}/xml" />
<junit
printsummary="yes" haltonfailure="no">
<classpath>
<pathelement
location="${bin}" />
<fileset
dir="${lib}">
<include
name="**/*.jar" />
</fileset>
</classpath>
<!--Section2-->
<formatter
type="plain"/>
<formatter
type="xml"/>
<batchtest fork="yes" todir="${report}/xml">
<fileset
dir="${src}">
<include
name="**/JUnitExample*.java"/>
<exclude
name="**/Example*.java"/>
</fileset>
</batchtest>
</junit>
<junitreport
todir="${report}"><!-- This section generated the Junit
report in html to "html" folder from the xml file. -->
<fileset
dir="${report}/xml">
<include
name="TEST*.xml" />
</fileset>
<report
format="frames" todir="${report}/html" />
</junitreport>
</target>
</project>
|
Note –
If you are using any other Java version then go to <!--Section1--> and
set the value of “javac source”. By default, its set to 1.
- Go to command prompt – > navigate to folder where the build.xml is kept ( In my case, the path is - D:\Automation\Selenium\TestLink\TestLink).
- Execute the following command: ant (press enter). This should fire the Selenium tests and generate the HTML / XML reports and put them into the report folder.
- After the execution, it creates one more folder – > report and it contains HTML reports that should look as below (Go to the “report” directory and double click on the “index.html” to view the complete report):










No comments:
Post a Comment