Using SSHLibrary currently I execute ssh commands in test suite file or keyword file. Is it possible to do that in my current library file? Meaning my library function just forms the string now. I wanted to executed that in ssh connection in library file.
test/testsuit.robot
*** Settings ***
Library SSHLibrary
Library ${EXEC_DIR}/lib/mylib.py WITH NAME llib
Suite Setup open_connection_and_login
Suite Teardown Close All Connections
*** Variables ***
${HOST} 10.x.x.x.x
${USERNAME} myuser
${PASSWORD} mypassword
*** Test Cases ***
example test
${sshstring}= llib.form_the_sshstring ls
${output}= Execute Command {sshstring}
*** Keywords ***
open_connection_and_login
Open Connection ${HOST}
Login ${USERNAME} ${PASSWORD}
lib/mylib.py
def form_the_sshstring(input):
sshstring = "{}".format(ls)
return sshstring
Expecting something like..
*** Test Cases ***
example test
${output}= llib.run_the_sshstring ls
/lib/mylib.py
import SSHLibrary
def run_the_sshstring(input):
sshstring = "{}".format(ls)
out = SSHLibrary.SSHCleint(sshstring)
return out
From within your library file you can get a reference to the imported SSHLibrary -- and thus, it's keywords -- with the built-in keyword Get Library Instance. With that, you can call the SSHLibrary keyword Execute Command
Example:
# mylib.py
from robot.libraries.BuiltIn import BuiltIn
def run_the_sshstring(input):
sshlib = BuiltIn().get_library_instance("SSHLibrary")
result = sshlib.execute_command(input)
return result
Related
I am trying to make a testcase in robotframework with SSHLibrary and running into an error trying to connect with the FTP server. The error I get is keyword login.login expects 0 arguments and gets two. I do not really understand why it does expect 0 arguments.
the resource file in which the keyword file exists has the following code:
*** Settings ***
Library SSHLibrary
*** Keywords ***
lOGIN
Set Default Configuration 30s
Open Connection ${HOST}
Login ${USERNAME} ${PASSWORD}
The test I like to run looks like:
*** Settings ***
Documentation Example of testing an accrual for TLOG
Resource ../Resources/login.robot
*** Variables ***
${HOST} sitenv
${USERNAME} crmapplication
${PASSWORD} Company11*
*** Test Cases ***
logintositenv
login.login
Any idea what is going wrong? I am especially surprised I cannot get it to work because I used the open connection and login keywords from SSHLibrary before when I was trying out a few things and it worked fine.
As you said login.login That tells me you have probably another library that has keyword login. In this case you have to specify that you want to use SSH library.
You can do this with
SSHLibrary.Login
Here you can have a look how to import library with custom name and use keywords
How can you run keywords in the robot framework if the file exists in the filesystem?
For example:
Run Keyword If ${filename} exists Delete File
OperatingSystem library could be used for this, even though there's not exactly any keyword for what you need. But you can get creative and perhaps use Get File, Get File Size, List Files In Directory, Run And Return Rc or even something else. There are also keywords like File Should Exist, File Should Not Exist, Should Exist. Perhaps you can change your code so you can use these.
Or you create your own simple library:
Libraries/file.py
import os
def file_exists(file):
return os.path.isfile(file)
import it and use it like you mentioned in your question:
Tests/test.robot
*** Settings ***
Library ../Libraries/file.py
*** Test Cases ***
Test File Exists
${fileExists}= File Exists test.robot
Run Keyword If ${fileExists} is True Log To Console Exists!
When I need this kind of action, I code something like that... maybe can work for you.
*** Settings ***
Library Process
*** Variables ***
#{FILES} ${CURDIR}/someFolder/aaa.pdf
... ${CURDIR}/someFolder/bbb.pdf
*** Test Cases ***
Check and Delete Files
FOR ${file} IN #{FILES}
${out} = run process ls ${file}
run keyword if ${out.rc} == 0 Delete File ${file}
END
*** Keywords ***
Delete File
[Arguments] ${f}
${o} = run process rm -f ${f}
Should Be Equal As Integers ${o.rc} 0
Get the File status and then use the status as
${file_exists}= Run Keyword and Return Status File Should Exist test.robot
IF ${file_exists}
Log File Exists
END
There are 2 files
Testcase/feature1.robot
POM/feature2.robot
feature1.robot
*** Settings ***
Library SeleniumLibrary
Resource ../POM/feature2.robot
*** Variables ***
${username} xxxxxxxx
${password} xxxxxxxx
*** Keywords ***
Login in DataRPM
#[Arguments] ${username} ${password}
Input Text ${email_id} ${username}
feature2.robot
*** Settings ***
Documentation This contains all the locaters of Login Page
*** Variables ***
${email_id} xxxxxxxx
But here I don't want to use the email_id locator to be mentioned in the variable section. If I am mentioning the ${email_id} in this feature file I am able to go ahead without any error. I want to use the locator in the feature2.robot file which is under my POM directory. Then I want to call the variable from feature2.robot.
I have mentioned the path in the feature1.robot but still I am getting error.
[ ERROR ] Error in file feature1.robot': Resource file 'path' contains
a test case table which is not allowed.
The error is pretty descreptive, while you not showing use you probably have test cases implemented in feature2.robot and in that case it is a suite file.
But you are trying to use feature2.robot as a resource file, and as the error message states, resource files are not allowed to have test case table.
You should create a third file like POM/locators.robot:
*** Settings ***
Documentation This contains all the locaters of Login Page
*** Variables ***
${email_id} xxxxxxxx
and use this in both feature1.robot and feature2.robot like:
Resource ../POM/locators.robot
Resource locators.robot
From Robot Framework 3.1 *.resource file extension is supported, so if you have version 3.1, you should use locators.resource to be more explicit about that this file is a resource file and not a test suite.
your question is not clear, However , i would try to show an error free example using your code only, with slight modification.
feature1.robot
*** Settings ***
Resource ../Data/Feature2.robot
*** Variables ***
${username} pankaj
${password} xxxxxxxx
*** Test Cases ***
Login
Login in DataRPM
*** Keywords ***
Login in DataRPM
#[Arguments] ${username} ${password}
log to console ${email_id}
log to console ${username}
Feature2.robot
*** Settings ***
Documentation This contains all the locaters of Login Page
*** Variables ***
${email_id} pankajigec26#gmail.com
if you run feature1.robot you will not get into any error .
In your case , please check if you are running the same code which you have pasted here.
Run the robotframework remote library keyword with TypeError
remote.py code
from robotremoteserver import RobotRemoteServer
from WiseLibrary import WiseLibrary
RobotRemoteServer(WiseLibrary(),host="0.0.0.0")
test.robot code
*** Settings ***
Library Remote http://127.0.0.1:8270/
Suite Teardown Delete Wise Sessions
*** Test Cases ***
test01
${name} Generate App Name 5
${resp} Create Application name=${name}
Verify Code And Data ${resp} 201 name=${name}
[Teardown] Delete Application Teardown ${name}
It looks like Connection got established to the library because I can see the reflection of keywords in RIDE
python -m robot.libdoc Remote::http://127.0.0.1:38270 list
Create Application
Delete Application Teardown
Delete Wise Sessions
Verify Code And Data
Generate App Name
......
But while executing any keyword I am getting following error:
FAIL : TypeError: run_keyword() takes exactly 4 arguments (3 given)
I have the following python file OpenBrowser.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def openit(browser):
chrome_options = Options()
chrome_options.add_argument("--headless")
desired_capabilities = chrome_options.to_capabilities()
desired_capabilities['acceptInsecureCerts'] = True
driver = webdriver.Chrome()
#driver = webdriver.Chrome("C:\Python27\Scripts\chromedriver.exe", chrome_options=chrome_options,desired_capabilities=desired_capabilities)
driver.get("http://www.python.org")
return browser
and a robot file:
*** Settings ***
Documentation Suite description
Library OpenBrowser.py
*** Test Cases ***
Test title
openit browser
The browser is open, but then it closes and if I want to run another keyword in RF I get error: No brpwser is open
How can I run the python function and keep the browser open?
From the top of my head this should allow you to do what you want:
*** Settings ***
Library SeleniumLibrary
Suite Teardown Close All Browsers
*** Test Cases ***
TC
# Options for startin Chrome
${chrome_options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${chrome_options} add_argument headless
Call Method ${chrome options} add_argument ignore-certificate-errors
# Arguments for starting ChromeDriver
#{service_args} Create List
... --verbose
... --log-path=${EXECDIR}/chromedriver.log
Create Webdriver Chrome chrome_options=${chrome_options} service_args=${service_args}
Go To https://self-signed.badssl.com/
Capture Page Screenshot
The service arguments will instruct ChromeDriver to generate a log file for you in the directory where you start Robot Framework. This may help with the analysis.