我的报告日志在范围报告中的不同类别中混杂在一起

时间:2018-08-24 08:40:52

标签: java selenium logging automation

我试图通过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(),“这会将商品添加到购物车”); enter image description here

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

        }

}

1 个答案:

答案 0 :(得分:1)

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

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

此外,您是否看到文档的示例部分,您已经有准系统的ExtentTestNGReportBuilder示例,可以在不创建任何自定义代码的情况下使用该示例。