如果我需要同时使用两个不同的浏览器运行相同的方法,那么我将如何实现它? 例如:
public class AppTest2{
@parameters("browser")
@Test(dataProvider="loginData")
public void login(String userName , String password, String param){
if(param.equals("firefox"){
//do something
}
if(param.equals("chrome"){
//do something else
}
}
}
我的testng.xml文件中的包含:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name ="My sutie" parallel = "methods", thread-count="5">
<parameter name="browser" value="firefox"/>
<test name ="My Test1">
<classes>
<class name="mq.demo.selenium.AppTest2"/>
</classes>
</test>
</suite>
所以我的目标是使用两个不同的线程同时在两个不同的浏览器中运行login方法。
任何人都可以帮忙吗?
由于
答案 0 :(得分:2)
您可以考虑以下内容作为可能的解决方案
package com.rationaleemotions.stackoverflow;
import org.testng.IAlterSuiteListener;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.collections.Maps;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MultiBrowserSample {
@Test
@Parameters("browser")
public void testMethod(String browser) {
System.err.println("Browser : " + browser + " on Thread [" + Thread.currentThread().getId() + "]");
}
public static class MySuiteAlterer implements IAlterSuiteListener {
@Override
public void alter(List<XmlSuite> suites) {
XmlSuite suite = suites.get(0);
//Check if there was a parameter named "browserFlavors" defined at the suite
String browserFlavors = suite.getParameter("browserFlavors");
if (browserFlavors == null || browserFlavors.trim().isEmpty()) {
//If no such parameter was found, then Try querying the JVM arguments to see if it contains
//value for it. Just to ensure we don't end up in a situation wherein there's no JVM also provided
//Lets add a default value for the JVM argument which in our case is "firefox"
browserFlavors = System.getProperty("browserFlavors", "firefox");
}
String[] browsers = browserFlavors.split(",");
List<XmlTest> xmlTests = new ArrayList<>();
for (String browser : browsers) {
XmlTest xmlTest = new XmlTest(suite);
xmlTest.setName(browser + "_test");
Map<String, String> parameters = Maps.newHashMap();
parameters.put("browser", browser);
xmlTest.setParameters(parameters);
XmlClass xmlClass = new XmlClass();
xmlClass.setName(MultiBrowserSample.class.getCanonicalName());
xmlTest.getClasses().add(xmlClass);
xmlTests.add(xmlTest);
}
suite.setTests(xmlTests);
}
}
}
套件xml文件可能如下所示
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="1265_Suite" parallel="tests" verbose="2">
<listeners>
<listener class-name="com.rationaleemotions.stackoverflow.MultiBrowserSample$MySuiteAlterer"/>
</listeners>
<!--
If the below line gets uncommented, then 3 <test> tags will be formed one for each browser flavor.
Since its now commented, you will have to provide a value for it via the JVM argument
-DbrowserFlavors=firefox,chrome,ie (or) the system will default to just working with firefox
-->
<!--<parameter name="browserFlavors" value="firefox,chrome,ie"/>-->
</suite>
正如您所看到的,这里我们正在使用名为IAlterSuiteListener
实现的TestNG侦听器,这将有助于我们动态地构建套件xml文件中的<test>
标记以及{套件xml文件中的{1}}标签将直接等于通过套件级参数<test>
(或)通过JVM参数browserFlavors
输出结果如下
-DbrowserFlavors