Monday, September 24, 2012
Selenium RC using Java Post 6 of 10
Wednesday, September 19, 2012
Selenium RC using Java Post 5 of 10
Today I’am going to tell you about UI mapping and Page Objects.
In your project create a Folder and names it as Resources
Now New à File -> and under the Resources folder create a new file and name it as locator.properties
Now in my locator file I need to create all control id’s of Login page. Here I’ll take username txt box , password txtbox and Login button
I can take it from my Login class
Now we need to have a method for find the locator file and get the details from the file
In my Test Base class I creates 2 variables
Now in my method I am going to get the locator file and get the details of property file in to a Hash Map
This is also a Beforeclass method
Now I should change my code in Login class to get the control id’s from property file (actually from the hashmap )
This is the new login class. Now I am getting the login page controls from the locator.properties file and for this I used the filereading and hash map.
Now I have to change the login object initiation method since new variable is added to the the constructor.
Test Base class
Now you can see that login object has 3 parameters now. (Locator Hash map in the new one)
So using Page objects you can improve the maintainability of the Test automation project. If new control added or existing control changes you can handle it using property file.
Tuesday, September 18, 2012
Selenium RC Using Java Post 4 of 10
Now I am going to talk about concept of page object. This is an extremely useful design pattern for UI tests as it abstracts away how we get to different pages or screens. Page Object design helps us create a DSL for our tests that if something were to change on the page we don't need to change the test, we just need to update the object that represents the page
Now I’m going to create a new class which is to place my all the functional methods related to Login
Page. Before that I create a new package which I am going to label as com.ABC.Test.Functional under this package I creates the class Login
public class Login {
public Selenium Sel;
public static String ptimeout;
public Login(Selenium Sel, String PtimeOut){
Sel = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost:8080/LogOn.aspx");
ptimeout = PtimeOut;
}
public void login(String uname,String pword){
Sel.windowMaximize();
Sel.open("http://localhost:8080/LogOn.aspx");
Sel.type("id=body_UserNameTextBox",uname);
Sel.type("id=body_PasswordTextBox", pword);
Sel.click("id=body_LogOnButton");
Sel.waitForPageToLoad(ptimeout);
}
}
Now I create the Selenium instance and login method and included all of login steps here. So I removed my Login steps from the testSucessLogin method.
Now I’m going back to TestBase class and create new method initPageObject()
Within this method I am going to create new object of Login class. (All the page objects will be initialize here )
public void initPageObject(){
login = new Login(Sel, SystemtimeOut);
}
I creates a new method “setup “ which is going to list all the setup variables . Here I am going to setup the “systemtimeout” I define this as a @Beforeclass and do the parameterization using TestNG xml.
@BeforeClass
@Parameters({"pageTimeOut"})
public void setUp(String systemtimeout){
SystemtimeOut = systemtimeout;
}
Now in my PageObject method I might get a problem because before create the objects I need to run the initSelenium and Setup method. For this I use TestNG annotation - @BeforeClass(dependsOnMethods = { <method names})
@BeforeClass(dependsOnMethods = { "setUp","InitSelenium" })
public void initPageObject(){
login = new Login(Sel, SystemtimeOut);
}
Now I need to call my login method under my testSucessLogin Test method
login.login(uname, pword);
Monday, September 10, 2012
Selenium RC using Java Post 3 of 10
Selenium RC with java Post 3 of 10
In this post I’ll discuss about How to handle drop downs, Text fields ,Date pickers, Data Grids and Paging using Selenium.
How to get the and set values from Text fields
Sel.type("id=body_body_shortNameTextBox", "manu11");
Get value from Text box
Sel.getValue("id=body_body_shortNameTextBox");
How to get and set values from Drop down
Can select values from 3 methods
From name of the item , ID and Index
Sel.select("body_body_countryDropDownList", "label=Singapore");
Sel.select("body_body_countryDropDownList", "ld=10");
Sel.select("body_body_countryDropDownList", "lndex=118");
Get the selected value in drop down
Sel.getSelectedLabel("body_body_countryDropDownList");
Sel.getSelectedId("body_body_countryDropDownList");
Sel.getSelectedIndex("body_body_countryDropDownList");
Data Grids
Following is my datagrid
Now I need to read the data grid (which has paging ) and then Click the edit button on particular row which I need to edit.
Now things I need to consider are
Unique name and it’s column id which I can read the datagrid
Control name of the icon which I need to click to go to the edit page
No of paging which I need to traverse through to find the name (or element ) which I am search for
Now to start up I find the column name of the field I am looking for - it’s column 2 (I can find the column index using selenium IDE ) here it’s 1.
Element which I need to click is Body_body_activityGridView_selectImageButton (plus it has a unique id at the end and it’s increments with each row )
If I create a script to find the given name’s row and click the edit image as follows
for(int row=1;row<11;row++){
activityName = Sel.getTable("//table[@id='body_body_activityGridView']/tbody."+ row +".1");
if (activityName.equals("ManujayaAutomation")) {
Sel.click("id=body_body_activityGridView_selectImageButton_" + (row-1));
Sel.waitForPageToLoad("30000");
break;
}
From this method I can find the find my activity name
Sel.getTable("//table[@id='body_body_activityGridView']/tbody."+ row +".1");
For getTable mehod I am passing the table id, row id and column id .
Now if my acivity name is in another page of this grid then I should handle the paging as well .
String noOfPagSentence = Sel.getText("css=span.gridPagerInfo");
int length = noOfPagSentence.length();
noOfPagSentence = noOfPagSentence.substring(noOfPagSentence.indexOf("of ")+3, length);
int noOfPage = Integer.parseInt(noOfPagSentence);
String activityName="";
int paging;
for(paging=1; paging<(noOfPage+1); paging++){
if (activityName.equals("ManujayaAutomation")){
break;
}
if (paging>1){
Sel.click("link=" + paging);
Sel.waitForPageToLoad("30000");
}
for(int row=1;row<11;row++){
activityName = Sel.getTable("//table[@id='body_body_activityGridView']/tbody."+ row +".1");
if (activityName.equals("YoosufAutomation")) {
Sel.click("id=body_body_activityGridView_selectImageButton_" + (row-1));
Sel.waitForPageToLoad("30000");
break;
}
}
if (paging == noOfPage+1){
break;
}
From the first 4 lines I am finding the no of pages which has in this grid
Using the first for loop I am handling the paging related things , I am traverse through each page in the grid until I find the wanted element.
Sunday, September 9, 2012
Selenium RC using Java Post 2 of 10
Run the Tests using TestNG Xml
You create a XML file and call the Test Suite to run using TestNG
Create a XML file -- New >>other >> XML >> XML file
In your xml file if you need to run the test suite you can define it like this
<suite name="ABC_Testing" verbose="1" parallel="false" thread-count="1">
</suite>
If you want to provide a Test name which displays with your results then XML will look like this
<suite name="ABC_Testing" verbose="1" parallel="false" thread-count="1">
<test name="ABC Testing" thread-count="1">
</test>
</suite>
Now if you need to just run a one test class and methods allocated to that then your file should look like this
<suite name="ABC_Testing" verbose="1" parallel="false" thread-count="1">
<test name="ABC_Testing" thread-count="1">
<classes>
<class name = "com.ABC.test.scripts.TestLogin">
<methods>
<include name="sucessLogin" />
</methods>
</class>
</classes>
</test>
</suite>
Now if you need to pass the data through your XML then you can parameterized your tests and then pass the data to your parameters using xml file
Now first you need to change your test to accept parameters.
@Test
@Parameters({"userName","password"})
public void testSucessLogin(String uname , String pword) {
Sel.windowMaximize();
Sel.open("http://ABC:8080/LogOn.aspx");
Sel.type("id=body_UserNameTextBox",uname);
Sel.type("id=body_PasswordTextBox", pword);
Sel.click("id=body_LogOnButton");
Sel.waitForPageToLoad("30000");cc
Now you need to define the parameters in the xml file and assign the values to them
<parameter name ="userName" value ="abc.admin@ABC.com"/>
<parameter name ="password" value ="Qwer1234"/>
Now the run the xml and see ………….
Thursday, September 6, 2012
Selenium RC Using Java - Post 1 of 10
Install Eclipse
Download TestNG latest jar file
Download Selenium server latest jar file
Install Selenium IDE
Create a new package for testscripts
Now in your source folder create a new folder name Lib
Now when you select each action you can find the command , target object and the values