首页 > 解决方案 > 我的报告日志在范围报告中因不同类别而混杂在一起

问题描述

我试图通过 testNG 并行运行不同的类来为所有类生成一个单一的范围报告。此外,我正在尝试为每个类生成日志,这些日志将显示在范围报告中。但是,当生成范围报告时,我可以看到一个类的测试用例的日志显示在另一个类的测试用例中。如何解决这个问题?下面是我的代码:

//// 这是我的范围报告的基类:

 @BeforeSuite
    public static void setUp()
    {
        htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/MyOwnReport.html");
        report = new ExtentReports();
        report.attachReporter(htmlReporter);
        report.setSystemInfo("OS", "Mac Sierra");
        report.setSystemInfo("Host Name", "Testing Xpert");
        report.setSystemInfo("Environment", "QA");
        report.setSystemInfo("User Name", "Vivek");
        htmlReporter.config().setChartVisibilityOnOpen(true);
        htmlReporter.config().setDocumentTitle("AutomationTesting.in Demo Report");
        htmlReporter.config().setReportName("My Own Report");
        htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
        htmlReporter.config().setTheme(Theme.DARK);

    }

  //Creating a method getScreenshot and passing two parameters 
  //driver and screenshotName




     public static String getScreenshot(WebDriver driver, String screenshotName) throws Exception {
                      //below line is just to append the date format with the screenshot name to avoid duplicate names      
                      String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
            TakesScreenshot ts = (TakesScreenshot) driver;
            File source = ts.getScreenshotAs(OutputType.FILE);

            String destination = System.getProperty("user.dir") + "/test-output/"+screenshotName+dateName+".png";
            File finalDestination = new File(destination);
            FileHandler.copy(source, finalDestination);
            //FileUtils.copyFile(source, finalDestination);

            return destination;
      }

        @AfterMethod
        public void getResult(ITestResult result) throws Exception
        {


            if(result.getStatus() == ITestResult.FAILURE)
            {
                 test.log(Status.FAIL, "Test Case Failed is "+result.getName());
            //   test.log(Status.FAIL, "Test Case Failed is "+result.getThrowable());
                 String screenshotPath = ExtentReportBaseClass.getScreenshot(UtilityMethods.getdriver(), result.getName());
                 test.log(Status.FAIL, (Markup) test.addScreenCaptureFromPath(screenshotPath));
                //test.log(Status.FAIL, MarkupHelper.createLabel(result.getName()+" Test case FAILED due to below issues:",test.addScreenCaptureFromPath(screenshotPath)));

                test.fail(result.getThrowable());
            }
            else if(result.getStatus() == ITestResult.SUCCESS)
            {
                test.log(Status.PASS, "Test Case Passed is "+result.getName());

            }
            else
            {
                test.log(Status.SKIP, MarkupHelper.createLabel(result.getName()+" Test Case SKIPPED", ExtentColor.ORANGE));
                test.skip(result.getThrowable());
            }
        }

        @AfterSuite
        public void tearDown()
        {

            report.flush();
        }

我正在运行我的两个课程。生成日志的方法在类中定义。(test.log(test.getStatus(), "This will add item to the cart"); 在此处输入图像描述)

public class PurchaseItemTestCase extends ExtentReportBaseClass{
    @BeforeClass
        public void launchBrowser() throws InterruptedException {

            //System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
            //driver = new ChromeDriver();
            driver = util.openBrowser(ConstantsValues.BROWSER_NAME);

            util.launchWebsite();
            driver.manage().window().maximize();
            //report = ExtentReportBaseClass.setUp();

        }

        @Test

        public void chkPurchaseItem() throws InterruptedException {
            //ExtentTest test;
            test = report.createTest("chkPurchaseItem", "This will check status for purchasing an item.");
            driver.findElement(By.xpath("//img[@title='Faded Short Sleeve T-shirts']")).click();

            driver.switchTo().defaultContent();
            driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@id,'fancy')]")));

            System.out.println("after frame");
            Thread.sleep(4000);
            test.log(test.getStatus(), "This will add item to the cart");
            driver.findElement(By.xpath("//button[@type='submit']//span[contains(.,'Add')]")).click();
            Set handles = driver.getWindowHandles();

            System.out.println(handles);

            // Pass a window handle to the other window

            for (String handle1 : driver.getWindowHandles()) {

                System.out.println(handle1);

                driver.switchTo().window(handle1);

                Thread.sleep(3000);
                driver.findElement(By.xpath("//a[@title='Proceed to checkout']//span[contains(.,'Proceed')]")).click();
                Thread.sleep(3000);
                driver.findElement(By.xpath(
                        "//a[@class='button btn btn-default standard-checkout button-medium']//span[contains(.,'Proceed')]//i[@class='icon-chevron-right right']"))
                        .click();
                test.log(test.getStatus(), "We will login to the account");
                driver.findElement(By.id("email")).sendKeys("vivekkumar009@gmail.com");
                driver.findElement(By.id("passwd")).sendKeys("vivek123");
                Thread.sleep(3000);
                // UtilityMethods.getdriver().findElement(By.id("SubmitLogin"));
                driver.findElement(By.id("SubmitLogin")).click();
                test.log(test.getStatus(), "User will add address for shipment");
                driver.findElement(By.name("processAddress")).click();
                test.log(test.getStatus(), "User will agree to terms and condition ");
                driver.findElement(By.id("cgv")).click();
                driver.findElement(By.xpath("//button[@name=\"processCarrier\"]")).click();
                driver.findElement(By.className("cheque")).click();
                test.log(test.getStatus(), "User confirms order");
                driver.findElement(By.xpath("//span[contains(text(),'I confirm my order')]")).click();
                boolean chkElement = util.getdriver().findElement(By.className("home")).isDisplayed();
                System.out.println(chkElement);
                Assert.assertTrue(chkElement);

            }

这是第二类:

public class CategoryWisePurchase extends ExtentReportBaseClass {   

    @BeforeClass
        public void launchApplicationBrowser() {

            util.openBrowser(ConstantsValues.BROWSER_NAME);
            util.launchWebsite();

        }

        @Test

        public void CategoryWiseShopping() throws InterruptedException {

            test = report.createTest("Category Wise Shopping", "This will check if the user is able to shop different products by selecting its category");
            // Actions actions = new Actions(UtilityMethods.getdriver());
            Actions action = new Actions(UtilityMethods.getdriver());
            test.log(test.getStatus(), "User will select the category of a particular item");
            WebElement mainMenu = UtilityMethods.getdriver().findElement(By.xpath("//a[@title='Women']"));
            WebElement subMenu = UtilityMethods.getdriver().findElement(By.xpath("//a[@title='T-shirts']"));

            action.moveToElement(mainMenu).build().perform();
            Thread.sleep(2000);

            action.moveToElement(subMenu).click().build().perform();
            test.log(test.getStatus(), "Various products will be shown to the user of the selected category");
            //reportLog("Various products will be shown to the user of the selected category");
            boolean chkElement = UtilityMethods.getdriver().findElement(By.xpath("//form[@method='post']")).isDisplayed();
            System.out.println(chkElement);
            Assert.assertTrue(chkElement);

        }

}

标签: javaseleniumloggingautomation

解决方案


如果测试在您的 Base 类中,就会发生这种情况。同时,所有继承您的 Base 类的类都将使用该测试。最好在这里使用 threadlocal 或类似的实现:

https://github.com/anshooarora/extentreports-java/blob/master/src/test/java/com/aventstack/extentreports/common/ExtentTestManager.java

此外,您是否看到文档的示例部分,您已经有了一个准系统 ExtentTestNGReportBuilder 示例,您可以使用它而无需像这里创建任何自定义代码。


推荐阅读