It
is not surprising Selenium has become the most popular tool for automated
testing of web applications.
Selenium
is free, easy to learn and really quickly we can create a bunch of automated
tests. But over the time when number of tests grow significantly we need
something more. Something that will allow us to easily manage and maintain our
test repository. That’s why we need test automation framework.
We
would like to show you some design patterns and good practices that will help
you to start with your own implementation of really basic test automation
framework.
In
several parts we will guide you step by step how to create it from a scratch.
Each part will be published regularly and will contain some examples how to
develop your framework.
Setting up the environment
First
things first. Before you begin to write any code you need to set up your
development environment. As I mentioned previously, we are going to write our
Selenium code in Java.
I
believe that Java is the most frequently chosen language to write automation
tests with Selenium, because of the great community and support. However, it is
always good to be consistent, so if application under tests is written in C#,
PHP or Python you should consider the choice of language to be the same. To resolve
dependencies like Selenium or TestNG in our project we will use Maven.
Test
automation framework will be based on TestNG. TestNG is a testing framework similar to
JUnit and NUnit. We will be using IntelliJ
Idea as IDE, but of course it’s optional for you. You can
use Eclipse if you prefer, but all examples have been prepared in IntelliJ
Idea.
Below you can find some instructions how to
set up your IntelliJ Idea environment on Windows.
Java
1.
Check if you have Java installed
1.1. Click
Start, search for “cmd”
and press Enter.
1.2. Type command: java
–version
2.
Download the installer
2.3. Select “Accept License
Agreement” and download JDK 7 for your version of Windows (32 or 64 bit)
3.
Run the JDK Installer
4. Update the PATH Environment
Variable
4.1. Click Start,
then Control Panel, then System.
4.2. Click Advanced, then Environment Variables
4.3.
Add the location of the bin folder of the JDK installation to the PATH variable
in System Variables. The following is a typical value for the PATH
variable: C:\WINDOWS\system32; C:\Program
Files\Java\jdk1.7.0_75\bin
5.
Repeat steps from 1. To verify java has been installed
Download
& install IntelliJ IDEA
Create
new project
1. Click
“Create new project” on Welcome screen
2.
Select “Maven” and Project SDK: 1.7 and then click on “Next” button
3.
Fill in fundamental unit of work in Maven (POM). A POM requires that its
groupId, artifactId, and version be configured:
- groupId – the id of the
project’s group.
- artifactId – the id of
the artifact (project)
- version – the version of
the artifact under the specified group
You
can fill in those with data as below or you can use your own. Click “Next”
button at the end.
4. Enter project name and location. Click
“Finish” button.
5.
At this point new project should be created. Click “Enable Auto-Import” if
you want IntelliJ to import Maven projects automatically each time you change
your pom.xml.
6. Add Selenium and TestNG to
your maven dependencies in pom.xml. In pom.xml add dependencies
section as below:
1
|
<dependencies>
|
2
|
<dependency>
|
3
|
<groupId>org.testng</groupId>
|
4
|
<artifactId>testng</artifactId>
|
5
|
<version>6.1.1</version>
|
6
|
<scope>test</scope>
|
7
|
</dependency>
|
8
|
<dependency>
|
9
|
<groupId>org.seleniumhq.selenium</groupId>
|
10
|
<artifactId>selenium-java</artifactId>
|
|
|
|
|
11
|
<version>3.7.1</version>
|
12
|
</dependency>
|
After
that your pom.xml should look like:
1
|
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
|
5
|
<modelVersion>4.0.0</modelVersion>
|
6
|
|
7
|
<groupId>com.github.pguzdziol.automation.tutorial</groupId>
|
8
|
<artifactId>auto-fwk-tutorial</artifactId>
|
9
|
<version>1.0-SNAPSHOT</version>
|
10
|
|
|
|
|
|
11
|
<dependencies>
|
12
|
<dependency>
|
13
|
<groupId>org.testng</groupId>
|
14
|
<artifactId>testng</artifactId>
|
15
|
<version>6.1.1</version>
|
16
|
<scope>test</scope>
|
17
|
</dependency>
|
18
|
<dependency>
|
19
|
<groupId>org.seleniumhq.selenium</groupId>
|
20
|
<artifactId>selenium-java</artifactId>
|
21
|
<version>2.45.0</version>
|
22
|
</dependency>
|
23
|
</dependencies>
|
24
|
</project>
|
The first test
Now
you should be ready to implement your first test because at this point you
should have fully configured development environment. First and most important
our application under tests is www.amazon.com. We will show you how to
implement sample test scenario
Create
test class: First Tests
Let’s
move forward and create your first test class, but before that let’s create a
new package to properly organize the code. To do so, right mouse click on src\test\javainside the project
and then New ->
Package.
Now
you are asked to enter new package name. You can use my naming convention, but
if you feel confident you can try to come up with your own.
Already
created package should be displayed under java. We can create new class inside
this package in almost exact same way; just select Java Class instead of
Package. Right mouse click on the package name com.github.pguzdziol.automation.tutorial.tests and
then New -> Java Class.
Enter the class name which you want (in my example: FirstTests) and click Ok
button.
If
everything is fine you should be able to see similar code as below in Editor
Window.
1
|
package com.github.pguzdziol.automation.tutorial.tests;
|
2
|
|
3
|
public class FirstTests {
|
4
|
|
Now
you can start writing the code for your test. Firstly you need to create a new
instance of WebDriver so you can use it in your test methods. You are going to
do this in setUp() method
annotated with @BeforeClass. @BeforeClass is TestNG
annotation and it means that the annotated method will be run before the first
test method in the current class is invoked. Go ahead and declare WebDriver
instance variable and next initialize Firefox driver in setUp() as following:
1
|
package com.github.pguzdziol.automation.tutorial.tests;
|
2
|
|
3
|
import org.openqa.selenium.WebDriver;
|
4
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
5
|
import org.testng.annotations.BeforeClass;
|
6
|
|
7
|
public class FirstTests {
|
8
|
|
9
|
private WebDriver driver;
|
10
|
|
|
|
|
|
11
|
@BeforeClass
|
12
|
private void setUp()
{
|
13
|
driver
= new FirefoxDriver();
|
14
|
}
|
You
have setUp() method ready
but you also need tearDown() method
to close browser window and safely end the session after all.
1
|
@AfterClass
|
2
|
private void tearDown()
{
|
Add
above method inside your FirstTests class. @AfterClass annotation means that the method will
be run after all the test methods in the current class have been run. To close
browser window and end the session call driver.quit().
There is also another method driver.close() which
close the browser window that the driver has focus of.
As
I mentioned previously you are going to create some tests for www.amazon.com. If everything is fine setUp() method should open
Firefox browser. Next step is to get Amazon page. Before each test method in
our test class we want to start on Amazon home page – that’s why we need a
method annotated with @BeforeMethod as
below:
1
|
@BeforeMethod
|
2
|
public void openHomePage(){
|
Basically,
this piece of code takes you to Amazon home page before each test method. Let’s
create empty test method for now to verify what has been done so far. Add
new test() method
with @Test annotation
inside the class. At this point you should have:
1
|
package com.github.pguzdziol.automation.tutorial.tests;
|
2
|
|
3
|
import org.openqa.selenium.WebDriver;
|
4
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
5
|
import org.testng.annotations.AfterClass;
|
6
|
import org.testng.annotations.BeforeClass;
|
7
|
import org.testng.annotations.BeforeMethod;
|
8
|
import org.testng.annotations.Test;
|
9
|
|
10
|
public class FirstTests {
|
|
|
|
|
11
|
|
12
|
private WebDriver driver;
|
15
|
public void setUp()
{
|
16
|
driver
= new FirefoxDriver();
|
19
|
@BeforeMethod
|
20
|
public void openHomePage(){
|
25
|
public void tearDown()
{
|
26
|
driver.quit();
|
29
|
@Test
|
30
|
public void test(){
|
Now
run this empty test method which basically do nothing but will help us to
verify that everything is fine at this point. Right mouse click on test() method and then Run.
If everything went as expected you should notice that the test passed.
Create
test method: testAddingItemToCard()
Test
steps:
The
first proposed test scenario is for adding item to the cart. Step by step you
are going to write code for it without going into too much detail. Please take
a look at following steps and try to reproduce them manually.
- Open www.amazon.com
- Select “Books” from
search category dropdown
- Enter search key:
“Selenium”
- Click “Go” button
- Click the first search
result item title
- Verify that product title
is correct
- Click “Add to Cart”
button
- Verify confirmation test
appears: “1 item added to Cart”
- Navigate to Cart page
from main menu
- Verify item is displayed
on Shopping Cart list
testAddingItemToCard()
Go
back to IDE and change the name of test() method
to testAddingItemToCard().
Then step by step add below code inside this method.
- Open www.amazon.com – step is already implemented in openHomePage()
- Select
“Books” from search category dropdown – We are going to use Select class. Select
class is a helper for dealing with select tags. Create new instance
of Select class, but do not assign it to any variable. Select class
constructor takes web element attribute. We can find this element by its
id: “searchDropdownBox”. Then we select option “Books” using selectByVisibleText(String option) method.
1
|
newSelect(driver.findElement(By.id("searchDropdownBox")))
|
2
|
.selectByVisibleText("Books");
|
- Enter
search key: “Selenium” –
Find search input field by its id and then enter “Selenium” into it.
1
|
driver.findElement(By.id("twotabsearchtextbox"))
|
2
|
.sendKeys("Selenium");
|
- Click
“Go” button – Find
“Go” button by its xpath and then click on it. Given xpath stands for
first element with value attribute equals “Go”.
1
|
driver.findElement(By.xpath("//*[@value='Go']"))
|
2
|
.click();
|
- Click
the first search result item title – In below code we are doing three things. Firstly
we create local variable for item title element founded by its class name.
Secondly we save item title text to local string variable so we can use it
further. And lastly we click on item title element.
1
|
WebElement
firstItemTitleElement =
driver.findElement(By.className("s-access-title"));
|
2
|
String
firstItemTitle = firstItemTitleElement.getText();
|
-
3
|
firstItemTitleElement.click();
|
- Verify
that product title is correct – Now it is time for the first assertion. Assertion
expects that given statement returns true. Otherwise it causes the test to
fail. We want to verify that the product title on product details page is
equal to variable from previous step 5.
1
|
assert (driver.findElement(By.id("productTitle"))
|
2
|
.getText()
|
-
3
|
.equals(firstItemTitle));
|
- Click
“Add to Cart” button
1
|
driver.findElement(By.id("add-to-cart-button"))
|
2
|
.click();
|
- Verify
confirmation test appears: “1 item added to Cart”
1
|
assert (driver.findElement(By.id("confirm-text"))
|
2
|
.getText()
|
-
3
|
.equals("1
item added to Cart"));
|
- Navigate
to Cart page from main menu
1
|
driver.findElement(By.id("nav-cart"))
|
2
|
.click();
|
- Verify
item is displayed on Shopping Cart list
1
|
assert (driver.findElement(By.className("a-list-item"))
|
2
|
.getText()
|
-
3
|
.contains(firstItemTitle));
|
As
the result of all the above steps your final test method should look the
following way:
1
|
@Test
|
2
|
public void testAddingItemToCard()
{
|
3
|
//Select
'Books' from search category dropdown
|
4
|
newSelect(driver.findElement(By.id("searchDropdownBox")))
|
5
|
.selectByVisibleText("Books");
|
6
|
|
7
|
//Enter
search key: 'Selenium'
|
8
|
driver.findElement(By.id("twotabsearchtextbox"))
|
9
|
.sendKeys("Selenium");
|
10
|
|
|
|
|
|
11
|
//Click
'Go' button
|
12
|
driver.findElement(By.xpath("//*[@value='Go']"))
|
15
|
//Click
the first search result item title
|
16
|
WebElement
firstItemTitleElement =
driver.findElement(By.className("s-access-title"));
|
17
|
String
firstItemTitle = firstItemTitleElement.getText();
|
18
|
firstItemTitleElement.click();
|
19
|
|
20
|
//Verify
product title
|
21
|
assert (driver.findElement(By.id("productTitle"))
|
22
|
.getText()
|
23
|
.equals(firstItemTitle));
|
24
|
|
25
|
//Click
'Add to cart' button
|
26
|
driver.findElement(By.id("add-to-cart-button"))
|
29
|
//Verify
confirmation text appears
|
30
|
assert (driver.findElement(By.id("confirm-text"))
|
31
|
.getText()
|
32
|
.equals("1
item added to Cart"));
|
33
|
|
34
|
//Navigate
to 'Cart' page
|
35
|
driver.findElement(By.id("nav-cart"))
|
36
|
.click();
|
37
|
|
38
|
//Verify
item is displayed on Shopping Cart list
|
39
|
assert (driver.findElement(By.className("a-list-item"))
|
40
|
.getText()
|
41
|
.contains(firstItemTitle));
|
42
|
}
|
Now
run it (Right mouse click on testAddingItemToCard
() and then Run).
The test should pass.
Summary
In
this part we have configured development environment by installing Java and
IntelliJ IDEA. Next we have created new maven project and we added required
dependencies like Selenium and TestNG to it. After that we were ready to create
our first test. Next how to implement sample test for adding item to card in
Amazon online store.
You
have learned some really basic Selenium methods that allowed you to interact
with web browser. Next you have to create the second test on your own. Your
assignment was to create test for Sign In and Sign Out functionality. At this
point you should have single test class with two test methods inside.
Comments
Post a Comment