【问题标题】:Selenium webdriver Page Object Pattern and ExtentReportsSelenium webdriver 页面对象模式和 ExtentReports
【发布时间】:2015-07-21 12:37:14
【问题描述】:

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

一段示例代码会很有帮助

【问题讨论】:

  • JFYI,页面对象模式与您的报告无关。您如何创建它们是测试、侦听器等任何东西的工作,但不是页面对象,它们仅代表您的应用程序的模型。

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


【解决方案1】:

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

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

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

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 无关。

【讨论】:

  • 感谢您的回复。我会尝试使用这个。我写过关于 ExtentsReports 的文章,但这篇文章的主要原因是了解设计测试类的最佳方法,并将它们与一些报告工具一起使用
【解决方案2】:

步骤:

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

}

公共类 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";
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-12
    • 2017-08-09
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多