RobotFramework / Selenium: How to set screenshot-name to testcase-name on failure

Viewed 3269

I was wondering if there is a possibility to make the following happen. Let's say I have 3 Testcases with the following results in RIDE:

Testcase Easter  --  PASS
Testcase Christmas  --  FAIL
Testcase Foo  --  PASS

I want to take a screenshot which should be named testcase_christmas.png (or with ' ' instead of '_', that does not matter). Is there a possibility to do it dynamically, something like

${testcase}=    Get Testcase Name
Capture Page Screenshot    ${testcase}

or anything like that? I am using:

  • Python 2.7.x (latest) 32 bit
  • wxPython 2.8 32 bit
  • geckodriver latest 64 bit
3 Answers

Robot framework automatically sets the variable ${TEST NAME} to contain the name of the currently executing test. See Automatic Variables in the user guide)

The documentation for SeleniumLibrary's Capture Page Screenshot shows that you can give it a filename as the first argument.

Putting those two together, you can do this:

Capture page screenshot  ${TEST NAME}.png

The way I would go about this is creating a test teardown and using automatic variables form robot framework. Found here: http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#automatic-variables

Your keywords page / resource file should have a load test data keyword that gets the test name, along with setting a test variable you can assign the screenshot too.

*** Keywords ***
Load Test Data
    ${data}    Get File    ${TEST NAME}.txt
    Set Test Variable    ${data}    ${data}

Common Test Teardown
    capture page screenshot    ${data}.png

Your test should call whatever test teardown you decide to use.

*** Settings ***
Test Setup    Load Test Data

*** Testcases ***
Test Case A
    My keywords
    [Teardown]    Common Test Teardown

Calling the test setup allows you to load the name of each test in your file and in the teardown if it fails will take a screenshot with the test case name you loaded in your test setup.

If you want to save the screenshots on basis of test case ie. a separate folder for all screenshots related to each test case. Then you can use:

Set Screenshot Directory ./Screenshots/${SUITE NAME}/${TEST NAME}

Capture Page Screenshot ABC.png

A Screenshot directory will be created in the project root folder where all the screenshots will be stored in different folders based on the test case and test suites.

For a single suite you can use

Set Screenshot Directory         ./Screenshots/${TEST NAME}
Related