Stop testing your app
By: Deelaka Algawatta
Tags: Selenium, Automation, Quality Assurance, Java, Selenium Webdriver

For decades developers and QA engineers are testing their applications using many different ways, for example, if it is a UI they check all the UI elements are visible, all the clickable elements are clickable or the text boxes are valid, etc… and if it is a backend they check whether the correct outputs are coming for a given set of inputs etc… This was the basic idea of testing but in the evolution of the changing tech and the complexity of applications does it enough to test your application by applying those methods. What happens if the program/application is changing constantly ? is it worth to do all the testing, again and again, going through the same process.

Problem is, it is time-consuming and can lead to errors. “because we all humans and we make mistakes”. As a solution developers start testing applications in units it is called Unit Testing.

How the unit test is done is, the developer starts writing test cases before start writing the actual code. At first, when you write the unit test it fails because the actual implementation is not done yet. At this point, the developer has to write every possible scenario that has to implement in the actual code and needs to have a clear idea of what is developing. Then the developer writes the code to pass the tests that wrote previously. In java, we can use the Junit framework to write unit tests. This method is called Test-Driven Architecture. Even it is a good way of testing the code does it enough?. And also developer has to have a clear idea about what he is developing otherwise the test will be written to the way that the developer wants and write the code according to that and pass the test. So still the actual requirement and the built application can be two different things even all the unit tests are passing. Another way of testing is Automation Testing.

In automation testing what happens is the test cases that manually executed by a Software developer or a QA engineer are automated. For example, if we are checking a certain error page is coming when inserting the wrong data. Manually we have to start the application, go to the page, insert and submit the data and verify. In automation testing, we can write these steps in a script and run it by a single click. One major advantage is we can run the script over and over again and if the requirements change we can alter the script easily. So even after we years developing the application we can run the automation suite and test the application easily. There are several tools that we can use to write Automation scripts. Selenium, TestNg, Cucumber are some of them.

How to create an automation project using Selenium

In this part, I will be guiding you to create a basic Automation project using java to test a simple website. Also, you will find how to write Automation in the Page object model.

We are writing simple automation to test the Wikipedia page

What is required

Java

IDE ( IntelliJ Idea, Eclipse, etc..)

Selenium web driver (https://www.seleniumhq.org/download/)

Web browser ( Google Chrome or Mozilla Firefox )

Web browser driver uses to identify the browser.

Google Chrome driver — https://sites.google.com/a/chromium.org/chromedriver/downloads

Firefox driver — https://github.com/mozilla/geckodriver/releases

Basic knowledge of java programming

I’ll be using IntelliJ Idea (you can use any IDE that you’re comfortable with) and Google Chrome browser.

Step 1

Create a basic maven project and add

Junit dependency

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency>

Step 2

Extract all the downloaded zip file

Import selenium web driver

Import all the jar files from the extracted selenium web driver folder and the libs folder inside.

In IntelliJ Idea

go to file -> project structure and go to libraries and click the add button

In Eclipse

right-click the project -> go to properties -> java build path -> go to libraries tab -> Add External JARs

Importing selenium web driver jars

Importing libraries related to Selenium web driver

Step 3

Create a Java class name TestSimpleWebSite in the src folder

import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.PageFactory; public class TestSimpleWebSite { private WebDriver webDriver; public WebDriver createWebDriver() { System.setProperty(“webdriver.chrome.driver”, “/Users/deelakaalgawatta/documents/chromedriver”); webDriver = new ChromeDriver(); PageFactory.initElements(webDriver, this); return webDriver; } public void closeWebDriver() { webDriver.close(); } }

What I have done here is I created a simple class to initialize the Selenium web driver

In CreateWebDriver method what I have done is I have set the path of the web browser driver downloaded, without the driver the test will not run.

“System.setProperty(“webdriver.chrome.driver”, “/Users/deelakaalgawatta/documents/chromedriver”);”

For chrome, the property should be webdriver.chrome.driver

For firefox, the property should be webdriver.gecko.driver

PageFactory.initElements(web driver, this) will initialize all the page elements so we can catch element in classes using @FindBy or webDriver.findElements

Step 4

In-Page object model basically what we do is we define the page separately

For example, we define all the elements and methods related to a certain page in a separate class and we invoke methods and get elements in test classes.

So For that, we have to create another class name WikipediaHomePage and add this

import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; public class WikipediaHomePage extends TestSimpleWebSite { protected WebDriver webDriver; @FindBy(id = “www-wikipedia-org”) private WebElement wikipediaLogo; @FindBy(id = “js-link-box-en”) public WebElement englishLanguage; public WikipediaHomePage() { webDriver = createWebDriver(); webDriver.get(“https://wikipedia.org"); } public boolean isWikipediaPageLoaded() { return wikipediaLogo.isDisplayed(); } public void selectLanguage() { englishLanguage.click(); } }

Here what I have done is created a simple class and extended the previously built class so we can initialize the WebDriver in the page class.

After that, I have defined the elements.

What are the elements?

Elements are the components that we see on a web site

For example, Headings, buttons, text boxes, images etc…

In Automation to write the tests, we need to catch those elements. To find an element we can use

@FindBy and WebDriver.FindElement/ WebDriver.FindElements

As above we can use @FindBy annotation and use

id to identify elements with id,

CSS to identify elements by CSS selector

className to identify elements by class name

Like the previous method, we can use WebDriver find element method to identify elements

WebDriver.FindElement/ WebDriver.FindElements

webDriver.findelement(By.id(“id name”)

webDriver.findelement(By.className(“class name”)

webDriver.findelement(By.cssSelector(“CSS selector”)

In the constructor, I have called the createWebDriver method in TestSimpleWebSite class and assign the initialized WebDriver to the web driver object and then call the get method in the WebDriver class to open up the browser and go to a specific URL.

isWikipediaPageLoaded( )

In this method, I have used the isDiplayed( ) method in the web driver to check whether the element is displayed.

In my have check whether the Wikipedia logo is displayed. If it displays it returns a boolean true value.

selectLanguage()

In this method I have written the code to click on an element using click() method. In this example, it clicks on the English language element

Step 5

Writing test cases

Create a test class in the test -> java folder

import org.junit.AfterClass; import org.junit.Assert; import org.junit.Test; public class TestWikipediaPage { private static WikipediaHomePage wikipediaHomePage = new WikipediaHomePage(); @AfterClass public static void tearDownClass() { wikipediaHomePage.closeWebDriver(); } @Test public void testWikipediaHome() { Assert.assertTrue(wikipediaHomePage.isWikipediaPageLoaded()); wikipediaHomePage.selectLanguage(); } }

Here I have written the test using Junit testing framework.

To load the Page I have initialized the Page class WikipediaHomePage to get all the page elements.

In testWikipediaHome test method what I have done is called the isWikipediaPageLoaded( ) in the WikipediaHomePage inside the Assert.assertTrue.

In Junit we can check value, equality, conditions.

assertTrue- check the condition whether it’s true/false if the condition is true yes pass and if the condition false test false.

assertFalse- check whether the condition is true/false if the condition is false the test pass and if the condition is true test fails.

assertEquals- check the equality of two values

After checking the page is loaded it calls the selectLanguage method in the WkipediaHomePage class.

After all the tests are completed it will run the @AfterClass method tearDownClass()

When we add @AfterClass the method will execute after all the other methods are executed.

So in this method, we can write the thing you want to do after everything completed.

Like closing a connection, closing a buffer stream, etc….

In this example, I have written the tearDownClass to close the webDriver so it will close all the open browsers in the test.

Likewise, you can use different types of methods to test web pages.

This is a simple article I wrote about setting up a Selenium automation project and testing as per my knowledge please do share and comment if there are any corrections/questions.

Git hub project link:- https://github.com/deelaka96/Sample-Selenium-Automation-Project

Thank You :)