超类中的 TestNG 注释 [英] TestNG Annotations in a Superclass

查看:25
本文介绍了超类中的 TestNG 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having some trouble with TestNG annotations, and I haven't found a good answer either on stackoverflow or in the testng documentation. What I'm trying to do is add a testng listener as well as parameters from a testng.xml file to a "test base" superclass. All testng tests will inherit the superclass. This will alleviate a lot of code redundancy in the testng test class files. But when I have the @Listeners and @Parameters annotation in the superclass, and not the class that contains the @Test method(s), the annotations do not work. In other words, the listener does not seem to "see" the tests, and the parameters from testng.xml do not get pulled in to the superclass during runtime.

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

My "test base" (the superclass) has a testng listener from the Sauce Labs api that will listen for tests and update the success/failure of the test to Sauce Labs during remote execution. It also uses the @Parameters annotation to pull parameters from the current test run's testng.xml.

TestBase 超类:

TestBase superclass:

@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 的示例测试类.

Here is an example test class that inherits the 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 的模型.

And here is a mockup of my 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 的超类上工作?提前致谢!对于任何糟糕的编码实践,我们深表歉意;所有这些代码都是即时模拟的.

So, is there any way to get TestNG's @Listeners and @Parameters to work on a superclass that does not contain @Test? Thanks in advance! And sorry for any bad coding practices; all this code was mocked up on-the-fly.

推荐答案

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

@Listeners and @Parameters annotations in a superclass will be inherited in the subclass. I have tested this and verified that it works. So, like my code example, the @Listeners annotation used in my TestBase parent class listened for any @Test annotations for a child class that inherited TestBase.

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

The issue I was having was related to Sauce Labs test execution vs. Selenium Grid test execution vs. local test execution. I had to add logic to Sauce Lab's SauceOnDemandTestListener class to figure out if the test execution was indeed routing to Sauce Labs VM browser jobs. That logic is outside of the scope of my original question, so I won't post it here.

这篇关于超类中的 TestNG 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆