2

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

1
  • JFYI, page object pattern has nothing to do with your reports. How you create them is a work for tests, listeners, anything, but not the page objects, which just represent model of your application. Commented Jul 23, 2015 at 21:11

2 Answers 2

2

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, i.e. 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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your reply. I will try to use this. I wrote about ExtentsReports but primary reason of this post was to know best approach to design test classes and using them with some reporting tool
1

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.