【问题标题】:LeanFT & Selenium custom automation framework - no HTML report generatedLeanFT 和 Selenium 自定义自动化框架 - 不生成 HTML 报告
【发布时间】:2018-01-11 17:08:34
【问题描述】:

我一直在尝试使 LeanFT html 报告与我的 Selenium/Junit 框架一起工作,但到目前为止还没有任何乐趣。 我在不同的论坛上多次搜索过这个话题,包括。惠普官方资料并尝试了我能找到的所有设置方法。 使用自定义 Selenium/LeanFT 框架时仍会生成 XML 运行结果文件。

我使用 JUnit 作为我的框架并使用 Selenium 作为 SDK 创建了一个 LeanFT 测试项目项目。我还添加了相关的硒罐。

LeanFT 版本为 14.0.2816.0。 我意识到这个问题:https://community.softwaregrp.com/t5/UFT-Practitioners-Forum/HTML-report-not-generated-in-custom-framework-using-LeanFT/td-p/1614027.

如果有人能解释我在哪里犯了错误,或者软件版本是这里的问题,我将不胜感激。非常感谢任何帮助。

实际设置包含更多抽象,通常更复杂并作为 jar 文件运行,但出于本主题的目的,我已经简化了代码,因为两种设置的结果是相同的 - runresults.xml 和没有 html :

  • 测试类:

    import com.hp.lft.report.CaptureLevel;
    import com.hp.lft.report.ModifiableReportConfiguration;
    import com.hp.lft.report.Reporter;
    import com.hp.lft.sdk.ModifiableSDKConfiguration;
    import com.hp.lft.sdk.SDK;
    import com.hp.lft.sdk.web.Browser;
    import com.hp.lft.sdk.web.BrowserDescription;
    import com.hp.lft.sdk.web.BrowserFactory;
    import com.hp.lft.sdk.web.BrowserType;
    import core.Selenium;
    import org.junit.After;
    import org.junit.AfterClass;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.*;
    import java.io.File;
    import java.net.URI;
    
    
    public class SeleniumTest extends Selenium {
    
        WebDriver driver;
        Browser browser;
    
        public SeleniumTest() {
            //Change this constructor to private if you supply your own public constructor
        }
    
    
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
    
            instance = new SeleniumTest();
            globalSetup(SeleniumTest.class);
    
    
        }
    
        @AfterClass
        public static void tearDownAfterClass() throws Exception {
            globalTearDown();
    
        }
    
        @Before
        public void setUp() throws Exception {
            ModifiableSDKConfiguration sdkConfig = new ModifiableSDKConfiguration();
            sdkConfig.setServerAddress(new URI("ws://localhost:5095"));
            SDK.init(sdkConfig);
            ModifiableReportConfiguration reportConfig = new ModifiableReportConfiguration();
            reportConfig.setOverrideExisting(true);
            reportConfig.setTargetDirectory("C:\\Users\\user\\IdeaProjects\\LeanFT_Selenium\\RunResults");
            reportConfig.setReportFolder("LastRun");
            reportConfig.setTitle("Summary");
            reportConfig.setDescription("Description");
            reportConfig.setSnapshotsLevel(CaptureLevel.All);
    
            Reporter.init(reportConfig);
    
            ChromeOptions options = new ChromeOptions();
            options.addExtensions(new File
                    ("C:\\Program Files (x86)\\HP\\Unified Functional Testing\\Installations\\Chrome\\Agent.crx"));
            System.setProperty("webdriver.chrome.driver",
                    "C:\\HP_Reporting\\Webdriver\\chromedriver.exe");
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
            driver = new ChromeDriver(options);
            browser = BrowserFactory.attach(new BrowserDescription.Builder()
                    .type(BrowserType.CHROME).build());
        }
    
        @After
        public void tearDown() throws Exception {
    
            driver.quit();
            browser = null;
    
            Reporter.generateReport();
            SDK.cleanup();
        }
    
        @Test
        public void test() throws Exception {
    
            driver.get("https://www.google.co.uk/");
    
        }
    }
    
  • 硒类:

    import com.hp.lft.report.Status;
    import com.hp.lft.unittesting.UnitTestBase;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.ClassRule;
    import org.junit.Rule;
    import org.junit.rules.TestWatcher;
    
    public class Selenium  extends UnitTestBase{
    
        protected static Selenium instance;
    
        public static void globalSetup(Class<? extends Selenium> testClass) throws Exception {
            if (instance == null)
                instance = testClass.newInstance();
            instance.classSetup();
        }
    
        @Before
        public void beforeTest() throws Exception {
            testSetup();
        }
    
        @After
        public void afterTest() throws Exception {
            testTearDown();
        }
    
        public static void globalTearDown() throws Exception {
            instance.classTearDown();
            getReporter().generateReport();
        }
    
        @ClassRule
        public static TestWatcher classWatcher = new TestWatcher() {
            @Override
            protected void starting(org.junit.runner.Description description) {
                className = description.getClassName();
            }
        };
    
        @Rule
        public TestWatcher watcher = new TestWatcher() {
            @Override
            protected void starting(org.junit.runner.Description description) {
                testName = description.getMethodName();
            }
            @Override
            protected void failed(Throwable e, org.junit.runner.Description description) {
                setStatus(Status.Failed);
            }
    
            @Override
            protected void succeeded(org.junit.runner.Description description) {
                setStatus(Status.Passed);
            }
        };
    
        @Override
        protected String getTestName() {
            return testName;
        }
    
        @Override
        protected String getClassName() {
            return className;
        }
    
        protected static String className;
        protected String testName;
    
    
    
    }
    

Libraries

【问题讨论】:

  • (1) UnitTestBase 已经完成了 SDKReporter 的初始化,所以在测试类中也这样做,你就做了两次。如果您不想在 Selenium 类中执行此操作,只需删除 extends UnitTestBase 位。 (2) 你说你知道在 14.00 报告不是使用自定义框架生成的(意味着你自己初始化并生成它)但是你在这里,做同样的事情。
  • 感谢您的意见。是的,我知道这一点,但我认为也许有办法解决它,或者人们有不同的经历。你自己初始化和生成html是什么意思?而且我相信如果不使用本机 LeanFT sdk,则必须引入可修改的 sdk 和 init,所以我认为 Selenium 中的 sdk 和报告器是为了引入新的可修改的 sdk 和可修改的报告器,而不是复制 UnitTestBase。跨度>
  • 如果明天还没有人回答,我会亲自回复一个全面的答案。
  • FWIW,我们已经尽可能多地放弃了leaft 框架,以便我们拥有更大的灵活性。虽然这对于像我这样的新测试项目很有用,但对于已经存在一段时间的项目可能不可行。在我们的例子中,我们使用了 TestNG 测试报告(而不是 JUnit)并且我们得到了可以接受的结果。祝你好运!

标签: java selenium leanft


【解决方案1】:

TL;DR

该报告仅在 14.00 通过扩展 UnitTestBase 生成。要使用自定义自动化框架生成报告,您必须至少升级到 LeanFT 14.01(尽管撰写本文时的最新版本是 14.02)。

哦,只是为了澄清一下:这个问题与 Selenium 无关。例如,在使用 NUnit 3 的 C# LeanFT SDK 中,您会有相同的行为。

详情

在这种情况下,什么是自定义自动化框架?

当我说自定义自动化框架时,我指的是任何可以自行执行 SDKReporter 初始化和清理的框架。

LeanFT 通过其引擎发挥所有作用。要告诉自定义框架连接到引擎,您需要执行以下步骤:

  • 在测试套件的开头:

    SDK.init();
    Reporter.init(); // This is not for the engine, but for the HTML report generation
    
  • 在测试套件结束时

    Reporter.generateReport();
    SDK.cleanup();
    

当您使用内置模板时,您会注意到自动创建了一个 UnitTestClassBase 类,该类从 UnitTestBase 扩展而来,并且您的 LeanFtTest 类从该 UnitTestClassBase 扩展而来。

通过这样做,您正在以一种非常具体的方式初始化SDKReporter,这就是通过这种初始化生成HTML 报告的原因。如果您要在 custom 框架中重现这种特定方式,您还将生成 HTML 报告。如果你好奇,可以查看UnitTestBase.class 文件的内容,看看它做了什么。

您正在从 UnitTestBase 扩展。为什么它不起作用?

正如我的 cmets 中所提到的,您实际上是在进行两次 SDKReporter 初始化,一次是通过扩展 UnitTestBase(如上所述,它已经进行了初始化),第二次是在您的setUp 方法。

由于您使用的是 LeanFT 14.00,您应该让 UnitTestBase 进行初始化、清理和报告生成。这意味着您的测试类文件应更改为不再包含SDKReporter 的init,如下所示:

import com.hp.lft.report.CaptureLevel;
import com.hp.lft.report.ModifiableReportConfiguration;
import com.hp.lft.report.Reporter;
import com.hp.lft.sdk.ModifiableSDKConfiguration;
import com.hp.lft.sdk.SDK;
import com.hp.lft.sdk.web.Browser;
import com.hp.lft.sdk.web.BrowserDescription;
import com.hp.lft.sdk.web.BrowserFactory;
import com.hp.lft.sdk.web.BrowserType;
import core.Selenium;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.*;
import java.io.File;
import java.net.URI;


public class SeleniumTest extends Selenium {

    WebDriver driver;
    Browser browser;

    public SeleniumTest() {
        //Change this constructor to private if you supply your own public constructor
    }


    @BeforeClass
    public static void setUpBeforeClass() throws Exception {

        instance = new SeleniumTest();
        globalSetup(SeleniumTest.class);


    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        globalTearDown();

    }

    @Before
    public void setUp() throws Exception {


    /*
        This will not work in LeanFT 14.00, so we are commenting it out

        ModifiableSDKConfiguration sdkConfig = new ModifiableSDKConfiguration();

        sdkConfig.setServerAddress(new URI("ws://localhost:5095"));
        SDK.init(sdkConfig);
        ModifiableReportConfiguration reportConfig = new ModifiableReportConfiguration();
        reportConfig.setOverrideExisting(true);
        reportConfig.setTargetDirectory("C:\\Users\\user\\IdeaProjects\\LeanFT_Selenium\\RunResults");
        reportConfig.setReportFolder("LastRun");
        reportConfig.setTitle("Summary");
        reportConfig.setDescription("Description");
        reportConfig.setSnapshotsLevel(CaptureLevel.All);

        Reporter.init(reportConfig);
    */
    ChromeOptions options = new ChromeOptions();
    options.addExtensions(new File
            ("C:\\Program Files (x86)\\HP\\Unified Functional Testing\\Installations\\Chrome\\Agent.crx"));
    System.setProperty("webdriver.chrome.driver",
            "C:\\HP_Reporting\\Webdriver\\chromedriver.exe");
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(options);
    browser = BrowserFactory.attach(new BrowserDescription.Builder()
            .type(BrowserType.CHROME).build());
}

@After
public void tearDown() throws Exception {

    driver.quit();
    browser = null;

    //Reporter.generateReport();
    //SDK.cleanup();
}

@Test
public void test() throws Exception {

    driver.get("https://www.google.co.uk/");

}

}

但我需要配置 SDK 和报告。如何在 LeanFT 14.00 中实现这一目标?

SDKReporter有两种配置方式:

  1. 在初始化时,正如您尝试通过 ModifiableSDKConfigurationModifiableReportConfiguration 所做的那样

  1. 在测试设置文件 (resources/leanft.properties) 中。你可以看到plenty details in the official help

您还可以通过Reporter API 在运行时操作一些报告设置:

  • 设置报告级别:Reporter.setReportLevel(ReportLevel.All);
  • 设置快照捕获级别Reporter.setSnapshotCaptureLevel(CaptureLevel.All);
  • 即使您通过基类初始化了SDKReporter,您仍然可以在报表中添加自定义报表事件(这些工作:Reporter.reportEvent();Reporter.startReportingContext();Reporter.startTest();等)

【讨论】:

  • 非常感谢您提供如此详尽的回答 - 您非常有帮助。非常感谢您的帮助和花费的时间。
  • 我的目标是让leanft问题的回答率尽可能接近100%。如果这回答了您的问题,请考虑接受它。有关它的更多信息in the help centerthis question
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-15
  • 2017-12-24
相关资源
最近更新 更多