3

有没有办法在 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()
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 个步骤的图像并以与“之后”方法相同的方式嵌入图像?

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

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

我该怎么办?

4

2 回答 2

4

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

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();
        }
    }
}
于 2016-03-25T23:46:48.630 回答
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.
于 2020-02-13T12:46:57.930 回答