Cucumber Java截图 [英] Cucumber Java screenshots

查看:168
本文介绍了Cucumber Java截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to take screenshots in between steps in Java Cucumber ?

我有以下情况:

@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

我想截取场景的某些步骤的截图。

I want to take screenshots for certain steps of the scenario.

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

I am currently taking a screenshot in the 'After' method.

@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();
}

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

As expected, this is capturing the image for the last step only.

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

How can I capture the images for the other 2 steps and embed the image the same way as in the 'After' method ?

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

I tried to create a new method to take screenshots and call that method when needed. But can any other method , other than the one specified in 'After' , take scenario as an argument ?

take_screenshot(Scenario_1, driver);

public void take_screenshot(Scenario s,WebDriver driver)
{
    final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
    s.embed(screenshot, "image/png");
}

我该如何解决这个问题?

How can I go about this ?

推荐答案

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

Create a screenshot step. Maybe something like this:

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();
        }
    }
}

这篇关于Cucumber Java截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆