首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium webdriver页面对象模式和ExtentReports

Selenium webdriver页面对象模式和ExtentReports
EN

Stack Overflow用户
提问于 2015-07-21 20:37:14
回答 2查看 1.6K关注 0票数 2

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

一段示例代码将非常有用。

EN

回答 2

Stack Overflow用户

发布于 2015-07-24 14:56:16

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

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

因此,您有一个业务对象:

代码语言:javascript
复制
public class Something {
    boolean toHappen;

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

    public boolean isToHappen() {
        return toHappen;
    }
}

你也有你的页面:

代码语言:javascript
复制
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();  
      }
}

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

代码语言:javascript
复制
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();
          }
     }
}

最后是你的测试

代码语言:javascript
复制
public class TestClass extends Assert {
    @Test
    public void test() {
        InevitableService.makeHappen();
        assertTrue(true);
    }
}

结果是:

  • 您在测试中没有驱动程序
  • 您在测试中没有页面对象
  • 您只操作高级逻辑

优点:

  • very flexible

缺点:

随着时间的推移,

  • 变得复杂

考虑到你的报告工具-我相信它只是监听你的测试结果,并将它们发送到服务器。或者,它只是获取测试的xml/html结果,然后生成漂亮而无用的饼状图。再说一次,和流行音乐没有任何关系。

票数 2
EN

Stack Overflow用户

发布于 2016-01-13 20:08:08

步骤:

代码语言:javascript
复制
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 {

代码语言:javascript
复制
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";
}

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31539242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档