Introduction To Selenium
Introduction To Selenium
With
Python
Simplified
Introduction:
Chapter 1: HTML and CSS
1.1: Introduction to HTML and DOM
1.2: Introduction to Cascading Style Sheets (CSS)
1.2.1: Internal CSS
1.2.2: External CSS
1.2.3: In-Line CSS
Chapter 2: Introduction to Python Programming.
2.1: Python Installation
2.2: Python Datatype and variables
2.3: Python List
2.4: Python Tuple
2.5: Python Dictionary
2.6: Python control statements
2.7: Python functions.
2.8: Python class
2.9: Python Inheritance
2.10: Python Try Except Block
Chapter 3: Selenium
3.1: Selenium Webdriver Architecture
3.2: Selenium and Driver Installation
Example 1
3.3: Locating Web Page Elements
Example 1
Example 2
Example 3
Example 4
Example 5
Example 6
Example 7
Example 8
3.3.1: XPATH
Example 9
3.4: Selenium Waits
3.4.1: Implicit Wait
3.4.2: Explicit Wait
3.5: Selenium Page Object Model
Page Object Model Architecture
3.5.1: Base Page
3.5.2: Locators
3.5.3: Pages
3.5.4: Test
3.5.5: Test Suite
3.5.6: How to generate a HTML report or a log file after
running a Test Case Class
Introduction:
What is HTML?
In Internal CSS, the CSS codes and main content of the web
page are present within the same HTML document and it
provides styling information exclusive for that page.
The CSS codes are written within the <style> tags in the
<head> section of a HTML file.
Now let’s start coding..
index.html
background-repeat: repeat;
HTML
CSS
In the above HTML code,
· The <div> container with id homePageHeader holds
both heading ( <h1> ) as well as the logo image with id
logoPic.
What is Id in CSS?
position : absolute
float : left
Now let’s save index.html and open the file using Google
Chrome.
It looks perfect.
CSS
In the above HTML code,
<ul> stands for unordered list and each items within the list
should be present within <li> tags.
position : absolute
bottom : 0px
right : 50px
display : inline;
● For the <a> tag links, I have set its color and set the text-
decoration to none.
text-decoration : none;
What is text-decoration property?
This CSS file contains the styling information for all HTML
web elements that belong to CSS Class container, fieldStyle,
submitButton and backlink.
foodDelivaryCss.css.
To incorporate this styling information into our web pages, we
need <link> tags and this tag should be present within the
<head> section of a HTML document.
Difference between id and class in CSS?
1. A class selector is a name preceded by a full stop (.) ,
whereas an id is preceded by hash sign (#).
Before we start coding for our Log In page, let’s look into
some HTML input syntaxes.
● The <input> syntax which will take only text value is:
<input type = “text” placeholder = “default_name ”
name = “name” id = “id”>
logIn.html
3. In the above HTML code, you will also notice the presence
of <label> tag.
· <p> tag
· <select> tag
<select>
<option value = “ val1 ”> Name_1 </option>
<option value = “ val2 ”> Name_2 </option>
……
</select>
· <form> tag
3. It is time consuming.
Chapter 2: Introduction to
Python Programming.
python file_name.py
Output
When we run the above piece of code, Python shows the data
types of variable x, y and z.
Apart from data types like int, float and string, Python also
has list, tuple and dictionaries.
Output
Code explanation:
● len( ) function is used to get the length of the list.
Output: is app
Tuple List
Output
Output
Output
def function_name ( ) :
……..
Code Explanation:
· In the above piece of code, we created a function
validate_username with parameter uname. If the
argument is “John123”, the function prints “Hello John”
or else it will print “Wrong username”.
class class_name :
def __init__(self) :
….
def method1 :
….
3. Saves time.
Class Parent:
……..
Class Child (Parent):
…….
Code explanation:
The class Tester inherits the c_id and c_name from its
super class and in order to make the Inheritance work
properly, we need to invoke the __init__ method of its
parent class as shown in the screen shot above. If we do
not invoke, then we will get exceptions as shown in the
screen shot below:
Output
2.10: Python Try Except Block
Syntax:
try:
……..
except:
………
finally:
………….
Code explanation:
Output
Please note: In this chapter, I have covered only few
important Python topics essential for coding scripts in
Selenium. To gain in-depth and through knowledge of all
Python topics, please visit website
https://www.w3schools.com/
Chapter 3: Selenium
· Selenium is an automation testing tool used for testing
web applications.
The tests are performed by test scripts and these scripts
are usually coded in popular programming languages like
Java or Python.
Please note: Each Browser has its own Browser Driver. For
example: Google Chrome has its own browser driver, Firefox
has its own browser driver etc.
Before selecting the browser for your automation testing, it is
very important to install its driver first. In this book we will
be using Google Chrome browser, so we have to install
Chrome Driver for it.
Example 1
In this example, Let’s open www.google.com through
Selenium code.
Code explanation:
On the Create New Profile link, right click -> then click on
Inspect
find_element_by_id ( id_name )
find_element_by_name ( name )
find_element_by_xpath ( xpath )
find_element_by_link_text ( link_name )
find_element_by_partial_link_text (
partial_link_text_name )
find_element_by_tag_name ( tag_name )
find_element_by_class_name ( class_name )
find_element_by_css_selector ( css_selector )
Example 1
In this example, the automation script will locate the
Create New Profile link in the Home page and click on it.
Code explanation:
· We passed the Create New Profile link text copied in
Step 2 into find_element_by_link_text ( ) method
Open Create New Profile page, right click on the First Name
input field -> Inspect.
Open our existing python file testing.py and add the following
lines of code.
Code explanation:
In the above piece of code, we are simply locating the web
elements and storing them in their respective variables.
send_keys( ) method is used to send some values within those
input fields.
Let’s save everything and run the script with the help of
python command.
Add this line just before filling out the Create New Profile
form page and this will tell selenium webdriver to pause
execution for 10 seconds before working with Create New
Profile page from Home page.
Now open testing.py file and add the following lines of code.
Please note: To work with selenium webdriver Select class,
we need to import it first.
Example 4
In this example, we will deal with any alert window present
in our web page.
Open our existing testing.py file and add the following lines of
code.
Code explanation:
Example 5
In this example, we would like to test, whether the
paragraph text on a web page matches with the expected
value or not. If matches return TRUE, else return FALSE.
Open Create New Profile page, you will notice that at the top
of the page a line is written highlighted in the screen shot
below
Let’s access the text value from the Create New Profile page,
Right click on the paragraph text value -> Inspect.
You will notice that the paragraph field belongs to class text.
Since there is no other element in the Create New Profile page
using this same class name, so we will be using this attribute
along with selenium webdriver
find_element_by_class_name( ) method to uniquely identify
the web element.
Example 6
In this example, we would like to GET the value from the
First Name input field.
Code explanation:
The script runs successfully and we also get the first name as
output.
Example 7
In this example, we will get the URL and title of the web
page and match the received title with the expected title.
Open our existing testing.py file and add the following lines of
code.
· driver.current_url is used to get the URL of the web
page.
Example 8
In this example, we would like to get the highlighted text
shown in the screen shot below and check whether it
matches with the expected value or not.
Open Create New Profile page -> right click on the First
Name text value -> Inspect
You will notice that the First Name text value does not
contain any ID or Class Name. So in this situation how we
can access the web element ?.
The answer to the question is XPATH.
3.3.1: XPATH
Absolute XPATH
We see that the text value is present within: <html> -> <body>
-> <div> container ID “parent” -> <div> container class
name “child” -> <table> -> <tbody> -> <tr> -> <td> .
· Relative XPATH does not start from the root node to get
to our desired element. We can simply write a query and
locate the web element using the following syntax:
//*[@id=‘parent’]//td
or
//td
Example 9
In this example, we will get the value from the Username
input field (entered in Example 2) and store it into a
variable entered_username.
Once the form is submitted by clicking on the submit
button, the user information gets displayed on the
Customer web page.
We will check whether the entered username stored in
variable entered_username matches with the displayed
username (of Customer web page) or not.
1. Implicit Wait
2. Explicit Wait
driver . implicitly_wait ( 5 )
(where 5 is the time in seconds)
NOTE:
1. Expected Condition returns a Boolean value.
The above piece of code shows both Implicit Wait and Explicit
Wait.
In Explicit wait, webdriver wait for 10 seconds for an element
with ID id_firstName to be found. If the element found
successfully, then value “KiaTaco” are send into it using
send_keys( ) method.
title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
text_to_be_present_in_element
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
invisibility_of_element_located
element_to_be_clickable
staleness_of
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present
ID ( id )
XPATH ( xpath )
NAME ( name )
2. It saves time.
If something in the web application changes, the fix needs
changes only in one place.
3.5.2: Locators
Code explanation:
Now let’s create separate class file for Create New Profile
page (Link:
http://sbasu.pythonanywhere.com/tastyFoodApp/create ).
Code explanation:
Code explanation:
3.5.4: Test
Before we create Tests for each web page of our project, let’s
first understand the basic structure of a Test Case Class
depicted by the screen shot below:
Important points to note are:
The screen shot below shows the test case class of Create
New Profile page.
test_CreateNewProfile.py
Code explanation:
python test_CreateNewProfile.py.
test_homePage.py
Code explanation:
Test Suite is used to run all Test Case Classes together and by
doing so the entire web application is tested end to end.
Now let’s create a Test Suite for our project by following the
above rules and structure.
Open Notepad++ -> create a new python file and
name it testSuite.py
testSuite.py