Parallel robot framework tests via locust - robotframework

We are trying to run the automation tests written in robot framework in parallel using locust.
As of now, we have been able to run the robot-framework test file through locust by using the robot-framework's robot package.
Code
1. Locust Script
import robot
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
wait_time = between(0.1, 0.2)
#task
def robot_parallel_test(self):
robot.run('robot_test1.robot')
2. Robot Framework test script (robot_test1.robot)
*** Settings ***
Library Selenium2Library
*** Variables ***
${URL} http://www.google.com
${BROWSER} Chrome
${search_form} css=form[name=f]
${search_query} css=input[name=q]
${search_term} RobotFramework
*** Test Cases ***
Google Search
Open Browser ${URL} ${BROWSER}
Wait Until Element Is Visible ${search_form}
Wait Until Element Is Visible ${search_query}
Input Text ${search_query} ${EMPTY}
Input Text ${search_query} ${search_term}
Submit Form
When we run the locust script, is starts running the robot framework tests by opening instances of Chrome and executing the automation. The problem here is that while one automation script is running, locust opens another instance of chrome which affects the previously running robot test cases and all tests fail.
We have tried running the tests in headless mode but they still fail if another test runs while the current test is running.
We are looking for a way to run our robot automation tests in parallel through locust so that they keep on running in parallel and do not disrupt each other flows, so that we can load test our application with concurrent users.
There is a library pabot which helps run robot scripts in parallel but we are trying to achieve this via locust.

Use pabot for parallel robot tests:
https://github.com/mkorpela/pabot

You don't need locust integration for this parallelism. It's just about to use some async library like gevent (used by locust) to do something like this:
from gevent import monkey
monkey.patch_all()
import gevent
from gevent.pool import Group
import robot
def robot_parallel_test(self):
robot.run('robot_test1.robot')
greenlets_group = Group()
workers = 5
for index in range(workers):
greenlets_group.add(gevent.spawn(robot_parallel_test))
greenlets_group.join()

Related

How do I run a python script in my robot code and terminate running my python code whenever I want?

This is my robot code, here main.py takes input from events.json(I modify this using robot code) and generates a text file. main.py only stops running when it satisfies a condition, after running it will create a text file and I want to terminate the program and then read the results of text file.But the problem is if i run the below code the test cases are not executing the robotcode keeps on running.
See this
robot framework
*** Settings ***
Library JSONLibrary
Library OperatingSystem
Library Process
*** Variables ***
#{events} READY_TO_WAIT STARTING COMPLETE
*** Test Cases ***
Testcase with correct events and checking final state is DONE
Testing with correct events events-Create Dict/Convert json to string/create file
validating state.txt to check if its ending the state with DONE
*** Keywords ***
Testing with correct events events-Create Dict/Convert json to string/create file
Run Process ${CURDIR}${/}main.py
FOR ${event} IN #{events}
${new_dict}= Create Dictionary event=${event}
${json_str1}= Convert Json To String ${new_dict}
Create File ${CURDIR}${/}events.json ${json_str1}
END
Terminate All Processes
validating state.txt to check if its ending the state with DONE
${result}= Get File ${CURDIR}${/}state.txt
Should Be Equal ${result} DONE
How do I run python scripts and terminate them manually?

Use get_log of selenium library to extract all the logs of a session

In robot framework, I created a python script to use get_log. What i want is to log all console outputs during a whole test or session, is that possible?
My script is as follows:
from robot.libraries.BuiltIn import BuiltIn
def get_selenium_browser_log():
selib = BuiltIn().get_library_instance('SeleniumLibrary')
return selib.driver.get_log('browser')

Save results from terminal in robot framework

I am having trouble output/saving the results from my terminal commands.
*** Settings ***
Library Process
Suite Teardown Terminate All Processes kill=True
*** Test Cases ***
Example
Run Process adb devices -l
Current Output
Expected Output
However, if i just run adb devices -l, it will provide me with a list of android devices id.
E.g. List of devices attached
0429329319 device usb: xxxx
My attempts
Based on the robot framework, it has this example that i tried to follow but gave me errors such as "No keyword with name ${result} = Run Process found"
Sample code from robot framework
${result} = Run Process program stdout=${TEMPDIR}/stdout.txt stderr=${TEMPDIR}/stderr.txt
Log Many stdout: ${result.stdout} stderr: ${result.stderr}
Another way that i have discovered is to use 'Get Process Result' keyword.
So my question is - how do i print/save the output of my terminal commands?
Would appreciate if anyone can take a look at it
Referenced to
http://robotframework.org/robotframework/latest/libraries/Process.html
https://github.com/robotframework/robotframework/blob/master/atest/testdata/standard_libraries/process/get_process_result.robot
I just found out one way would be using the OperatingSystem library - 'Run'.
Then log the results of the command entered into the terminal/command prompt using 'Log To Console'
*** Settings ***
Library OperatingSystem
*** Test Cases ***
Get list of devices
${result} = Run adb devices -l
Log To Console [${result}]
To save the printed stuff in the console, just do
robot xx.robot > console.txt
Referenced to - how to run commands in CMD prompt using robot framework

How can I keep the browser open after I open it with a python function Open Browser in RobotFramework

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.

robotframework execute python method against result

I am using Robotframework with Jython. I don't have access to requests module therefore I would like to use native urllib2 to test the respose code of a URL.
So, I am doing something like this
*** Settings ***
Library urllib2
*** Test Cases ***
Test HTTP
${result}= urlopen http://localhost:80
It seems to work but this returns a filehandle. I would like to read the file handle.
Having another python script is out of the question since this is an embedded system.
So, is there a way to evaluate ${result}

Resources