Selenium webdriver页面对象模式和ExtentReports

时间:2015-07-21 12:37:14

标签: java selenium-webdriver automated-tests functional-testing pageobjects

有人可以告诉我如何编写功能性应用程序测试,它结合了Selenium Page Object Pattern和ExtentsReports(http://extentreports.relevantcodes.com/)来生成这些测试用例的报告。如何设计测试类?因为我知道验证应该与页面对象分开。这样做的最佳方法是什么?

一段代码示例非常有用

2 个答案:

答案 0 :(得分:2)

当然,将模型(页面对象)与测试分开是一种很好的方法。为此,您可以使用一层服务,即辅助类,它们可以与业务对象和页面对象进行交互。

注意:我将回答您问题的第二部分,而不是另一部分用于报告。

所以,你有一个业务对象:

public class Something {
    boolean toHappen;

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

    public boolean isToHappen() {
        return toHappen;
    }
}

您还拥有自己的页面:

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

因此,为了不在测试中混合业务对象和页面,您需要创建一个服务:

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

最后你的测试

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

结果:

  • 你没有参加考试的司机
  • 测试中没有页面对象
  • 您只操作高级逻辑

优点:

  • 非常灵活

缺点:

  • 随着时间的推移变得复杂

考虑您的报告工具 - 我相信它只是监听您测试的结果并将它们发送到服务器。或者它只需要你测试的xml / html结果并制作漂亮且无用的饼图。再次,与POP无关。

答案 1 :(得分:1)

步骤:

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

}