Cucumber Java截图

时间:2016-03-25 15:55:06

标签: java junit cucumber screenshot cucumber-java

有没有办法在Java Cucumber中的两个步骤之间截取屏幕截图?

我有以下情况:

export PYTHONPATH=/Library/Frameworks/SQLite3.framework/Versions/3/Python:/Library/Python/2.7/site-packages/numpy-override:/Library/Frameworks/GDAL.framework/Versions/1.7/Python/site-packages:/Library/Frameworks/cairo.framework/Versions/1/Python:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python26.zip:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python22.7.6/lib-tk:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload:/Library/Python/2.7/site-packages:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC:/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/wx-2.8-mac-unicode:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:/Users/Sasha/PyQt-gpl-5.5.1:/Users/Sasha/Library/Python/2.7/lib/python/PyQt-gpl-5.5.1
# Setting PATH for Python 3.5
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
alias python="python3"

我想为该方案的某些步骤截取屏幕截图。

我目前正在使用'After'方法截取屏幕截图。

@Scenario_1
Given I log into url
And I see the home page is displayed in English //Take screenshot
And I click on 'Edit Profile'
And I see the language set to 'English' 
When I change the language to Chinese   //Take screenshot
And I navigate to home page
Then everything is displayed in Chinese //Take screenshot

正如预期的那样,这只是为最后一步拍摄图像。

如何捕获其他2个步骤的图像,并以与“After”方法相同的方式嵌入图像?

我尝试创建一种新方法来截取屏幕截图并在需要时调用该方法。但除了'After'中指定的方法之外,任何其他方法都可以将场景作为参数吗?

@After()
public void execute_after_every_scenario(Scenario s) throws InterruptedException
{
    Thread.sleep(2000);

    final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    s.embed(screenshot, "image/png");

    driver.quit();
}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

创建截屏步骤。也许是这样的:

public class YourStepDefinitions {

    private Scenario myScenario;

    @Before()
    public void embedScreenshotStep(Scenario scenario) {

        myScenario = scenario;

    }

    @Then ("^I take a screenshot$")
    public void i_take_a_screenshot() throws Throwable {

        try {
            myScenario.write("Current Page URL is " + driver.getCurrentUrl());
            byte[] screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
            myScenario.embed(screenshot, "image/png");  // Stick it in the report
        } catch (WebDriverException somePlatformsDontSupportScreenshots) {
            log.error(somePlatformsDontSupportScreenshots.getMessage());
        } catch (ClassCastException cce) {
            cce.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

您可以使用范围报告:

Extent reports are more customizable than others.
Extent API can produce more interactive reports, a dashboard view, graphical view,         
capture screenshots at every test step, and emailable reports.
It can be easily integrated with frameworks like JUnit & TestNG.