我们的团队使用TestNG在Selenium中运行一些测试。我们需要能够在3种不同的浏览器(Chrome,Firefox和[可悲的] IE)上运行给定的测试。我们的基础测试类上有一个browser
参数,实际上我们可以声明三个测试,每个测试对应一个;但是,我们真的希望能够将browser
值指定为“标准3 ”,并自动在每个浏览器上运行测试。
所以,我已经构建了一个实现ISuiteListener
的类,并尝试动态创建新的测试。但是,我试图添加测试的任何方式都失败了。也就是说,套件不会执行我尝试添加的新测试。这就像我实际上没有改变任何东西。
这是我的代码:
public class Standard3BrowserSuiteListener implements ISuiteListener {
@Override
public void onStart(final ISuite suite) {
final XmlSuite xmlSuite = suite.getXmlSuite();
final Map<String, String> suiteParameters = xmlSuite.getParameters();
final List<XmlTest> currentTests = new ArrayList<XmlTest>(xmlSuite.getTests());
final ArrayList<XmlTest> testsToRun = new ArrayList<XmlTest>(currentTests.size());
for (final XmlTest test : currentTests) {
final Browser browser;
final Map<String, String> testParameters = test.getAllParameters();
{
String browserParameter = testParameters.get("browser");
if (browserParameter == null) {
browserParameter = suiteParameters.get("browser");
}
browser = Util.Enums.getEnumValueByName(browserParameter, Browser.class);
}
if (browser == Browser.STANDARD_3) {
XmlTest nextTest = cloneTestAndSetNameAndBrowser(xmlSuite, test, testParameters, "Chrome");
xmlSuite.addTest(nextTest);
testsToRun.add(nextTest); // alternate I've tried to no avail
nextTest = cloneTestAndSetNameAndBrowser(xmlSuite, test, testParameters, "Firefox");
xmlSuite.addTest(nextTest);
testsToRun.add(nextTest); // alternate I've tried to no avail
nextTest = cloneTestAndSetNameAndBrowser(xmlSuite, test, testParameters, "IE");
xmlSuite.addTest(nextTest);
testsToRun.add(nextTest); // alternate I've tried to no avail
} else {
testsToRun.add(test);
}
}
// alternate to xmlSuite.addTest I've tried to no avail
testsToRun.trimToSize();
currentTests = xmlSuite.getTests();
currentTests.clear();
currentTests.addAll(testsToRun);
}
private XmlTest cloneTestAndSetNameAndBrowser(final XmlSuite xmlSuite, final XmlTest test,
final Map<String, String> testParameters, final String browserName) {
final XmlTest nextTest = (XmlTest) test.clone();
final Map<String, String> nextParameters = new TreeMap<String, String>(testParameters);
nextParameters.put("browser", browserName.toUpperCase());
nextTest.setName(browserName);
final List<XmlClass> testClasses = new ArrayList<XmlClass>(test.getClasses());
nextTest.setClasses(testClasses);
return nextTest;
}
@Override
public void onFinish(final ISuite suite) {}
}
如何将测试替换为browser
值“标准3 ”并进行3次测试,并使其正常运行?谢谢!
答案 0 :(得分:1)
这是您需要做的事情:
<listeners>
标记通过ServiceLoaders
连接此侦听器(如此界面的javadoc中所述)