如何在 Selenium 或 Java 中截取 Soft Assert 的屏幕截图
How to take a screenshot for Soft Assert in Selenium or Java
我正在尝试为使用 soft 失败的测试用例截屏 Assertion.I 我正在使用 softAssertion,因为当特定步骤失败时,它会在报告中显示失败的步骤,但会继续 execution.So 这样的当 tescase 在 soft Assert 中失败时,我如何截取屏幕截图..plz help ?
在 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();
}
}
我遇到了同样的问题并通过以下方法解决了。
创建您自己的 SoftAssert class,它扩展了断言 class 和 TestNG 的软断言 class 的方法。根据您的 need.I 自定义 doAssert() 方法,我正在使用 allure 来管理屏幕截图。您可以在此处执行创建快照的步骤。
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;
}
}
我一直在寻找一种解决方案,以便在使用 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");
希望对您有所帮助,这对我来说都是全新的 :)
我正在尝试为使用 soft 失败的测试用例截屏 Assertion.I 我正在使用 softAssertion,因为当特定步骤失败时,它会在报告中显示失败的步骤,但会继续 execution.So 这样的当 tescase 在 soft Assert 中失败时,我如何截取屏幕截图..plz help ?
在 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();
}
}
我遇到了同样的问题并通过以下方法解决了。
创建您自己的 SoftAssert class,它扩展了断言 class 和 TestNG 的软断言 class 的方法。根据您的 need.I 自定义 doAssert() 方法,我正在使用 allure 来管理屏幕截图。您可以在此处执行创建快照的步骤。
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;
}
}
我一直在寻找一种解决方案,以便在使用 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");
希望对您有所帮助,这对我来说都是全新的 :)