jeudi 11 juin 2015

Automatic Selenium screenshot with TestNg, Maven and Jenkins CI

Recently I was working on the automatisation of Selenium tests.
We have built a pretty classical stack for running test:
  1. Jenkins master - main entry point and orchestrator of the build
  2. Jenkins swarm client on a Windows 8 PC with IE 11 - where the builds are executed
  3. Maven Surfire Plugin
  4. TestNg
  5. Selenium
One of the problem I encountered was not very user-friendly alert about failed tests. Jenkins send an email with a link to the unstable build. No details about failed tests, no exceptions, just a link. So I need to open my laptop, turn on my VPN, go to Jenkins server and see the details.
Too many steps.

It would be great to get a detailed alert. It's not a problem with Jenkins' Editable Email Notification and special token ${FAILED_TESTS}. This token will bring in your email detailed information about failed test. I love stacktraces, but for normal people it's a mess and frankly speaking it takes some time to understand what's going on even for a geek. 

So my idea was to send a screenshot of every failed test within Jenkins alert notification. So this way, it takes a second to see the broken page and understand the root cause.

For this we will need to use the following tricks:
  1.  org.testng.TestListenerAdapter.onTestFailure is triggered when a test is failed.
  2.  org.openqa.selenium.TakesScreenshot takes a screenshot
  3.  copy the screenshot to maven target folder for further usage
  4.  use Jenkins Editable Email Notification/Attachments feature
First 3 steps are already implemented in this maven project -
To use it you need to add the following dependency in your pom.xml file which holds the selenium tests.
The 4 step is only about jenkins config:
    Go to your Jenkins job configuration 

    Modify the regexp path to match your maven project holding the selenium test
    Post your comments here or in my twitter if you have some questions or idea to improve this stuff.

    4 commentaires:

    Unknown a dit…


    thank zou again for your help here:

    I have one question/suggestion. I am testing advertising platform. I create marketing campaign, open new tab where I check the result of campaign creating, close this new tab and go to original tab, where I change some settings and send campaign again.

    I realized, that if the test fails on newly opened tab, screenshot is taken on the original tab and I cannot see the problem, which is on the new tab.

    Do you think I should change something in my test or this behavior is normal for your tool?

    Thank you again.

    Voronetskyy Yevgen a dit…


    As far as I understand you are trying to test 2 different pages simultaneously. Correct me if I missed something.

    Maybe you should use an another (second) instance of WebDriver for your test campaign page.

    Unknown a dit…

    Well. The point is, that if I go to check created campaign on same tab, I would loose my settings in forms for new campaign.

    So I open created campaign on new tab, make the check and then go back to original tab where I continue the work - change settings and send another new campaign. I am testing how the system works with different combinations of settings.

    I always work only in one tab at the same time. I was thinking, that the problem may be, that after failure, new tab closes before the screenshot is taken. So I see screenshot of the original tab...

    Voronetskyy Yevgen a dit…

    To be honest, I didn't catch your workflow.
    If you think that the problem is a timing issue, you can add a Thread.sleep(1000) instruction before closing tab. Hovewer it is a bad style.