Parameterization of a particular test is needed in order to avoid hard coding of values. If a test is recorded with Selenium IDE, then all the values (e.g. locators) are hardcoded making it very difficult to execute with subsequent changes in the application. In this post let us discuss a simple form of parameterization with creating a separate class for holding static variables for each locator.
The following example actually demonstrates the usage of parameterization.
Let us assume that our test suite contains more than 500 test cases and more than 50 different screens and we have thousands of objects on the application under test. Multiple test cases may be (more than 10) using a particular screen (e.g. Contract creation screen). If there is a change request from the client to enhance / change any of the object, then we need to update all the test cases.
In QTP, there is a concept called “Object Repository”. All the objects are added to Object Repository and the objects are used in the test. If there are any changes in the application then we need to update only Object Repository.
But in Selenium, there is no concept of Object Repository. Using static variables we can create a similar kind of feature in order to handle frequent changes in the application under test (AUT).
We will have a look at the below code,
AdvancedSearch.java
package com.selftechy.parameterization;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
public class AdvancedSearch extends SeleneseTestCase {
private static SeleniumServer seleniumServer;
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in/");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}
@Test
public void testAdvancedSearch() throws Exception {
selenium.open("http://www.google.com/");
selenium.click("link=Advanced search");
selenium.waitForPageToLoad("30000");
selenium.type(ORas.wedAllTheseWords, "selftechy, selenium, eclipse");
selenium.select(ORas.wlsResultsperPage, "label=20 results");
selenium.click(ORas.btnAdvancedSearch);
selenium.waitForPageToLoad("30000");
}
@After
public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}
}
ORas.java:
package com.selftechy.parameterization;
/*
* Author - Seetaram Hegde
* This class holds all static values for objects of "Advanced Search"
* page on Google Search
* ORas - ObjectRepositoryAdvancedSearch
* wed - Web Edit
* wls - WebList - Dropdown
* btn - Button
*/
public class ORas {
public static final String wedAllTheseWords="as_q";
public static final String wedExactWord="as_epq";
public static final String wlsReadingLevel="tbs";
public static final String wlsResultsperPage="num";
public static final String btnAdvancedSearch="//input[@value='Advanced Search']";
}
ORlt.java:
package com.selftechy.parameterization;
/*
* Author - Seetaram Hegde
* This class holds all static values for objects of "Language Tools"
* page on Google Search
* ORlt - ObjectRepositoryLanguageTools
*/
public class ORlt {
public static final String wedSearchFor="clirq";
public static final String wedTranslateText="source";
public static final String btnTranslateandSearch="Translate and Search";
}
In this example, I created two different classes “ORas” and “ORlt” for two different screens. Both of these classes hold objects in the form of static variables. Instead of using locators directly in the tests we are going to use the locators in the form of static variables. Hence, there is no need to make any change to test cases, the only place where we need to make change is the “Class” containing static variable.
How can we use/link these ORas and ORlt with main test case?
With Selenium commands use ORas.wedAllTheseWords in the place of a locator
Ex:
selenium.type(ORas.wedAllTheseWords, “selftechy, selenium, eclipse”);
Where to change value. if i want to put different value for search condition where i have to put. in the ORas.java class or in the main class. please reply.
selenium.type(ORas.wedAllTheseWords, “selftechy, selenium, eclipse”); change “selftechy, selenium, eclipse” to “Test Automation with Selenium” (it should be done in the test method)
Hi seetharam,
Thanks for the valuable information. I was very much troubled with changes in object properties in web pages.
keep the objects outside your script so that you can modify them whenever needed..
Seetaram instead of putting as static variable, we can put all in Messages.properties file, Which will reduce the compile time. I mean to say no build is required, you can change properties values at any point of time.
Regards,
ravi
@Ravi
Yes..I tried to make things easier for the beginners!! Whatever you said is a good option.