Selenium - A Developer's tool -Surprised ??!!..find it out how !!

Selenium - A Developer's tool -Surprised ??!!..find it out how !!

Note: This article can also be looked upon for folks who are into automation of testing using Selenium (POM,PageFactory).

For understanding of basics of Selenium check my “Basics of Selenium - Webdriver” article by following me.

Audience: Developers,Testers

Pre-requisite : Need to know to work on Selenium IDE and the methods and events in Selenium.

I had been a hard core developer,development lead for 15+ yrs and I have had enjoyed the technologies that came into my way. I focused on different tools to innovate and be productive for day-to-day activities and also for coding and development of the project until I came across Selenium, which when explored, discovered a huge set of opportunities even in development.

If you are using TDD , (Junit or TestNG) to write unit test cases , what I found was that it only helped in testing the unit piece of code that is developed by a developer but not as a total the functional piece.

For eg., if a user story such as this which says - “As a user, I would like to login to the system with my valid credentials so that i can do my activities in the site and logout from the system”

As soon as, the developer picks up this user story to build his code, following are the steps one would carry out:

  • Builds the login screen with username,password(a classical login screen)
  • Tests for the validation of the fields, on clicking “Submit” button are done using Unit testing tools like Junit or TestNG.During this process, the developer may find bugs, fixes it and pushes a clean code to Configuration management repository (Such as VSS,CVS,Git etc).
  • Any Continuous Integration tools(for say Hudson), would pick the code from CM repository and integrate them , throws error if build fails.
  • Developer fixes again until build succeeds.

If you carefully note that there is no functional testing done which satisfies the above user story. All that we have done is just unit testing the fields with different values that satisfies both positive and negative inputs for the fields.

Let us see now how Selenium helps developers when used in conjunction with Junit or TestNG in Unit testing and also what I term it as “Functional or Behavioural” testing. We can closely achieve what cucumber is trying to achieve the same. This is one time activity and as you move on with consecutive Sprints, testing becomes easier as you would be building functionalities in parallel.

Moving on ,

1. Let us first build selenium code functionality first.

I have used Page Object Model (shortly called as POM and pageFactory class) of Selenium here, where all the “functionalities” are defined as pages. This is the key when building testing modules based on functionalities.

Functionalities i meant such as Login,Logout,menus etc.,as different classes.

For eg: for the given user case or user story, I would build a class file named SignIn and have only one function named login() with parameters for username,password and the next expect functionality which I would like to test.

A sample view of code is given below of the SignIn functionality defined in a class:

public class SignIn{
……..
logintoSite(String username,String password){
WebElement usr = driver.findElement(By.name(“name”));
WebElement passwd = driver.findElement(By.name(“pwd”));
usr.sendKeys(username);
passwd.sendKeys(password);
}
public <T>T login(String usrname,String pwd,Class<T> expectedPage) {
logintoSite(usrname, pwd);
return PageFactory.initElements(driver, expectedPage);
}

}

As you see from the code, the login() method 3rd parameter takes a generic class of typeClass and returns instance of the next functionality defined in another class.

2. Now let us build unit test cases for testing the user story through Selenium functionality.I am using Junit to test for my login user story which is as show below.

public class LoginLogoutTest extends TestCase {
WebDriver driver;

@Before
public void setUp() throws Exception {
driver = new HtmlUnitDriver();
}
@Test
public void test_Valid_Login_with_Logout() {
String sitename =”https://gmail.com/”;
String username = “user123”;
String password = “pass123”;

driver.get(sitename);
SignIn signfn = PageFactory.initElements(driver, SignIn.class);
SignOut so= signfn.login(username, password,SignOut.class);
so.signOut(SignIn.class);
}
@Test
public void test_InValid_Login() {
sitename =”https://gmail.com/”;
username = “user123”;
password = “@@##”;
driver.get(sitename);
SignIn signfn = PageFactory.initElements(driver, SignIn.class);
signfn.login(username, password,SignIn.class);
}

Lets us go through line by line of the test case,test_Valid_Login_with_Logout() :

Line 1 to 3 in test_Valid_Login_with_Logout() :

Variable sitename => URL which needs to be tested.

Variables username,password => the inputs to the login() method of SignIn class.

Line 4 in test_Valid_Login_with_Logout():

driver.get(sitename); => navigating to the mentioned url.

Line 5 in test_Valid_Login_with_Logout():

SignIn signfn = PageFactory.initElements(driver, SignIn.class);

=>PageFactory initialises the SignIn class.

Line 6 in test_Valid_Login_with_Logout():

SignOut so= signfn.login(username, password,SignOut.class);

=> Calling login() method with username, password and the next functionality to test which in this case is SignOut.class.

Note: The third parameter gives me freedom to navigate to my expected functionality class which is SignOut.class in this case. In case, if i want to check dashboard functionality, all i need to do is to pass Dashboard.class as the parameter. Hope you got I was trying to say.

Line 7 in test_Valid_Login_with_Logout():

so.signOut(SignIn.class); => calls the signOut() in SignOut Class.

As you can see that i have called SignIn class to execute the login() function that takes login username,password and the next function SignOut to transverse, which is another functionality by itself.

Now with both Selenium functionality classes and Junit test cases,as a developer, you can test your code for functionality based on different workflows, well before the code is picked up by Continuous Integration tools such as Hudson for integration to other modules.

From what I have seen from many articles and blogs that many automation testers, build POM that resembles the exact page. This is not scalable if we are looking to test across different projects. Building POM based on functionalities gives us the edge to use the same code across different projects.

This will ensure that more and more defects are reduced when developer builds the code.

You can also check more about selenium @ www.seleniumhq.org

Hope you liked my post.Please drop your thoughts.

Please follow me @ https://vishnurocks.tumblr.com/

要查看或添加评论,请登录

Vishnu V的更多文章

社区洞察

其他会员也浏览了