gpt4 book ai didi

java - 当多个测试状态发送到报告时,屏幕截图未添加到范围报告 4

转载 作者:太空宇宙 更新时间:2023-11-04 09:13:17 25 4
gpt4 key购买 nike

我为@Test设置了父测试,为@Methods设置了子节点,当我发送多个测试状态(通过或失败)来报告测试失败时不会添加屏幕截图,但仅当为该@method发送一个状态时才会添加屏幕截图。

This method has multiple statuses sent but screenshots are not added. Image is present in local but not appended to report

This method has only one status sent so the screenshot is added

这里我将测试和节点设置为@Test和@method

package modules;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;

import com.aventstack.extentreports.ExtentTest;

import io.github.bonigarcia.wdm.WebDriverManager;

public class openBrowser implements auto_constant {
public static WebDriver driver;
public static String extBrowser;
public static ThreadLocal<ExtentTest> parentTest = new ThreadLocal<ExtentTest>();
public static ThreadLocal<ExtentTest> test = new ThreadLocal<ExtentTest>();
// static String screenShotPath = screenPath + "/screenshot " + dateFunc.getShotDate();

@BeforeSuite
public void setupExtent() {
extentReports.attRepo();
}

@BeforeMethod
public void setChildTests() {
ExtentTest child = parentTest.get().createNode(extBrowser);
test.set(child);
}

@BeforeTest(description = "Checking the browser and launching it")
@Parameters({ "browser" })
public void beforeTest(String browser) {

/*
* Setting every @Test as parent node
* Name will be the class name of that @Test
*/
ExtentTest parent = extentReports.extent.createTest(getClass().getName());
parentTest.set(parent);

/*
* This assigns the browser driver to use for the extent reports for setting child node
*/
openBrowser.extBrowser = browser;

if (browser.equalsIgnoreCase("Chrome")) {
WebDriverManager.chromedriver().arch64().setup();
if (Property.getProperty("head").equalsIgnoreCase("false")) {
setDriver(new ChromeDriver());
} else {
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
setDriver(new ChromeDriver(options));
}
} else if (browser.equalsIgnoreCase("Firefox")) {
WebDriverManager.firefoxdriver().arch64().setup();
if (Property.getProperty("head").equalsIgnoreCase("false")) {
setDriver(new FirefoxDriver());
} else {
FirefoxOptions options = new FirefoxOptions();
options.addArguments("--headless");
setDriver(new FirefoxDriver(options));
}
}
driver.manage().window().maximize();
driver.get(url);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}

@AfterTest(description = "Terminating the browser instance and reports")
public void afterTest() {
if (Property.getProperty("extent").equalsIgnoreCase("on")) {
extentReports.extent.flush();
}
driver.close();
}

@AfterMethod
public void afterMethod(ITestResult result) {
if (Property.getProperty("extent").equalsIgnoreCase("on")) {
if (result.getStatus() == ITestResult.FAILURE)
test.get().fail(result.getThrowable());
else if (result.getStatus() == ITestResult.SKIP)
test.get().skip(result.getThrowable());
else
test.get().pass("Test passed");
}
}

public void setDriver(WebDriver driver) {
openBrowser.driver = driver;
// driver = drive;
}

}

这里我通过调用断言方法将状态发送到报告

package pageModels;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.aventstack.extentreports.ExtentTest;

import modules.Assertion;
import modules.Property;
import modules.excelUtils;
import modules.extentReports;
import modules.openBrowser;

public class signup_login_Page extends openBrowser {
ExtentTest extTest;

/*
* Login page Web Elements
*/
@FindBy(xpath = "//div[@class='block-content']/form/fieldset/div[4]/div/button/span")
private WebElement sign_Submit;
@FindBy(xpath = "//div[@id='email-error']")
private WebElement email_error;
@FindBy(xpath = "//div[@id='pass-error']")
private WebElement pass_error;
@FindBy(xpath = "//div[@class='panel header'] //li[@class='greet welcome']/span")
private WebElement userName;
@FindBy(xpath = "//input[@id='email']")
private WebElement emailBox;
@FindBy(xpath = "(//input[@id='pass'])[1]")
private WebElement passBox;
@FindBy(xpath = "//div[@class='messages']/div/div")
private WebElement emptyLogErr;

/*
* Constructor Sets the driver to Current page
*/
public signup_login_Page(WebDriver driver) {
PageFactory.initElements(driver, this);
}

/*
* Checks if error messages are displayed when fields are empty
*/
public void checkError() throws Exception {
extTest = extentReports.extentTest();
sign_Submit.click();
modules.wait.waitVisible(email_error);

// Comparing Error Messages
Assertion.assertEquals(email_error.getText(), excelUtils.getData(Property.getProperty("sheetName"), 2, 6),
extTest, "Proper error msg for 'Email Field' is displayed",
"Proper error msg for 'Email Field' is NOT displayed");
Assertion.assertEquals(pass_error.getText(), excelUtils.getData(Property.getProperty("sheetName"), 2, 6),
extTest, "Proper error msg for 'Password Field' is displayed",
"Proper error msg for 'Password Field' is NOT displayed");

}

/*
* Login with valid credentials Checks the header for username to verify
* Successful login
*/
public void signin() throws Exception {
extTest = extentReports.extentTest();

int cell = 4;
for (int i = 1; i <= 2; i++) {
driver.findElement(By.xpath("(//form[@class='form form-login']/fieldset/div/div)[" + i + "]/input"))
.sendKeys(excelUtils.getData("userData", 2, cell++));
}
// actions.moveClick(sign_Submit);
sign_Submit.click();
Thread.sleep(10000);
// modules.wait.fluentVisible(userName);
String fullName = excelUtils.getData(Property.getProperty("sheetName"), 2, 2) + " "
+ excelUtils.getData(Property.getProperty("sheetName"), 2, 3);
String[] acName = userName.getText().split(",");
Assertion.assertContains(acName[1].trim().substring(0, acName[0].trim().length() - 1), fullName, extTest,
"Logged in Successfully", "User could NOT login");
}

}

在这里,我将在断言后发送状态报告以及屏幕截图

package modules;

import java.io.IOException;

import org.testng.Assert;
import org.testng.IReporter;

import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.Status;

public class Assertion implements IReporter {

/*
* Asserting the two Strings The result is recorded in Report if reports are
* enabled
*/
public static void assertEquals(String actual, String expected, ExtentTest extTest, String passMsg,
String failMsg) {
try {
Assert.assertEquals(actual, expected);
if (extentReports.xclude(extTest)) {
extTest.log(Status.PASS, passMsg);
}
System.out.println(passMsg);

/*
* Handling Assertion Error
*/
} catch (AssertionError error) {
if (extentReports.xclude(extTest)) {
/*
* Adding the Screen capture to the ExtentReports and handling it if file not
* found
*/
try {
extTest.fail(failMsg,
MediaEntityBuilder.createScreenCaptureFromPath(screenshot.shot(failMsg)).build());
} catch (IOException e) {
System.out.println("Could NOT find the Screenshot");
}
}
System.out.println(failMsg);
}
}

使用下面的方法为每个方法创建子节点

/*
* Creates a new Extent test and returns the extentTest object
*/
public static ExtentTest extentTest() {

if(Property.getProperty("extent").equalsIgnoreCase("on")) {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
ExtentTest child = parentTest.get().createNode(stackTrace[2].getMethodName());
return child;
}else {
return null;
}

}

如果这个问题得到解决,将会有很大帮助。我也没有收到任何需要调试的错误。

谢谢。

最佳答案

Assertion.assertEquals(email_error.getText(), excelUtils.getData(Property.getProperty("sheetName"), 2, 6),
extTest, "Proper error msg for 'Email Field' is displayed",
"Proper error msg for 'Email Field' is NOT displayed");
Assertion.assertEquals(pass_error.getText(), excelUtils.getData(Property.getProperty("sheetName"), 2, 6),
extTest, "Proper error msg for 'Password Field' is displayed",
"Proper error msg for 'Password Field' is NOT displayed");

图像未上传,因为在删除图像上传的失败消息中的单引号后,我发送了单引号中包含文本的失败消息。

我不知道为什么,但它有效。

Assertion.assertEquals(email_error.getText(), excelUtils.getData(Property.getProperty("sheetName"), 2, 6),
extTest, "Proper error msg for Email Field is displayed",
"Proper error msg for Email Field is NOT displayed");
Assertion.assertEquals(pass_error.getText(), excelUtils.getData(Property.getProperty("sheetName"), 2, 6),
extTest, "Proper error msg for Password Field is displayed",
"Proper error msg for Password Field is NOT displayed");

关于java - 当多个测试状态发送到报告时,屏幕截图未添加到范围报告 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59422749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com