How to pass **kwargs values from Robotframework to python class
At times its become very confusing how to pass **kwargs values to robotframework.
In below post , i will try to explain you , how you can pass your **kwargs value to python class from robot file.
Lets suppose we have file Demo_kwargs.py , which accept **kwargs value from the user.
output would be
This work absolutely fine with python
But what if you have created this class to be used as library in robotframework and you need to use the value of ip,username,password in your robot scripts .
Lets create a robot file (check.robot) and see how it goes
If you run this code, all you would get is 'None',because you never passed any values to class Demo_kwargs.
However, lets modify our scripts little bit
If you run this code , you will see the output would be
Now lets use all the three files ( Demo_kwargs.py,Data.py,check.robot) to do our task
We will use Data.py as --variable file,lets modify our script (check.robot)
run command now :
pybot -V Data.py check.robot
Output
When you pass Data.py with -V i.e. --variablefile option , it stores all the variable values with reference of object Server_01
You can then pass each object like above to get the desired result
In below post , i will try to explain you , how you can pass your **kwargs value to python class from robot file.
Lets suppose we have file Demo_kwargs.py , which accept **kwargs value from the user.
Demo_kwargs.py
from subprocess import Popen, CREATE_NEW_CONSOLE,PIPE,STDOUT
import time
import subprocess
import datetime
import os
class Demo_kwargs:
"""Demo class"""
def __init__(self,**kwargs):
self.filters=''
self.window_ip = kwargs.get('ip')
#print type(self.window_ip)
self.window_user= kwargs.get('username')
self.window_password= kwargs.get('password')
self.dest_path= kwargs.get('Target_path')
self.interface= kwargs.get('interface')
self.terminal='cmd'
self.home=kwargs.get('Home_path')
def test(self):
print 'Checking kwargs .....'
return self.window_ip
Now let's call this file from python file 'Data.py'
Data.py
from Demo_kwargs import *
SERVER_01 = Demo_kwargs(
name='WIRESHARK_ENV91',
ip='192.168.1.16',
username=r'INTRA\pmmm', #always prepend r , before giving your username and password
password='jan@2018',
prompt='$ ',
autostart=False,
unzip_capture=True,
filter='',
#interface=['ens2f0'],
interface='Ethernet',
Target_path=r'D:\Users\pankaj-m\Desktop\Test'
)
print SERVER_01.test()
output would be
Checking kwargs .....
192.168.1.16
This work absolutely fine with python
But what if you have created this class to be used as library in robotframework and you need to use the value of ip,username,password in your robot scripts .
Lets create a robot file (check.robot) and see how it goes
check.robot
*** Settings ***
Library Demo_kwargs.py
*** Test Cases ***
Test
check
*** Keywords ***
check
${abc} = test
log to console ${abc}
If you run this code, all you would get is 'None',because you never passed any values to class Demo_kwargs.
However, lets modify our scripts little bit
*** Settings ***
Library Demo_kwargs.py ip='10.20.20.20' WITH NAME Mylib
*** Variables ***
${window_ip }
#&{names} = Create Dictionary 0=First Name 2=Email
*** Test Cases ***
Test
check
*** Keywords ***
check
${abc} = test
log to console ${abc}
If you run this code , you will see the output would be
============================================================================== Check ============================================================================== Test '10.20.20.20' | PASS | ------------------------------------------------------------------------------
Now lets use all the three files ( Demo_kwargs.py,Data.py,check.robot) to do our task
We will use Data.py as --variable file,lets modify our script (check.robot)
check.robot
*** Settings ***
Library Demo_kwargs.py ip=${SERVER_01.window_ip} username=${SERVER_01.window_user} password=${SERVER_01.window_password} Target_path=${SERVER_01.dest_path} interface=${SERVER_01.interface} Home_path=${SERVER_01.home} WITH NAME Mylib
*** Variables ***
${window_ip }
#&{names} = Create Dictionary 0=First Name 2=Email
*** Test Cases ***
Test
check
*** Keywords ***
check
${abc} = test
log to console ${abc}
run command now :
pybot -V Data.py check.robot
Output
============================================================================== Check ============================================================================== Test '10.20.20.20' | PASS | ------------------------------------------------------------------------------
When you pass Data.py with -V i.e. --variablefile option , it stores all the variable values with reference of object Server_01
You can then pass each object like above to get the desired result
Comments
Post a Comment