如何在Selenium或Java中获取Soft Assert的屏幕截图

问题描述 投票:-2回答:3

Iam试图为使用软Assertion失败的测试用例截取屏幕截图。我在使用softAssertion时,如果某个特定步骤失败,它会在报告中显示失败的步骤,但会继续执行。所以在这种情况下我怎样才能截取屏幕截图tescase在软Assert..plz帮助失败?

java selenium selenium-webdriver
3个回答
0
投票

在executeAssert的catch块中,调用一个截取屏幕截图或在那里实现代码的方法。

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

0
投票

我有同样的问题,并通过以下方法解决。

创建自己的SoftAssert类,扩展Assertion类和TestNG的软断言类的方法。根据您的需要自定义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;
    }
}

0
投票

我正在寻找一种解决方案,在使用TestNG时获得关于软断言和硬断言的屏幕截图,我想我发现什么对我有用。通常,使用SoftAssert声明:

public static SoftAssert softAssert = new SoftAssert();

所以你可以软断言:

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

仍然很难断言看起来像:

Assert.assertEquals("String1","String1");

但是,如果你想用软件和硬件断言做截图,你必须@Override软件和硬件断言。喜欢:

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

package yourPackage;

import org.testng.asserts.Assertion;
import org.testng.asserts.IAssert;

public class CustomHardAssert extends Assertion{

        @Override
        public void onAssertFailure(IAssert<?> assertCommand, AssertionError ex) {
              Methods.takeScreenshot();
        }
}

我的Methods.takeScreenshot看起来像这样(需要当前时间并将其用作文件名):

public static void takeScreenshot() {

    String pattern = "yyyy-MM-dd HH mm ss SSS";
    SimpleDateFormat simpleDateFormat =
            new SimpleDateFormat(pattern);

    String date = simpleDateFormat.format(new Date());

    try {
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File("D:\\github\\path\\"
            + "Project\\test-output\\screenshots\\" + date + ".png"));
    }
    catch (Exception e) {
            e.printStackTrace();
        }
    }

你的基地或你声明软和硬断言的地方都应该有两个:

public static CustomSoftAssert softAssert = new CustomSoftAssert();
public static CustomHardAssert hardAssert = new CustomHardAssert();

现在你可以使用以下截图进行软断言:

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

和硬断言截图如:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.