Tutorial # 6 – Python basics for robot framework

This article covers the python basics required to write Robot Automation Scripts, will also discuss about Python Variables, Data Types,Operators,Control Flows and Functions

No comments

What is Python?

Python is a programming language, we are using python syntax to write test automation scripts for Robot framework, However, Robot framework has it’s own way of defining variables, Lists and Dictionaries. first let’s discuss about Python basics, we can then move on to other important concepts in Robot framework.

Prerequisites to compile python program

  • Install Python and Pycharm (Refer Tutorial#1)

Variables and Data Types:

A variable is used to store values, python is dynamically typed which means that you don’t need to specify data type for variables, so you can type xyz=100, which will become integer data type.

Integer Example 
a = 123 print("Value of a :" + str(a))
# Assign same value to Multiple variables
a=b=c=100
print("Values of a,b,c: " + str((a,b,c)))
#Assign Multiple values to multiple variables
a,b,c=20,30,40
print("Values of a,b,c: " + str((a,b,c)))


Output
Value of a :123
Values of a,b,c: (100, 100, 100)
Values of a,b,c: (20, 30, 40)
Float Example 
x = 15.6
print("Value of x: " + str(x))
output Value of x: 15.6

String Examples

str = "Python for Robot framework" 
print("Complete String: "+str)
print("First letter of a String: " +str[0])
print("First two words of a String: " +str[0:10])
print("Omit First 3 Letters and Print rest of a String: " +str[3:]) print("Multiply String :" + str * 2)
print("String Concatenation: "+ str +"&"+ " Automation is Awesome")

Output
Complete String: Python for Robot framework
First letter of a String: P
First two words of a String: Python for
Omit First 3 Letters and Print rest of a String: hon for Robot framework
Multiply String :Python for Robot frameworkPython for Robot framework String Concatenation: Python for Robot framework& Automation is Awesome

List: List is used to store collection of values

List Example

list = ["Python","Robot","Selenium","RPA"] 
print("Complete List: " +(','.join(list)))
print("First word in a List: "+ list[0])
print("Third word in a List: " + (''.join(list[2:3])))
print("Fourth word in a List: " + (''.join(list[3:])))
print (len(list))
Output
Complete List: Python,Robot,Selenium,RPA
First word in a List: Python
Third word in a List: Selenium
Fourth word in a List: RPA
4

Tuple:

A Tuple is like a list, but the difference is that you can’t update the values in Tuple list whereas you can update the values in List

Example

tuple = {'AutoIt', 'PDF','API','REST'}
print(tuple)
# append a new name will throw an error

tuple.append('excel')
print(tuple)
Output
{'AutoIt', 'PDF', 'API', 'REST'}
AttributeError: 'set' object has no attribute 'append'

Dictionary

  • The python dictionary is an ordered collection of values
  • Dictionary is a set of Key:Value pairs and Keys are unique within a same dictionary.
  • The main operation that you perform on the dictionary is that storing a value in a key and extracting the value with the given key
Dictionary Examples

dict = {1:'Test',2:'Automation',3:'Solutions'}
print("\n print value of Key 1:")
print(dict[1])
#you can use Get method also to fetch the key values
print(dict.get(3))
# if the key is not defined, it will return None
print(dict[4])
Output
print value of Key 1:Test
Solutions
None

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s