首页 > 解决方案 > 将多个测试状态发送到报告时,屏幕截图未添加到范围报告 4

问题描述

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

此方法发送了多个状态,但未添加屏幕截图。图像存在于本地但未附加到报告中

此方法仅发送一个状态,因此添加了屏幕截图

在这里,我将测试和节点设置为 @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;
    }

}

在这里,我通过调用 Assertion 方法将状态发送到报告

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

    }

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

谢谢。

标签: javaseleniumselenium-webdrivertestngextentreports

解决方案


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");

推荐阅读