Selenium Object Bank - SOB
Google’s
Selenium Web Driver has took a pace where automating a web application has made
easier than ever. With very few methods exposed by the API, almost all the
actions can be achieved that are necessary for working with a web application.
In automation script where all the application Objects are given within,
maintaining these Objects can be tiresome. Readability of the Script is not as
easy as it looks. Understanding the code at later point of time can be real
painful. Addressing these issues users including myself had followed various
approaches for minimizing the complexity, one such is implementing the Page
Object Pattern, or may be storing all the Objects in an external property file,
Providing detailed comments for the Objects, or any other user defined
approach.
Keeping these in mind, I wondered how we could deal with all these cumbersome
tasks using one single solution. There took the birth of Selenium Object Bank
(SOB) that can address the following questions.
·
Storing
all the Objects in a central location
·
Changing
Object properties without any modification on the Script
·
Readability
and clear understanding of the automation steps performed by just looking into
the Script
·
Minimizing
the Script code by Object notation
·
Creating
Robust Scripts
·
Re
usability of the Objects
·
Identifying
the Objects with different locators other than those provided by Web Driver
(id, name, xpath, css, class, tagName)
Sample Code
A
Sample code for automating WordPress website using regular approach
driver.findElement(By.id("user_login")).sendKeys("admin");
driver.findElement(By.id("user_pass")).sendKeys("demo123");
driver.findElement(By.id("wp-submit")).click();
driver.findElement(By.linkText("Posts")).click();
driver.findElement(By.linkText("Add New")).click();
driver.findElement(By.id("title")).sendKeys("Sample");
driver.findElement(By.id("publish")).click();
driver.findElement(By.linkText("All Posts")).click();
driver.findElement(By.id("post-search-input")).sendKeys("Sample");
driver.findElement(By.id("search-submit")).click();
driver.findElement(By.xpath("/html/body/div/div[3]/div[2]/div/div[4]/form/table/thead/tr/th/input")).click();
WebElement
selectBulkOption = driver.findElement(By.name("action"));
Select
select = new Select(selectBulkOption);
select.selectByVisibleText("Move to Trash");
driver.findElement(By.id("doaction")).click();
Same
code when used with SOB
setWebDriverObject(driver);
uname().sendKeys("admin");
pass().sendKeys("demo123");
login().click();
posts().click();
addNew().click();
postTitle().sendKeys("Demo Post");
publish().click();
allPosts().click();
searchBox().sendKeys("Demo Post");
searchPostsButton().click();
tickAllCheckbox().click();
postActions().selectByVisibleText("Move to Trash");
applyButton().click();
using
SOB, it makes easier for a person to read the program. It hides the Object
details keeping them separately.