Iam尝试为使用软Assertion失败的测试用例截取屏幕截图。我在使用softAssertion时,如果某个特定步骤失败,则显示报告中的失败步骤但继续执行。所以在这种情况下如何才能获取在软Assert..plz帮助中,每当测试用例失败时,屏幕截图?
答案 0 :(得分: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();
}
}
答案 1 :(得分:0)
我有同样的问题,并通过以下方法解决。
创建自己的SoftAssert类,扩展断言类和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;
}
}
答案 2 :(得分: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();
}
}
您的Base或您声明软断言和硬断言的任何地方都应同时具有两者:
public static CustomSoftAssert softAssert = new CustomSoftAssert();
public static CustomHardAssert hardAssert = new CustomHardAssert();
现在,您可以使用以下屏幕截图进行软断言:
softAssert.assertEquals("String1","String1");
softAssert.assertAll();
并使用以下屏幕截图进行硬断言:
hardAssert.assertEquals("String1","String1");
我希望对您有所帮助,这对我来说是新的:)