Importing `File Should Exist` Keyword from multiple libraries - robotframework

Robot Framework provides the File Should Exist Keyword in both OperatingSystem and SSHLibrary. This is true for some other Keywords as well but just taking this as an example.
My question is how do I tell my test case which Keyword I am intending to use?
I believe File Should Exist from OperatingSystem works on the local file system (where the TC is being run) and the File Should Exist from SSHLibrary works on the remote server file system. Or am I wrong here too?

You can use both keywords by prefixing the library's name to the keyword.
*** Test Cases ***
Sample Test
SSHLibrary.File Should Exit ${args}
OperatingSystem.File Should Exist ${args}
You'll find more about handling keywords with the same name in the user guide.

You are right. File Should Exist from OperatingSystem works on the local file system (where the TC is being run) and the File Should Exist from SSHLibrary works on the remote server file system.
But you are connecting to a remote system from local. After connecting to remote if you want to do some operation on local you can by using the OperatingSystem library keywords.
So, when you have keywords with the same name in different libraries use the library name as prefix.
*** Test Cases ***
Sample Test
SSHLibrary.File Should Exit ${args}
SSHLibrary.Get File <filename>
OperatingSystem.File Should Exist ${args}
In the above case even though you connected to remote system, still you can do operations on local system.

Related

Can Robot Framework test suites be called with command-line parameters?

We are developing / publishing robot-tests in a git environment, where the public (published) git has slightly different settings then the development git (due to personal work-space (development) vs global environment settings).
Because of this one of our robot keywords is different for development and public.
Currently we manage this by commenting out 1 line in the keyword's .robot implementation for the development git and another one for the public git, but this is error prone.
Is it possible to call pybot with some extra command-line options which will be passed on via the .robot files to the python files implementing the final keywords so we can move the 2 lines from the .robot file to the python implementation and decide which to use based on the given command-line option?
Check the Setting variables in command line part of the user guide. With the --variable MYVAR:value command line argument you can create global variables.
For example:
*** Variables ***
${VAR} localenv # this is not necessarily needed, it is used to set default value
*** Test Case ***
Test
IF '${VAR}' == 'localenv'
Log Do this
ELSE IF '${VAR}' == 'globalenv'
Log Do that
END
Run locally
robot test.robot
on global:
robot --variable VAR:globalenv test.robot

How to open an excel file explicitly using Robot Framework in Windows

I need to open an Excel file explicitly to perform further operations using white library.
I tried this in Windows OS. I tried to open the excel file using ExcelLibrary-Open Excel keyword and also Run keyword of Operating System Library. But in both cases, system is not opening file explicitly.
Approach 1 - Using Open Excel keyword of Excel Library
*** Settings ***
Library WhiteLibrary
Library OperatingSystem
Library ExcelLibrary
*** Test Cases ***
Open Excel and perform Operation
Open Excel D:/Regression_Suite/Test_Cases.xls True
Sleep 10s
Approach 2 - Using Run keyword of Operating System
*** Settings ***
Library WhiteLibrary
Library OperatingSystem
Library ExcelLibrary
*** Test Cases ***
Open Excel and perform Operation
Run C:/Program Files (x86)/Microsoft Office/Office15/EXCEL.EXE ${EXECDIR}${/}Test_Cases.xls
Sleep 10s
I expect the Excel file to opened explicitly, so that I can perform further operations using WhiteLibrary
Use python's os.startfile() call - this will open the file with the associated program, the same as double clicking is in Explorer:
Evaluate os.startfile("${EXECDIR}${/}Test_Cases.xls")
The call is going to return control to your next code line immediately, so you have to wait a bit for Excel to actually start before interacting with it.

how to run commands in CMD prompt using robot framework

How do i run the command 'ipconfig' in cmd prompt using robot framework.
running below code gives me the complete result. I just need IPV4 Address.
${frt}= | Run | ipconfig
Log ${frt}
Although it would propably be more effective of use a custom Python library, it is possible to accomplish with regular Robot Framework:
*** Settings ***
Library OperatingSystem
Library String
*** Test Cases ***
Test IPConfig
${frt}= Run ipconfig | find "IPv4"
${IP}= Fetch From Right ${frt} ${SPACE}
Log To Console [${IP}]
Create .bat file and write your commands in that. If your .bat file located in other folder then use cd commands and then your required commands
Example of bat file like
cd C:\robotFramework\runner
java abc.class
Use following Keyords
Run xyz.bat : for this use Library OperatingSystem
Or
Run Process xyz.bat : for this use Library Process

setup pythonpath before starting test suite

Currently I am setting pythonpath as pybot --pythonpath ~/Test_suite main.robot while running the tests.
I also see there is option Set Environment Variable PYTHONPATH ${CURDIR} to set through robot framework. But it doesn't run before main settings
*** Settings ***
Documentation Suite description
Resource settings.robot
And below is settings.robot file
*** Settings ***
Resource keywords/keywords_test.robot
Library tests.test_1.TestClass
How to setup the pythonpath before running the suite?
You can't do what you want. The settings are all processed before any test or keyword is run. You can use the --pythonpath option from the command line, or set the environment variable PYTHONPATH before starting your test.
You can do it by adding to sys.path in suite init.
E.g. you can create __init__.robot file in tests directory with:
*** Settings ***
Suite Setup Setup before all tests
*** Keywords ***
Setup before all tests
evaluate sys.path.append(os.path.join("path", "to", "library")) modules=os, sys
Described in official docs: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#configuring-sys-path-programmatically
I'm not sure if this would work, but I think there actually might be a way to do what you're talking about. It's pretty jenky, so bear with me.
You can't really do it in Robot Framework, but if you expand your horizons to Python, there's a neat little exploit I've found. If you take a look in other posts how to create a custom Python Library for Robot framework, you'll notice that there's a required method called __init__ with parameters of (self). I believe that this is the very first code to run when the Library instance is created. Little-known fact, you can add parameters to the creation of the Library instance. In your case, ~/Test_suite would be the value that you would pass.
IN THEORY, because I haven't tried this, you could tell __init__(self, path_in) to run your BuiltIn keyword with the following code:
self.BuiltIn().set_environment_variable('PYTHONPATH', path_in)
Don't forget to use the following import statement at the top of the file.
from robot.libraries.BuiltIn import BuiltIn

Windows Command prompt shell for sqlite3

The documentation states that there is a command-line shell for sqlite3:
To start the sqlite3 program, just type "sqlite3" followed by the name the file that holds the SQLite database."
When I try this, in the Windows Command Prompt I get the error message:
'sqlite3' is not recognized as an internal or external command,
operable program or batch file.
Windows explorer reveals several 'Sqlite3" folders in various places:
backends(C:/Python26/Lib/site-packages/django/db)
Lib(C:/Python26)
backends(C:/Django-1.1.1/Django-1.1.1/build/lib/django/db
backends(C:/Django-1.1.1/Django-1.1.1/django/db)
How do I access the shell, can anyone help?
Download sqlite3 binary for windows here. Unzip it and put it somewhere in your path.
That's the error message you get if you try to run any executable that's not in your current directory or in the path.
To correct the problem, find the SQLite executable (SQLITE3.EXE), and run it from the directory in which it resides, or add SQLITE3.EXE to your PATH environment variable.
You have to properly set your PATH environment variable to include one of the locations where sqlite3.exe resides. Usually SQLite seems to set that environment variable upon install but the list of paths where you found it indicates that it just came as a library for various other applications. Therefore it's not too surprising that the path isn't set.
I have sqlite3 on my machine, and as others have mentioned it must be located within a folder specified by your PATH environment variable. Since I use it a lot, I threw it in windows\system32, which is where I place a lot of utilities like pstools.

Resources