0% found this document useful (0 votes)
95 views10 pages

Selenium BasicAutomatedTesting Tutorial

Selenium and Selenium IDE are powerful automated testing tools, this basic tutorial introduces how to take advantage of them on your projects.

Uploaded by

BCmoney
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
95 views10 pages

Selenium BasicAutomatedTesting Tutorial

Selenium and Selenium IDE are powerful automated testing tools, this basic tutorial introduces how to take advantage of them on your projects.

Uploaded by

BCmoney
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

Basic Selenium Tutorial

Chapter 0: Foreword
In order to follow with the following instructions, it will be good for you to
understand the basic programming and syntax of Java. The links below point to a
useful site on Java, however, only the most basic Java understanding is needed,
so, theres no need to delve too deep into it yet.
http://www.javabeginner.com/learn-java/basic-language-elements
http://www.javabeginner.com/learn-java/java-control-flow-statements
I hope this will be helpful for all testers out there who is willing to learn more
about Test Automation.

Chapter 1: Launching the window, browser and navigating pages:


1.
2.
3.
4.
5.
6.

WebDriver driver = new FirefoxDriver();


driver.get("http://www.google.com.my");
driver.manage().window().maximize();
WebElement queryBox = driver.findElement(By.id("gbqfq"));
queryBox.sendKeys("My Very First Automation");
driver.findElement(By.id("gbqfb")).click();

The first line, WebDriver calls upon the driver for the browser. Basically it is a
control and an identifier for the window. For example:
WebDriver driver1 = new FirefoxDriver();
WebDriver driver2 = new FirefoxDriver();
driver1.doSomeShit();
driver2.doSomeEvenMoreSeriousShit();
Some shit will be done on FireFox Browser window 1, and some even more
serious shit will be done on FireFox Browser window 2.
The 2nd line, the driver tries to navigate to the url specified.
The 3rd line, the driver maximizes the window.
Commonly used function in WebDriver is:
close : To close the window.
get: Go to the URL.
findElement: Find an element on a page.
The 4th line, the id of the element of query box is found. This is in order for us
to manipulate (enter text, read) from the element (which is the query box). The
id of the item can be found in the following way.
1. Right click and select Inspect Element

2. The id is then found.

The 5th line is entering text into the element (query box).
Elements can be found by multiple ways. It is determined by the By. as shown.
The following are the commonly used methods to find the element.

Do avoid using cssSelector if possible. The simplest and most effective way is to
use id.
The 6th line is to click on the Google Search.
Tips: Always use the . in NetBeans to see what are the available functions are
provided along with the API.
Quiz1:
Write down the code if you are to combine the action in line 4 and line 5 into 1
single line (only 1 ; instead of 2). (Hint: Refer to line 6)
Challenge1:
Code a simple Function that will take in the variable below, and do navigations
and searches according to what is specified:
String [][] testData =
{
{"google","my first search"},//search on google.com for the text "my first
search"
{"yahoo","my second search"},//search on yahoo.com for the text "my
first search"
{"google","my final search"}
};

Chapter 2: Advanced Element Find


In the previous chapter, easily available elements are used. In real world
applications, many elements that are needed might not be easily found. In
previous chapters, findElement is used. findElement returns 1 result. If there are
multiple elements that fit the criteria, it will fail.
HTML Example1:
(For HTML examples, just copy the text into a notepad, rename the file to
<whatever>.htm then open it with your browser.
<div id="something1">
field1<input id="Test1"></input>
field2<input id="Test2"></input>
</div>
<div id="something2">
field1<input id="Test1"></input>
field2<input id="Test2"></input>
</div>
How to find test2s Test1? If findElement(By.id(Test1)) is used, then it will return
2 results thus fail!
The idea is to find the parent, then by accessing the parent element, perform
another find.
(driver.findElement(By.id(something2))).findElement(By.id(Test2))
HTML Example2:
<div id="something1">
field1<input id="Test1"></input>
field2<input id="Test1"></input>
field3<input id="Test1"></input>
field4<input id="Test1"></input>
</div>
How to find the 1st input box and click it?
The element can still be found by using another method, findElements.
findElements return an array of objects. These are all the different objects that fit
the criteria. Return null if not found.
In this case, the following code can solve the issue:
List<WebElement> we = driver.findElements(By.id("<something"));
if(we.size()>0){
((WebElement)((we.toArray())[0])).click(); //pros dun whine, I just like to use
array alright :p
}

Chapter 3: Getting the values of an element


How do we know if a page is displaying certain text correctly? First, we must be
able to extract the text then compare it with what is expected to display. In this
chapter, we will learn how to extract the text.
Example:

Solution:

The function to call upon here is .getText()

A simple way of utilizing this can be summed up in the next example:

Chapter 4: Getting attributes of an element


If you would need to verify the attributes of the elements, for example the link of
which the hyperlink will go to upon clicking, you can do so as the example below
will show you.

The solution:

<element>.getAttribute(<insert your html attribute that you need>);


This will enable you to get more information for that element to verify. It will be
useful if you need to verify the font, font size, color, background color.
There are limitations to this approach, however, this is where you rely on
communication with the programmer to circumvent the seemingly impossibility
of coding, or to be creative (or skillful) to work around these limitations.

Chapter 5: Performing actions on an element


In this aspect, Selenium API provides a limited number of actions that can be
simulated on the object.
In previous chapter, we have seen that we can do the following on a button:
<button element>.click();
Another important action that can be performed is:
<form>.sendKeys(<enter whatever text you wish to enter here>);
With these basic actions, most of the actions needed to be carried out
(automated) can be performed. Actions that cannot be simulated (example:
Scrolling down a screen) can use the workaround of simulating a press on the
page_down button. If many advanced actions needed to be simulated, I will
suggest that a better tool (which costs money) to be invested in.
To have access to all the different buttons to simulate as input, do the following:
Add the following to the top of the codes:
import org.openqa.selenium.Keys;
Then by typing the following, you will get a list of different buttons to press:
forgetPassLink is just a web element, it can be any web element.

Chapter 5.5: Mini Conclusion


With these simple techniques and walkthrough, you will be able to code simple
automations to test on your web application. The challenge remains as to how
the data is organized, and how the coding will be structured for a more complex
web application. Do stay tune to QAKL group, these are the things that will be
covered in more details. Do share your ideas as to how you overcome these
challenges.
As it is, I do not know if I will have time to continue with Chapter 6. I hope I do,
but I cannot promise on that. Stay tuned in QAKL on facebook. Post your
questions there. If it is a project that is confidential in nature, then you may
directly email me. Otherwise, every question will be a good learning opportunity
for others as well.
Thank you for your kind attention.
Disclaimer: This document is for educational and personal use only. Do not
distribute it for commercial purposes. Although every reasonable effort is
made to present current and accurate information, no guarantee will be
given.
Regards,
Fong Kai The testing guy @QAKL

You might also like