How to implement code re-usability in Selenium Automation Framework

In this post, you will learn kind of coding pattern which is very helpful in maintaining our automation code. This post is written insight to help beginners. We'll learn that instead of writing a linear script, we should create page (action) methods which in general contain actions which we are going to perform on our web software. 

Let's say, we have a login functionality in our software and we have to automate it. In that case we'll create a method named login and write the commands like, entering username, password and click login in that method. Now we can use that method wherever we need it in other test cases as well. Benefits of using action methods are:

  • Code Re-usability
  • Better maintainability of code
Sharing below a sample script, in which I'm entering data in a web form using action methods:





You can also place these action methods in some other class like Common.java. And if you want to re-use any of the methods in your Selenium script, just create the object Common class and call that method. Like .,

// Create Object
Common commonObj = new Common();

//Call method
commonObj.openURL("https://www.techlistic.com/p/selenium-tutorials.html");

Happy Learning! :)


Top 25 Must to Know Selenium Commands  << Previous      ||      Next >>  10+ Selenium Webdriver Practice Assignments


Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10 Best Demo Websites for Automation Testing Practice

Automate 'User Registration' of E-commerce Website using Selenium

Automate Browser Actions on Godaddy.com using Selenium

Selenium WebDriver Browser and Navigation Commands