Challenges I encountered in Selenium test automation: Part 1

Chaya Thilakumara
Chaya Thilakumara
Published in
6 min readSep 29, 2018

The purpose of this note is to present my experiences in Selenium test automation, as a Business Analyst. Initially, I did not have any clue as to what automation test was about. First I started the test manually using mobile and web. In this process of trying I understood the automation to be the best way to increase the effectiveness and efficiency. Test automation saves time, also it improves the accuracy of repetitive tasks which could create human errors. This is my journey of Selenium test automation.

While I was engaged in automating the system, the most troublesome questions I encountered were the duplicated code, duplicated functionality, and duplicated usage of locators. If I changed a specific locator, I move through the whole code to correct that specific locator. Isn’t that a troublesome problem? Actually it was!

Finally, I found a solution

The approach I used as a solution for this is called Page Object Model(POM). It improves the readability, maintainability and re-usability of the code.

But, before moving on to the solution, let’s create a selenium maven project by using eclipse IDE. Then, it will be easy to explain the steps of arriving at the solution.

Maven and TestNG Project In Eclipse IDE

Open Eclipse IDE. Go to “Help” and click on “Install New Software”.

Help > Install New Software

Click on add button.

If you are installing Maven, enter the name as Maven and location as http://download.eclipse.org/technology/m2e/releases/ and click on “OK” button.

If you are installing TestNG, enter the name as TestNG and location as http://beust.com/eclipse/ and click on “OK” button.

Add Name and Location to install Maven
Add Name and Location to install TestNG

From the list in the above screen, select “Maven”|”TestNG” and click on “Next” button. Then, click on “Next” button until the screen below appears to accept “Terms and Conditions”. All the steps below are similar, when installing TestNG or Maven.

Accept terms and conditions and click on “Finish” button.

Accept terms and conditions to complete the installation
Installation in progress
Click on “Yes” button to continue with the installation
Click on “Yes” to restart Eclipse for the changes to take effect

Now installation is complete, and let’s examine how to create a Maven project. Click on “File” and go to “New”- “Other”

File > New > Other

Click on “Maven Project” and click “Next” button.

Select Maven Project

Next, you can select “Create a simple project” tick box and then, click on “Next” button. Enter the “Group Id”, “Artifact Id” and click the “Finish” button. Please note that Artifact Id is the project name.

Add Artifact id and Group ID
Structure of the project looks as this

The POM is the file where we add dependencies to a Maven project. Double click on pom.xml file and click on pom.xml tab. You can copy and paste below dependencies in your pom.xml file.

<dependencies><! --Selenium --><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>2.53.1</version></dependency><!- -TestNG --><dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>6.8</version><scope>test</scope></dependency></dependencies>
The dependencies are related to Selenium WebDriver, TestNG in our Maven Project.

Right click on project: Maven >Update project, and Maven dependencies in your project will appear.

After installing and creating maven project, let’s look at the solution for my troublesome issue.

Page Object Model(POM)

A web application or web site has multiple web pages and those pages offer different functionalities. As an example, Sign up page helps to register providing email, password and confirm password. There can be different pages like Login page, Sign Up page, and Registration page, etc.

In page factory framework each page in the web site is considered as an object.Web pages in a web application are represented by a class. The functionality offered by those web pages are represented by methods.We have to create a test class by calling all the methods. Test cases associate with page objects and page objects associate with Test base which associates only with the web driver.

Basic structure of Page object model (POM)
Create Object Repository for web UI elements
Page to Sign In
  • com.test: Contain test classes

Sample Test

  • com.pages: Contain page classes

Sign In page (Page to Login)

Sign Up page (Page to register)

  • com.helpers: Helper classes

Test base page

Here I am using the same example which is available in the git hub. You can go through links below to get the sample project available in Git.

First, let’s start with Test base class. Test base class contains all the common functionalities available in the test classes. All the test classes extended to Test base class.

When considering the Object-Oriented Programming (OOP), concept of extending test classes to Test base class is “Inheritance“. Code of Test base is shown below.

Test base page: contains the common methods of all page classes, such as click, type, etc.

Sign Up page: contains all the locators in pages . In the example below, it contains element locators (email, password, confirm password and signup button) which are required to sign up. Sign Up page is extended to “Test base” class, and so, I used common functionalities available in the “Test base” class. As an example, open, type and click. Open common function is used to navigate to the URL.

By using Google Chrome, you can easily identify the locators in your page. Right click on any element on a page, you can easily open the “Inspect Element” option to identify the locator of the element.

Inspect element Google chrome

Sign In page: contains all the locators in pages . In the example below, it contains element locators (email, password and sign in button) which are required to sign in.

A proper named methods in classes are easy to read and easy to update in future. As an example, “SignInToApplication”,“SignUpToApplication” .

Sample Test: now, we can write simple test with simple method calls. Let’s simply go through some of the annotations I have used.

@BeforeMethod and @AfterMethod: Here I have initialized my browser details and have created class objects in before method. I have closed my browser by adding drive.close(); code to after method.

@Test: This is the main part of an automation script, here, we write all the business logic.

There are some more annotations which we can discuss in detail in another article.

You can run the test : right click on the Sample Test navigate to “Run As” and click on “TestNG test” and run the project to get results.

And the test result is shown below

Test results of the sample project

Selenium will generate an email-able-report in Project-Folder\test-output.

Email-able report

I hope that this would help you to solve at least one problem when you are automating a system using selenium. Let’s meet with another exciting article in a series I am planning to share my own experience in facing problem in Selenium with you.TILL THEN HAPPY CODING !!!

--

--

Chaya Thilakumara
Chaya Thilakumara

Pursue your passion, and everything else will fall into place.