Tuesday, February 21, 2017

PyTest with Selenium - HTML report with Screenshot

recently I was working with Py.Test with selenium. It's really powerful tool with awesome fixtures. It generates JUnit XML report as well which is good to be consumed by Jenkins. However, I needed an HTML report as well with embedded screenshot for the ease of identifying the issue. And this is how I did it finally after doing some googling.

Generate HTML Report of PyTest 

Pytest-html plugin is available which you can use to generate the beautiful Report. The all you have to do is install the plugin and run your test case with this plugin. 

Embede ScreenShot When Test Case Fails

PyTest-html plugin allows you to extend the plugin and that's the best thing about this plugin. 

from selenium import webdriver
import pytest
driver = None

def pytest_runtest_makereport(item):
    Extends the PyTest Plugin to take and embed screenshot in html report, whenever test fails.
    :param item:
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])

    if report.when == 'call' or report.when == "setup":
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            file_name = report.nodeid.replace("::", "_")+".png"
            if file_name:
                html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
                       'onclick="window.open(this.src)" align="right"/></div>' % file_name
        report.extra = extra

def _capture_screenshot(name):

@pytest.fixture(scope='session', autouse=True)
def browser():
    global driver
    if driver is None:
        driver = webdriver.Firefox()
    return driver
def test_screenshot_on_test_failure(browser):
    # driver = webdriver.Firefox()
    assert False

Tuesday, March 29, 2016

linux java shows older version after upgrade

While working with Linux today I stumbled upon a weird problem, even after upgrading java to 1.8 it was showing 1.7 only.

And when I tried to install it again, it showed me following message.
sudo yum install java-1.8.0-openjdk.x86_64

Package 1:java-1.8.0-openjdk- already installed and latest version

Nothing to do
Following command help me to figure out what's going wrong. 

Following command will show you all the java versions currently installed on your system, it will also show you which one is currently active.  If you need to change just provide the number and that would be activated. 

sudo update-alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
 + 1           /usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin/java
*  2           /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java

Enter to keep the current selection[+], or type selection number: