Why Python for automation testing?
- Python is Free and open source
- High level
- Interpreted
- Large Community
Python Closely resembles the English Language.
- It is easy to learn and understand, and simpler code
- World is moving towards Artificial Intelligence with machine learning and Python plays a crucial role in implementing them.
- More jobs, Less competition.
Python has many built-in testing frameworks that cover debugging and the fastest workflows
What is PIP?
Pip is a package management system used to install and manage software packages written in Python.
Splinter -> Tool
Pytest -> Framework
Robot -> Framework
Testing is a tedious task and Python is a booster.
Selenium Python Binding
Selenium python binding provides a simple API to write functional tests using the Selenium web driver.
Commands to Import selenium web driver
from selenium import webdriver
from selenium. web driver. common.keys import keys
OR
from selenium.import
Important Commands:
pip freeze => to check to install Python packages in our system.
pip install selenium => to install selenium
pip install HTML-test runner => for HTML test report
pip list => to check the list of libraries that you have installed for python
pip show selenium =>to show a summary of the library
pip install -U selenium => to upgrade selenium library
What is Selenium?
Selenium is a set of tools and libraries that automates web browser actions.
Selenium provides tools that can interact with browsers and can automate browser actions click, inputting, selecting, navigating, etc with the help of scripts.
Selenium is a free and open-source solution for test automation of web applications.
Selenium was started By: Jason Huggins.
Selenium releases = 1) 2006 with Selenium RC
2) 2011 with Selenium RC and Webdriver
3) 2016 with Webdriver
Browsers Supported by Selenium:
All major browsers supported Selenium
Features of selenium:
Flexible and Extensible
Multiple Language Supported.
Multiple Browser Supported.
Components of Selenium:
Selenium IDE: is an add-on for Firefox and it is a record and playback plugin
And use it for prototype testing.
Selenium RC(Remote Control): is used to execute scripts (written in any language) using javascript.
Selenium Webdriver:
most actively used component, it is an API used to send commands directly to the browser.
It is a successor to Selenium RC.
Selenium Grid:
is a tool that provides greater capabilities to run tests in parallel across different machines and different browsers simultaneously.
Used to minimize execution time.
With selenium:
- Identified web elements
(Using identifiers like ID, Xpath)
- Add Actions
(using Python programing language)
- Run Test And Validate.
Sample Selenium Project.
Create a test for Google search
Add implicit wait for 10 sec
Maximise window
Create unit tests
Add HTML reporting library
Run from the command line
Copy the file path then type the command:
python -m unittest.py
-m is a flag
Types of locators:
ID: the ID strategy looks for an element in the page having an id attribute corresponding to the specified pattern. will be matched by a locator like id=my_id or just my_id
Name: Locating elements by name is very similar to locating by ID, except that we use the “name=” prefix instead.
Link Text: this type of locator applies only to hyperlink text. We access the link by prefixing our target with “link=” then followed by the hyperlink text.
CSS Selector: Locating by CSS selector using an HTML tag and a class name is similar to using a tag and ID. but in these cases, a dot (.) is used instead of a hash sign.
Format : css=tag.class[attribute=value]
Partial Link Text: in some situations, we may need to find links by a portion of the text in a link text element it contains. In such situations, we use partial link text to locate elements.
Syntax: find_element_by_partialLink
XPath: is designed to allow the navigation of XML documents, to select individual elements, attributes, and some other parts of XML documents for specific processing.
XPATH=//div[@class=’signIn’]
Questions and answers:
What is REST?
What is the REST API?
What is JSON
“JSON” stands for javascript object notation.
Name JSON is a (Mostly) language-independent way of specifying objects as name-value pairs.
JSON is a way of structuring data, JSON is a data format.
JSON value
A JSON instance contains a single JSON Value
A JSON value may be either an object, array, number, string, true, false, or null.
Python Basics
*operators:
Operators are used to perform operations on variables and values.
There are seven types of operators,
Python divides the operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
*Arithmetic Operators:
Arithmetic operators are used with numeric values to perform common mathematical operations.
*Python Assignment Operators:
Assignment operators are used to assign values to variables.
*Python Comparison Operators:
Comparison operators are used to compare two values.
*Python Logical Operators:
Logical operators are used to combine conditional statements.
*Python Identity Operators:
Identity operators are used to comparing the objects, not if they are equal,
but if they are the same object, with the same memory location.
*Python Membership Operators:
Membership operators are used to test if a sequence is presented in an object.
*Python Bitwise Operators:
Bitwise operators are used to compare (binary) numbers.
Python Datatype:
* Python is a loosely typed language. Therefore, there is no need to define the datatype of any variables.
* NO need to declare variables before using them.
Data Types:
- Immutable: this is similar to fixed lists, where you cannot modify the existing values. There are three types of immutable data types.
1. Numbers.
2. strings.
3. tuples.
- Mutable: Whereas, mutable can modify or update any existing values.
1. lists.
2. Dictionaries.
3. sets.
*Immutable:
1. Numbers:
1. Integer: Example; 1,5, etc. 2+4=6
2**3=8( 2 to the power of 3)
2. Float: Example; 3.5,5.0, etc. 7.0+3.5=10.5
6/3 =2.0
3. Complex: Example; 5+3j, etc. (3+4j)+(1+1j)= 4+5j
(2+2j)*2=4+4j
2. strings:
* strings are sequences of one-character strings
Example='welcome to the world of python'
or
Example="Welcome to the world of Python"
* multi-line strings can be denoted using triple quotes,''' or """
Example= ''' Equality for all browsers: Chrome. Firefox Edge. IE. Safari. Opera.
Equality for all devices: Desktop. Laptop. Tablet. Mobile.'''
* Type-specific method
Method Description
capitalize() Converts the first character to uppercase
case fold() Converts a string into lowercase
center() Returns a centered string
count() Returns the number of times a specified value occurs in a string
encode() Returns an encoded version of the string
ends with() Returns true if the string ends with the specified value
expand tabs() Sets the tab size of the string
find() Searches the string for a specified value and returns the position of where it was found
format() Formats specified values in a string
format_map() Formats specified values in a string
index() Searches the string for a specified value and returns the position of where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
is decimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
identifier() Returns True if the string is an identifier
is lower() Returns True if all characters in the string are lowercase
is numeric() Returns True if all characters in the string are numeric
printable() Returns True if all characters in the string are printable
isspace() Returns True if all characters in the string are whitespaces
title() Returns True if the string follows the rules of a title
super() Returns True if all characters in the string are upper case
join() Joins the elements of an iterable to the end of the string
just() Returns a left justified version of the string
lower() Converts a string into lowercase
strip() Returns a left trim version of the string
make trans() Returns a translation table to be used in translations
partition() Returns a tuple where the string is parsed into three parts
replace() Returns a string where a specified value is replaced with a specified value
find() Searches the string for a specified value and returns the last position of where it was found
index() Searches the string for a specified value and returns the last position of where it was found
just() Returns a right justified version of the string
partition() Returns a tuple where the string is parsed into three parts
split() Splits the string at the specified separator and returns a list
strip() Returns a right trim version of the string
split() Splits the string at the specified separator and returns a list
split lines() Splits the string at line breaks and returns a list
starts with() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string
swap case() Swaps cases, the lower case becomes the upper case and vice versa
title() Converts the first character of each word to uppercase
translate() Returns a translated string
upper() Converts a string into upper case
fill() Fills the string with a specified number of 0 values at the beginning
3. tuples:
* A tuple is a sequence of immutable Python objects like float numbers, string literals, etc.
* The tuples can't be changed like lists.
* Tuples are defined by using circular brackets.
Example: my_tuple=( 'tuple',2.5,5,'python')
Method Description
count() Returns the number of times a specified value occurs in a tuple
index() Searches the tuple for a specified value and returns the position of where it was found
Datatype:
*mutable type;
-Lists.
-dictionary.
-set.
1. Lists:
* A list is a select method:
sequence of mutable Python objects like floating numbers, string literals, etc.
* The list can be modified.
* Lists are defined by using square brackets[].
Example : mylist= ['lists',2.0,7,'python']
Method Description
append() Adds an element at the end of the list.
clear() Removes all the elements from the list.
copy() Returns a copy of the list.
count() Returns the number of elements with the specified value.
extend() Add the elements of a list (or any iterable), to the end of the current list.
index() Returns the index of the first element with the specified value.
insert() Adds an element at the specified position.
pop() Removes the element at the specified position.
remove() Removes the first item with the specified value.
reverse() Reverses the order of the list.
sort() Sorts the list.
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
2. Dictionaries:
*Dictionaries are perhaps the most flexible built-in data type in Python.
*Dictionaries, items are sorted and fetched by key, instead of by positional offset.
Example : mydict={ 1:'joshi', 2:'bob', 3:'james'}
where;
1--> key.
Joshi --> value.
Dictionary method:
Method Description
clear() Removes all the elements from the dictionary.
copy() Returns a copy of the dictionary.
from keys () Returns a dictionary with the specified keys and values.
get() Returns the value of the specified key.
items() Returns a list containing a tuple for each key-value pair.
keys() Returns a list containing the dictionary's keys.
pop() Removes the element with the specified key.
pop item () Removes the last inserted key-value pair.
set default () Returns the value of the specified key. If the key does not exist: insert the key, with the specified value.
update() Updates the dictionary with the specified key-value pairs.
values() Returns a list of all the values in the dictionary.
3. Set:
* Set is an unordered collection of items.
* Every element is unique (no duplicates)and must be immutable (which cannot be changed)
Example : myset={1,2,3}
Set method :
Method Description
adds () Adds an element to the set.
clear() Removes all the elements from the set.
copy() Returns a copy of the set.
difference() Returns a set containing the difference between two or more sets.
difference_update() Removes the items in this set that are also included in another, specified set.
discard() Remove the specified item.
intersection() Returns a set that is the intersection of two other sets.
intersection_update() Removes the items in this set that are not present in other, specified sets(s).
is disjoint () Returns whether two sets have an intersection or not.
subset () Returns whether another set contains this set or not.
superset () Returns whether this set contains another set or not.
pop() Removes an element from the set.
remove() Removes the specified element.
symmetric_difference() Returns a set with the symmetric difference of two sets.
symmetric_difference_update() inserts the symmetric differences from this set and another.
union() Return a set containing the union of sets.
update() Update the set with the union of this set and others.
0 Replies to “Automation Testing With Python”
Leave a Reply
Your email address will not be published.