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

标签 java selenium selenium-webdriver testng extentreports

我为@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/

相关文章:

java - 未找到 key 保护算法 : java. security.KeyStoreException:证书链未验证

java - OSGi和依赖注入(inject)有什么关系

python - 如何使用 Selenium 和 Python 3.7.4 从一系列复选框中找到特定复选框

python - 检查元素在 Selenium 中是否可点击

Selenium - 元素不再附加到 DOM - Python

java - 如何在 Spring MVC 中发送 HTML 电子邮件?

java - EL表达式解析整型为long

javascript - 在 Firefox 中运行 Protractor

python - 'WebDriver' 对象没有属性 'find_element_by_link_text' - Selenium 脚本突然停止工作

java - 如何使用 Selenium - Junit 测试运行程序文件和 cucumber 选项为每次运行创建测试结果文件夹?