如何在Selenium或Java中拍摄软断言的屏幕截图

如何在Selenium或Java中拍摄软断言的屏幕截图,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我正在尝试为一个使用软断言失败的测试用例截屏。我正在使用软断言,当某个特定步骤失败时,它会在报告中显示失败的步骤,但会继续执行。因此,在这种情况下,当测试用例在软断言中失败时,我如何截屏..plz help?在executeAssert调用截图方法或在那里实现代码 @Override public void executeAssert(IAssert a) { try { a.doAssert(); } catch (AssertionError ex)

我正在尝试为一个使用软断言失败的测试用例截屏。我正在使用软断言,当某个特定步骤失败时,它会在报告中显示失败的步骤,但会继续执行。因此,在这种情况下,当测试用例在软断言中失败时,我如何截屏..plz help?

executeAssert调用截图方法或在那里实现代码

@Override
    public void executeAssert(IAssert a) {
    try {
        a.doAssert();
    } catch (AssertionError ex) {
        onAssertFailure(a, ex);
        takeScreenshot();
        m_errors.put(ex, a);
    }
    }

    private void takeScreenshot() {
    WebDriver augmentedDriver = new Augmenter().augment(driver);
    try {
        if (driver != null
            && ((RemoteWebDriver) driver).getSessionId() != null) {
        File scrFile = ((TakesScreenshot) augmentedDriver)
            .getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File(("./test-output/archive/"
            + "screenshots/" + "_" + ".png")));
        }       
    } catch (Exception e) {
        e.printStackTrace();
    }
    }

我有同样的问题,并解决了以下方法

创建您自己的SoftAssert类,该类扩展了断言类和TestNG的SoftAssertion类的方法。根据需要定制doAssert()方法。我正在使用诱惑来管理屏幕截图。您可以在此处执行创建快照的步骤

import java.util.Map;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.asserts.Assertion;
import org.testng.asserts.IAssert;
import org.testng.collections.Maps;

import io.qameta.allure.Attachment;
import io.qameta.allure.Step;

/**
 * When an assertion fails, don't throw an exception but record the failure.
 * Calling {@code assertAll()} will cause an exception to be thrown if at least
 * one assertion failed.
 */
public class SoftAssert extends Assertion {
    // LinkedHashMap to preserve the order
    private final Map<AssertionError, IAssert<?>> m_errors = Maps.newLinkedHashMap();
    private String assertMessage = null;

    @Override
    protected void doAssert(IAssert<?> a) {
        onBeforeAssert(a);
        try {
            assertMessage = a.getMessage();
            a.doAssert();
            onAssertSuccess(a);
        } catch (AssertionError ex) {
            onAssertFailure(a, ex);
            m_errors.put(ex, a);
            saveScreenshot(assertMessage);
        } finally {
            onAfterAssert(a);
        }
    }

    public void assertAll() {
        if (!m_errors.isEmpty()) {
            StringBuilder sb = new StringBuilder("The following asserts failed:");
            boolean first = true;
            for (Map.Entry<AssertionError, IAssert<?>> ae : m_errors.entrySet()) {
                if (first) {
                    first = false;
                } else {
                    sb.append(",");
                }
                sb.append("\n\t");
                sb.append(ae.getKey().getMessage());
            }
            throw new AssertionError(sb.toString());
        }
    }

    @Step("Validation fail: {assertMessage}")
    @Attachment(value = "Page screenshot", type = "image/png")
    public byte[] saveScreenshot(String assertMessage) {
        byte[] screenshot = null;
        screenshot = ((TakesScreenshot) TestBase.driver).getScreenshotAs(OutputType.BYTES);
        return screenshot;
    }
}
import java.util.Map;
导入org.openqa.selenium.OutputType;
导入org.openqa.selenium.TakesScreenshot;
导入org.testng.asserts.Assertion;
导入org.testng.asserts.IAssert;
导入org.testng.collections.Maps;
导入io.qameta.allure.Attachment;
导入io.qameta.allure.Step;
/**
*当断言失败时,不要抛出异常,而是记录失败。
*调用{@code assertAll()}将导致引发异常,如果至少
*一个断言失败。
*/
公共类SoftAssert扩展了断言{
//LinkedHashMap以保留顺序

private final Map在使用TestNG时,我正在寻找一种解决方案,以获取软断言和硬断言的屏幕截图,我想我找到了适合我的解决方案。通常,使用SoftAssert,您声明:

public static SoftAssert softAssert = new SoftAssert();
所以你可以这样做:

softAssert.assertEquals("String1","String1");
softAssert.assertAll();
仍然很难断言如下所示:

Assert.assertEquals("String1","String1");
但是,如果您想同时使用软资产和硬资产进行屏幕截图,则必须分别@Override soft和hard asserts。例如:

package yourPackage;

import org.testng.asserts.IAssert;
import org.testng.asserts.SoftAssert;

public class CustomSoftAssert extends SoftAssert {

    @Override
    public void onAssertFailure(IAssert<?> a, AssertionError ex) {
          Methods.takeScreenshot();
    }
}
您的基地或您声明软资产和硬资产的任何地方都应同时拥有这两种资产:

public static CustomSoftAssert softAssert = new CustomSoftAssert();
public static CustomHardAssert hardAssert = new CustomHardAssert();
现在,您可以使用如下屏幕截图进行软断言:

softAssert.assertEquals("String1","String1");
softAssert.assertAll();
hardAssert.assertEquals("String1","String1");
和硬断言屏幕截图,如:

softAssert.assertEquals("String1","String1");
softAssert.assertAll();
hardAssert.assertEquals("String1","String1");

我希望这能有所帮助,这对我来说是全新的:)

你已经尝试过一些代码了吗?你自己尝试一下,当你被绊倒的时候,过来问一些help@Ethaan..thanks谢谢你的评论