How to use custom library in Robot Framework

RobotFramework have rich set of libraries to do all the necessary tasks, but sometimes we are not that much familiar with the library and we want to write our own method and want to import that method as a library in RF.
Let me take an example here, suppose you have a CSV file

data.csv


Name,Age,region,country,Marks
pankaj,22,delhi,india,45
neeraj,32,noida,india,75


Task is to find value of 2nd Row and 3rd Column from a Robot Script.

How one like to proceed from here ?.

There are two options to do this

1. Using existing Libraries developed by other users

you can use libraries created by other users and see if that solves our purpose

e.g. for this use-case you can search some csv library for robotframework available in git hub

These libraries have enough documentation and example to download and use them. 

2.Creating our own library to do the task

if you are well versed in python or java and if you know how it can be done in your preferred language, then the solution is super easy.

Let’s proceed with python, I have created below function which will parse the csv and help us to find the value of any desired row or column

csv2.py

  1. import csv  
  2. def go_to_nth_row_nth_column(File,row_no,col_no):  
  3.     inputFile = File  
  4.     row_no=int(row_no)  
  5.     col_no=int(col_no)  
  6.     with open(inputFile) as ip:  
  7.         reader = csv.reader(ip)  
  8.         for i, row in enumerate(reader):  
  9.             if i == row_no:      # here's the row   
  10.                 #print row[col_no] # here's the column  
  11.                 return row[col_no] 

if i run this function
  1. qa=go_to_nth_row_nth_column("data.csv",2,1)  
  2. print qa  

output is
32

which is 2nd row 1st column

Now, we have created a function, lets  use this in robot framework.

here is how our robot file should look like

Test.robot
  1. *** Settings ***  
  2. Library    csv2.py #both robot and python file is in same folder  
  3.   
  4.  *** Test Cases ***  
  5. Test  
  6.     Check row column  
  7.     Search String  
  8.   
  9. *** Keywords ***  
  10. Check row column  
  11.     ${result} =    go_to_nth_row_nth_column    data.csv    2    1  
  12.     log  ${result}  

*** Settings ****

inside settings section , if python and robot file are with in same folder then you can simple refer the python file as shown below.


When you see the output




This is the way by which you can create your custom libraries and import in RF.

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to connect adb devices to linux container

How to start with APPIUM PYTHON

Configuring jenkins with Robot Framework