简体   繁体   中英

Selenium webdriver Page Object Pattern and ExtentReports

Someone could tell me how to write a functional application tests which combine Selenium Page Object Pattern and ExtentsReports ( http://extentreports.relevantcodes.com/ ) to generate reports from these test cases. How to design test class? because I know that validation should be separated from page objects. What is the best approach to do this?

A sample piece of code would be very helpful

It is a good approach, of course, to separate your model (Page Objects) from you tests. For this to happen, you may use a layer of services, ie helper classes, which can interact both with business objects and page objects.

Note: I'm going to answer the second part of your question, not that on yet-another lib for reporting.

So, you have a business object:

public class Something {
    boolean toHappen;

    public Something(boolean toHappen) {
         this.toHappen = toHappen;
    }

    public boolean isToHappen() {
        return toHappen;
    }
}

You also have your page:

public class ApplicationPage {

      // how driver object is put here is your own business.
      private static WebDriver driver;

      @FindBy(id = "id")
      private Button triggerButton;

      public ApplicationPage() {
           PageFactory.initElements(driver, this);
      }

      public static ApplicationPage open(){
           driver.get("http://page.net");
           return new ApplicationPage();
      }

      public void trigger() {
           triggerButton.click();  
      }
}

So in order not to mix business objects and pages in tests, you create a service:

public class InevitableService {

     public static void makeHappen() {

          // just a very stupid code here to show interaction
          Something smth = new Something(true);

          ApplicationPage page = ApplicationPage.open();

          if(smth.toHappen()){
               page.trigger();
          }
     }
}

And finally your test

public class TestClass extends Assert {
    @Test
    public void test() {
        InevitableService.makeHappen();
        assertTrue(true);
    }
}

As a result:

  • you have no driver in tests
  • you have no page objects in tests
  • you operate only high-level logic

Pros:

  • very flexible

Cons:

  • gets complicated over time

Considering your reporting tool - I believe it just listens the result of you tests and sends them to server. Or it just takes the xml/html results of you tests and makes pretty and useless pie-charts. Again, has nothing to do with POP.

Steps:

1. Declare variables under Test Suite class
    public ExtentReports extent ;
    public ExtentTest test;

2. Create object for Extent Managers User defined class
    extent = ExtentManager.instance();
3. Pass extent parameter to the Page Object Class
    inbound = new DemoPageObject(driver,extent);
4. Goto page object class method and Start with "Start log"
    test = extent.startTest("View details", "Unable to view details");
5. For Success steps and we need end test
        test.log(LogStatus.PASS, "The list of details are successfully             displaying");
        test.log(LogStatus.INFO, test.addScreenCapture(ExtentManager.CaptureScreen(driver, "./Send")));
        log.info("The list of details are successfully displaying  ");
        extent.endTest(test);
6. For Failure and no need to end test
    test.log(LogStatus.FAIL, "A Technical error is displaying under "); 
7. Use @AfterMethod to handle error test cases

    @AfterMethod
    public void tearDown(ITestResult result) {
    if (result.getStatus() == ITestResult.FAILURE) {
      test.log(LogStatus.FAIL, "<pre>" + result.getThrowable().getMessage() + "</pre>");
      extent.endTest(test);
            }        
        }               
8. Finally Adding results to the report
    @AfterTest
    public void when_I_Close_Browser() {
    extent.flush();

}

public class ExtentManager {

public static ExtentReports instance() {
    ExtentReports extent;
    String Path = "./ExtentReport.html";
    System.out.println(Path);
    extent = new ExtentReports(Path, true);

      //extent.config() .documentTitle("Automation Report").reportName("Regression");
    extent
    .addSystemInfo("Host Name", "Anshoo")
    .addSystemInfo("Environment", "QA");

    return extent;
}

public static String CaptureScreen(WebDriver driver, String ImagesPath) {
    TakesScreenshot oScn = (TakesScreenshot) driver;
    File oScnShot = oScn.getScreenshotAs(OutputType.FILE);
    File oDest = new File(ImagesPath + ".jpg");
    try {
        FileUtils.copyFile(oScnShot, oDest);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    return ImagesPath + ".jpg";
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM