Selenium TESTNG and Eclipse IDE
Selenium TESTNG and Eclipse IDE
Abstract: This article is a guide for the beginners when they use Selenium, Testing
and Eclipse IDE for automation testing
1. Introduction
Selenium IDE/Web driver has becoming a very popular testing tool as it is an open source. This is a GUI
Automation tool. This is a beginners guide to install and use Selenium with Testing and Eclipse IDE. This
gives a step by step installation guide to Kick-start an Automation journey.
This document will describe one of the ways of setting up the environment automating Web
Applications. Described here will be a sample test script, object repository build up using XPath or CSS,
reporting configuration for the test run.
Figure 2
Figure 4
2. Selenium Introduction :
The Selenium-IDE (Integrated Development Environment) is the tool you use to develop your Selenium
test cases. Its an easy-to-use Firefox plug-in and is generally the most efficient way to develop test
cases. It also contains a context menu that allows you to first select a UI element from the browsers
currently displayed page and then select from a list of Selenium commands with parameters pre-defined
according to the context of the selected UI element. This is not only a time-saver, but also an excellent
way of learning Selenium script syntax.
Figure 5
Figure 6
Figure 7
The web contains static and dynamic objects/elements. For static elements Ids are same at any time.
Ex: Button , Text box , Text Area ids .Using Inspect this element we can identify element id xpath.
1. While using Selenium IDE, sometimes the dynamic elements cannot be identified. These have to
be identified at run time.
2. Static elements are like button, text box, text area. For static elements Ids are same at any time.
3. But for Dynamic elements Ids will change at execution time.
4. DOM Inspector is useful to identify dynamic elements with common id at any time.
5. Here Xpath is creating id to identify dynamic element.
6. Example : Identify one checkbox id among multiple check boxes ids.
7. Check box id will change dynamically. That time Xpath using Dom inspector is useful to identify
the Id.
1.
2.
3.
4.
CSS
Another way of locating the objects is by using CSS Selectors instead of XPATH:
Open the Selenium IDE > Options > Options> Locator Builders > Make sure CSS is present in the list
Figure 10
For this we can use DOM Inspector to get the elements, its attributes and its hierarchy
To locate a text field >
1.
2.
3.
4.
5.
Figure 11
3.Install eclipse.exe:
1.
2.
3.
4.
Figure 12
11. Once the installation is complete, open File> New> Other(or Ctrl + N) > Testing Class must be
listed in the window
Figure 16
Figure 18
5. Add the Selenium jar to the library ( File> Properties > Goto Libraries > Add External JARs .. >
Select the selenium-server-standalone-2.25.0.jar or the latest selenium jar
Figure 19
It should be added
Figure 20
)
6. Create a new Class MyFirstAutomationScript.java
Figure 21
a.
b.
c.
d.
e.
Click on MyFirstAutomationScript.java
Click New
Click TestNG class
Click @BeforeTest
Finish
Figure 22
org.testng.annotations.Test;
org.testng.annotations.BeforeTest;
org.testng.annotations.AfterTest;
org.openqa.selenium.By;
org.seleniumhq.*;
com.thoughtworks.selenium.Selenium;
com.beust.testng.*;
static org.testng.Assert.*;
//Write the setup steps like starting the Selenium server, bringing
the application url up
}
@AfterTest
public void afterTest() {
//Write the cleanup steps like logging out of the application,
stopping the Selenium server
}
}
11. Click on the java class and Select Run as > TestNG Test
12. As a beginner in Automation, the first steps are to give proper logging setup. In TESTNG, we
have Reporter class which has logging functions which can be used.
Reporter.log("PASS : <font color=\"green\"><b>"+ "Test case has
passed"+"</b></font><br/>");
The below given code is an example of how java classes and functions can be linked to xml files
Java classes and Functions:
We should write java functions to connect xml file and run the web application.
Write java class for each web component of application .Each java class is one component.
Template consists of set of functions as well as abstract methods.
Write java functions for each and every element (buttons, textbox) action items functions of particular
component.
Sample java code:
//import packages
//import Templates;
Each Java function should be called inside xml file, values passed inside xml file will be passed to java
function and execution will be done according to xml file steps.
XML file:
1. XML stands for EXtensible Markup Language
2. XML is a markup language much like HTML.
3. Here every test script is written in xml file format with steps having element value and ID of the
element.
4. Each XML file generation is used to run each test script.
Example:
<TestStep action="function name" type="template path">
<StepDatas>
<StepData value="elementid"></StepData>
<StepData value="elementvalue"></StepData>
</StepDatas>
</TestStep>
5.
Each Test step is one single action in web application. Web page element id, values will be pass
as single individual steps .XML test script generation is main part in automation web application.
Conclusion:
This article provides the outline for setting up all the tools required to start automation using Selenium
with Java Eclipse IDE. Additional to this, knowledge of Core JAVA is required to test the functionalities.
This document in brief overview these points:
1.
2.
3.
4.
5.