What is the most humbling experience you have ever had?

In August of 2016 I moved to Norway from Australia to be with my boyfriend. By the time I moved to Norway, I had lived in 3 different countries and 6 different cities, so the moving across the world…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Guide to create Selenium Automation Framework using Page Object Model.

In this world of machines & robots, we really don’t need to define why we need automation framework. But if you are new to test automation, you should check my earlier blog on Why we need Automation framework?

Most, in fact majority of applications today are written as web-based applications to be run in browser, There are a number of commercial (e.g. UFT) and open source (e.g. robot framework ) tools available for assisting with the development of test automation. But Selenium is possibly the most widely-used open source solution for web based applications. In this guide, we will cover how to setup the selenium test framework from scratch.

What is Selenium?

Selenium is software testing framework for web applications. It gives you flexibility to write code in various languages such as Java, Perl, Python, Ruby , Groovy and Scala. It’s open source and can be run on Windows, Linux or Mac machines and with different browsers like Chrome, Internet Explorer, Mozilla firefox etc. We can combine Selenium with the framework like TestNG which provides flexibility to run test cases in parallel and reduces execution time for test suite. Most big companies extensively use and maintain the Selenium.

So let’s dive in and set a Selenium Automation Framework from scratch.

Selection of Test Framework

While setting the automation framework, we would need to choose which test framework to use. Test Framework is software, which will enable us to run our tests as needed. There are two most common frameworks:

Difference between TestNG and JUnit

Both the framework are used for unit testing. TestNG is similar to JUnit with few more additional functionality. TestNG provides parallel execution, grouping, parameterization and dependency which are not available in JUnit. It also provides reporting in HTML & XML format.

Our choice of test automation framework is TestNG.

Now we have finalized the test framework, let’s start with the setup.

Prerequisite:

In order to use maven in eclipse, you must have maven plugin install in your eclipse (if you don’t have one, you can install using Help -> Install new software > type m2e -> Select maven integration for eclipse -> click finish).

Step 1: We need to create a Java project in eclipse using File -> New -> Java Project.

Step 2: Convert the project to Maven Project by right click on the the project->Configure->Convert to Maven Project.

Step 3: Now we need to add dependencies for Selenium and TestNG. Inside the pom.xml, create dependencies field under project field and outside of build field and add following dependencies.

Step 4: Convert the project to run as TestNG by right click on the project->TestNG->Convert to TestNG.

Afterwards this step you will notice a new testng.xml will get generated inside the Java project. You can modify the testng.xml as per your suite and test needs. You can also create multiple testng.xml based on your requirement and specify different set of parameters as per need.

By this step your setup is almost ready.

Step 5: Designing the framework: In order to create selenium automation framework, we need to first choose how to organize our framework and test cases.

This approach is called Page Object Model(POM). It helps make the code more readable, maintainable, and reusable.

What is POM?

Let’s start developing our own selenium framework using POM design pattern.

Step 6: Creating POM based framework

First create following packages inside the source folder as shown in below diagram.

Step 7: Creating page classes

We will store page related web element properties inside each class. We need to create different Java classes for each and every page. If there will be any changes in the object property you just need to modify in this class and it will automatically reflect everywhere in the project.

Step 8: Creating a action classes

Now, We need to create a class with common action on login page. In action classes we will perform action on the elements of the page class like entering any value in text box or clicking any element.

So let’s take same example and create action class on the Facebook login page. We have already created the page class to read the property of these elements, now we can perform action like enter username, enter password and click on login button using action class methods.

Let’s create a class for login action(LoginAction.java) and will create method which we will perform action on the web element of page class. For entering username and password and clicking on login, we will have all the three action in one method called login which accepts user name and password as argument and click on login button.

Step 9: Create the test case

Now we have created pages and actions for assistance, We will create a test class where we launch browser, pass test data on login page and validate the result. We will create different Java classes for different test cases.

First, create a new package name “com.company.saf.test” under the src folder.

Second, we need to create a base class called TestCase which will have methods to create instance of webdriver and which helps to launch the browser and close the browser when done.

For the same example, we will create LoginTestCases class that will extend the base class (TestsCase)and provide the url “www.facebook.com” for the browser to launch. We need to enter username as “testuser”, password as “testpassword” and click on Login button to access the Facebook home page.

We have already created reusable methods for this case in LoginAction class. Now we need to call the methods of LoginAction class and enter the valid input from LoginTestCase class.

Here its calling setup method which calls init method of base class (TestCase) by providing the url of facebook .

Verification: We can use different assert methods to verify actual with expected result. Here after entering credential user is navigating to Facebook home page hence we are verifying page title using assertEquals method. This test cases will pass if actual title is same as expected title else it will show test cases as Fail.

Step 10: Executing the test

There are different ways to execute your test scripts:

I hope these step will help you in understanding of Page Object Model, TestNG, and how to set up the automation test framework from scratch.

Happy Coding!!

Add a comment

Related posts:

Romeo and Juliet

Throughout the world, eight billion people have their perspectives, views, and attitudes on the idea and feeling of love. Love is seen in many different ways by everyone. Like attitude, love is…

Should Soccer Have Stricter National Eligibility Rules?

The GSC panel weighs in on the nationality eligibility debate in soccer, the Ben Woodburn situation with Wales, and possible solutions for all sports leagues.

Bring everyone to the conversation with Guest Invites

The best projects are the result of diverse teams being brought together for a common cause. Ideal candidates are not always the ones working in your organization, so we team up with external…