【问题标题】:TestNG Annotations in a Superclass超类中的 TestNG 注解
【发布时间】:2014-02-28 18:01:23
【问题描述】:

我在使用 TestNG 注释时遇到了一些问题,而且我在 stackoverflow 或 testng 文档中都没有找到好的答案。我想要做的是将 testng 侦听器以及 testng.xml 文件中的参数添加到“测试基础”超类。所有 testng 测试都将继承超类。这将减轻 testng 测试类文件中的大量代码冗余。但是当我在超类中有@Listeners 和@Parameters 注释,而不是包含@Test 方法的类时,注释不起作用。换句话说,监听器似乎没有“看到”测试,并且来自 testng.xml 的参数在运行时不会被拉入超类。

我的“测试库”(超类)有一个来自Sauce Labs api 的 testng 侦听器,它将侦听测试并在远程执行期间将测试的成功/失败更新到 Sauce Labs。它还使用@Parameters 注解从当前测试运行的 testng.xml 中提取参数。

TestBase 超类:

@Listeners(SauceOnDemandTestListener.class)
public class TestBase implements SauceOnDemandSessionIdProvider, SauceOnDemandAuthenticationProvider {
    private SauceOnDemandAuthentication authentication;
    private DesiredCapabilities capabilities;
    protected ParallelWebDriver parallelWebDriver;

    @Parameters({"sauce_username", "sauce_accesskey", "platform", "browser", "version"})
    @BeforeClass(alwaysRun = true)
    public void setUp(String userName, String accessKey, String platform, String browser, String version) {   
        // Set up Sauce Auth
        if (userName != null && accessKey != null) {
            this.authentication = new SauceOnDemandAuthentication(userName, accessKey);
        } else {
            this.authentication = new SauceOnDemandAuthentication();
        }

        // Set up the DesiredCapabilities
        if (platform != null) {
            this.capabilities.setCapability("platform", platform);
        } else {
            throw new NullArgumentException("[Parameter] platform does not exist or is not a valid value in testng.xml:");
        }

        if (browser != null) {
            this.capabilities.setCapability("browser", browser);
        } else {
            throw new NullArgumentException("[Parameter] browser does not exist or is not a valid value in testng.xml:");
        }

        if (version != null) {
            this.capabilities.setCapability("version", version);
        } else {
            throw new NullArgumentException("[Parameter] version does not exist or is not a valid value in testng.xml:");
        }

        // Set up the ParallelWebDriver for the test run
        parallelWebDriver = new ParallelWebDriver(new RemoteParallelDriver(), this.testingPlatform, capabilities);
        parallelWebDriver.setUserName(userName);
        parallelWebDriver.setAccessKey(accessKey);
    }

    @Override
    public String getSessionId() {
        SessionId sessionId = ((RemoteWebDriver)((WebDriver)parallelWebDriver.getDriver())).getSessionId();
        return (sessionId == null) ? null : sessionId.toString();
    }

    @Override
    public SauceOnDemandAuthentication getAuthentication() {
        return authentication;
    }
}

这是一个继承 TestBase 的示例测试类。

public class SauceLabsRemote_Test extends TestBase {
    @BeforeTest
    public void testSetUp() throws MalformedURLException {
        parallelWebDriver.openBrowser();
    }

    public void searchGoogle(String searchText) throws InterruptedException {
        parallelWebDriver.getDriver().get("https://google.com");
        WebElement searchBox = parallelWebDriver.getDriver().findElement(By.id("gbqfq"));
        searchBox.sendKeys(searchText);
        WebElement searchButton = parallelWebDriver.getDriver().findElement(By.id("gbqfb"));
        searchButton.click();

        Assert.assertEquals(searchBox.getText(), "banjo");
    }

    @Test
    public void searchGoogle_Test1() throws InterruptedException {
        searchGoogle("banjo");
    }

    @Test
    public void searchGoogle_Test2() throws InterruptedException {
        searchGoogle("guitar");
    }

    @AfterTest
    public void testTearDown() {
        parallelWebDriver.closeBrowser();
    }
}

这是我的 testng.xml 的模型。

<suite name="Base Framework Unit Test Suite" verbose="1" >   

    <!-- Parameters required for Sauce Labs run -->
    <parameter name="sauce_username" value="testuser" />
    <parameter name="sauce_accesskey" value="acc3-55k3y-t3st-t3st-t3st" />

    <test name="Sauce Labs Remote Execution Test" >
        <parameter name="platform" value="OS X 10.6" />
        <parameter name="browser" value="firefox" />
        <parameter name="version" value="26" />
        <classes>
            <class name="unittest.SauceLabsRemote_Test" />
        </classes>
    </test>

</suite>

那么,有什么方法可以让 TestNG 的 @Listeners 和 @Parameters 在不包含 @Test 的超类上工作?提前致谢!对于任何不良的编码习惯,我们深表歉意;所有这些代码都是即时模拟的。

【问题讨论】:

  • 是否有您想要的特定测试?还是您希望您的所有测试都具备您的听众的能力?
  • 这是个好问题!如果我在 Sauce Labs 远程运行测试,我只想要监听器的功能。例如,如果我对 Selenium Grid 执行测试,我不需要监听器。这种思路让我相信我需要编写一个自定义的侦听器来处理远程 Sauce Labs 运行、Selenium Grid 运行和本地运行。这实际上可能是我的问题!我会用这个理论做一些测试,然后回复你。谢谢!
  • :) 很高兴这个问题有帮助!

标签: java testng saucelabs


【解决方案1】:

超类中的@Listeners 和@Parameters 注解将在子类中继承。我已经对此进行了测试并验证了它是否有效。因此,就像我的代码示例一样,我的 TestBase 父类中使用的 @Listeners 注释侦听继承了 TestBase 的子类的任何 @Test 注释。

我遇到的问题与 Sauce Labs 测试执行与 Selenium Grid 测试执行与本地测试执行有关。我必须向 Sauce Lab 的 SauceOnDemandTestListener 类添加逻辑,以确定测试执行是否确实路由到 Sauce Labs VM 浏览器作业。这个逻辑超出了我原来的问题的范围,所以我不会在这里发布。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多