Execution Level Setup & Teardown with Robot Framework - robotframework

I understand there's Test Setup which will get executed before every test case and Suite Setup which will get executed before every suite (i.e. each .robot file).
However, I'm trying to do Setup and Teardown of the command level which is Setup once I run the robot command and when all test suites have ran, run Teardown.
Tried with having __init__.robot file in my scenario directory but they didn't get called at all.
*** Settings ***
Resource ../_common/keywords.robot
Suite Setup Prepare Browser
Suite Teardown Close Browser
I want to be able to launch the browser before any test is started and then close browser only after all tests are completed.
For example, robot 1.robot 2.robot should:
Open browser
Run 1.robot test suite
Run 2.robot test suite
Close browser

You could do that by having "special" suites just for that, and call them first and last in a run. With the SeleniumLibrary having a global scope, the browser initialized in the first one should be accessible for all follow-up suites in the same run.
E.g. the suite "Startup.robot" will open the browser, and "Closing.robot" will close it, and any in between will use it:
robot Startup.robot 1.robot 2.robot Closing.robot
When you pass a directory for execution, the framework takes the .robot files in it in alphabetical order, so you can name those special suites "0000_Startup.robot" and "zzzz_Closing.robot" for them to be ran in the corresponding order (that's if you use only ascii/latin file names).
And yep, the initialization files are not used to run something before the other suites - they are there to set default values for those other suites, which can be overridden downstream. See their description in the documentation.

Related

How to mark pass the test when is retested in robotframework

Right now when my team made a deploy in out QA environment, I run one suite tests of robotframework. This, have several test that failed at first cause the environment is not "warm up". So, in the same pipeline I have the "--rerun" option if some test has failed. Usually, in the second run they work just fine. Then, I merge the outputs with rebot:
rebot --merge output.xml output2.xml
And even the log.html is showing correctly the information (on the test and on the suite level).
Now comes the fun part. Even when in the output.xml now I have two runs of the test (all with some failled and the retries with all pass), when I upload this to XRay, it create a test execution with the results of the first run only.
So, my question is: why? In the output.xml is clearly al the results as the last run. If not, I would understand that this create one test execution and then put all the results inside (first and second run) which is not the case.
It seems to me that XRay is not importing the data correctly.
First, I have never used rebot in that way.
However, I think what you obtained in Xray is the consequence of the "merged" output.xml you have and how Xray works.
Whenever you upload test automation results to Xray, usually a Test Execution is going to be created, containing Test Runs (one per each Test)🤞🏻.
Test issues will be autoprovisioned unless they already exist; if they exist, only Test Runs will be created for those Tests.
A Test Execution cannot contain more than one Test Run for each Test. In other words, a test execution is like a task for running (or that contains the results of) a list of tests; this list cannot have duplicates.
I would advise to upload all the reports, individually. Xray will consider the latest results whenever showing coverage status.

Cypress test data generation scripts not as part of the test suites

I wrote a couple of Cypress end-2-end tests for our website. These tests run against a staging environment. Since we have a couple of those staging environments for our dev teams I would like to make sure these tests are stable on all stagings.
For some tests I need certain test data, so I wrote a cypress test, creating that test data. Normally this test data generation test is not executed on our CI system. This test is located in a separate file within the integration directory, so that cypress is able to find and execute it. It is just executed once per staging environment. The test data will just remain there and does not have to be generated again and again.
When opening the cypress GUI (cypress open) I would like to have this test data generation test being ignored by cypress so that I can simply run all suites at once. But, when I add this test data generation test to the set of ignoredTestFiles, I can't run the test data generation test at all anymore.
Do you have an idea how I can make the cypress GUI ignore my test data generation test on the one side and on the other keep it executable by cypress, when I explicitly want it to execute?
You could make the test generation data test depend on an environment variable, and only set that variable when you explicitly want it to execute. Something like:
if (Cypress.env('GENERATE_TEST_DATA')) {
// Generate your test data here
// You could even put this if around the entire it block
// so the test doesn't execute at all when the environment variable isn't set
}
Then when you want the test data to be generated, set the environment variable like this:
CYPRESS_GENERATE_TEST_DATA=true npx cypress run
Also see the documentation on Cypress.env()

ReTest bloc of tests in Robotframework

I have a list of tests to execute on robot framework where it has a block of tests that can be executed again if a specific test fails as the following flow explains, And i want to know if this is dowable with robot framework.
No, it is not doable. At least, not in a single test run, and not without a lot of work. Robot has no ability to re-run a test within a single test run. You would have to exec a second instance of robot where the output is sent to a separate output file, and then you would have to somehow merge the output files of the original test run and the second exec.
However, robot does support being able to give it the output.xml from a previous run so that it will re-run only the failed test cases. You can do that with the --rerunfailed command line option. See Re-executing failed test cases in the robot framework user guide.

Appium script on an already launched app

I am automating android app tests with Appium, and I need to split my test script in half while the app is still running.
The first script initiates a log into the app, and the next script achieves it. Therefore the second script must get the app in the exact same state as it was left by the first step. The app cannot be closed meanwhile.
Is there a way to do this, and if there is, how?
Thank you!
This is quite simple.
Create your test suite directory and insert Initialization file into it.
In the Initialization file do the following setup:
Suite Setup Open Application
Suite Teardown Close All Apps
Then then in the scripts in this directory do not Open Application, just add your Appium keywords and those will use the current open application.
I use this when I need to do something in iOS application (AppiumLibrary), then do operation over same data on web client (SeleniumLibrary) and then finish in iOS and finally check in web client/database.
The file structure is something like this:
__init__.robot
01_login_appium.robot
02_do_appium_stuff1.robot
03_do_selenium_stuff1.robot
04_do_appium_stuff2.robot
...
Alternatively if you just want to have separate script for logging to reuse in scripts, I prefer to create keyword resource file for setup, logging or other common tasks where I would encapsulate several steps in such keyword and reuse it in setup for different scripts.

Using Test Configuration when running on-demand automated tests from VSTS Test Hub

My organization has moved from executing automated tests within MTM to executing automated tests within the VSTS Test Hub, via Release Definitions. We're using MSTest to run tests with Selenium / C#.
It is a requirement for us to be able to run any individual test on-demand, post-build, and to use the Test Configuration to control the browser. This was working without an issue when runnings tests through MTM.
Previously, when running tests through MTM, my MSTest TestContext's "Properties" were populated with runtime parameters, such as the Test Configuration, such as TestContext.Properties["__Tfs_TestConfigurationName__"]. The runtime properties could also be seen in the resulting .trx file.
Now, when running tests through the VSTS Test Hub, those values are no longer set in TestContext automatically, nor do I see them in the .trx file, or in my Release deployment logs.
Are there any suggestions as to how the Test Configuration values of each test can be propagated into TestContext (or accessible via code otherwise) to be able to control the browser at test run-time?
I have considered creating a .runsettings file, but am unaware as to how Test Configuration variables would be dynamically populated into that file.
I have considered attempting to query the Test Run / Test Results in my Selenium code to determine the Test Points / Configuration, but am unaware of how to determine the Test Run ID (I see the Test Run ID in the release logs, but don't know how to programmatically access it)
I have seen a VSTS Extension called 'VSTS Test Extensions' which is purported to inject the VSTS variables into a .runsettings file, but there are no details as to how to configure the .runsettings file in order to accomplish this, and the old MTM-style parameter names do not seem to work.
Are Test Configuration variables accessible globally in VSTS somehow?
I.e. could I create a PowerShell Release task to somehow access test run / test point data? Microsoft does not provide any indication about how to use Test Configurations other than using them for manual tests.
Any help is greatly appreciated.
After some more review, it seems that my only option is a giant hack:
I noticed that in the Release logs, the [TEST_RUNID] is output, which points me to the $(test.runid) variable, which I didn't know existed.
I can output the Run ID to an external file via a Powershell Release task.
I can then have my test code read that file, perform an API call to VSTS querying the Test Results for that Run ID, and then use that response to output the Test Configuration Name for each Test Case ID.
I can then match up my Test Case ID within each of my test methods with the Test Case IDs from the service call response, and set the browser according to the Test Configuration Name.
The only issue then is if the same test exists multiple times in the same run with different configurations, which can be hacked around by querying the Test Run every time a test begins and checking which configuration has already been run based on the State ("Pending", etc.) or by some other means I haven't thought of yet.

Resources