黄瓜 Java 截图

Cucumber Java screenshots

有没有办法在 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

我想对场景的某些步骤进行截图。

我目前正在用'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 个步骤的图像并以与 'After' 方法相同的方式嵌入图像?

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

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

我该怎么做?

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

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

您可以使用范围报告:

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.