【发布时间】:2015-06-27 18:16:39
【问题描述】:
我是自动化测试/Java 的新手,所以如果这是一个愚蠢的问题,请原谅我。
我的团队正在考虑使用 Sauce Labs 而不是本地 Grid 配置来测试我们基于 Web 的应用程序。我成功地在 Sauce 的网站https://docs.saucelabs.com/tutorials/java/#running-tests-in-parallel 上基于此代码并行运行测试,但我不喜欢必须在每个测试用例上指定要测试的浏览器的冗余。此外,示例代码仅适用于桌面配置,不适用于移动设备(仅声明操作系统、版本和浏览器)。
我修改了代码,现在它从 Maven 项目中的文本文件中读取名称/值对,并遍历每一行并将每个名称/值设置为一项功能。使用这种方法,我不必更新我的每个测试,因为 WebDriver 支持新的浏览器版本/设备。
在研究如何更改代码以简化测试套件时,我发现了 JUnit 文档,该文档表明这种方法正在创建依赖项,http://junit.org/faq.html#organize_3。
我很想知道人们对这种方法的看法。从维护的角度来看,在测试用例中指定浏览器似乎是一场噩梦。有没有人有一个代码示例来说明他们如何设置测试套件或资源来引导我走向正确的方向?
感谢您的任何意见!
代码示例:
@RunWith(ConcurrentParameterized.class)
public class NewTest implements SauceOnDemandSessionIdProvider {
private String capabilities, sessionId, jobID;
private WebDriver driver;
public SauceOnDemandAuthentication authentication = new SauceOnDemandAuthentication(CommonConstants.SAUCE_USERNAME, CommonConstants.SAUCE_ACCESS_KEY);
public NewTest(String capabilities){
this.capabilities = capabilities;
}
@ConcurrentParameterized.Parameters
public static LinkedList<String[]> browsersStrings() throws IOException {
LinkedList<String[]> capabilities = new LinkedList<String[]>();
//get file of desired capabilities.
File file = new File(CommonConstants.CAPABILITIES_TEXT_FILENAME);
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
//iterate through each line of the file and assign that line to an array
String line;
while((line = bufferedReader.readLine()) != null) {
capabilities.add(new String[] {line});
}
//close the file reader
bufferedReader.close();
//return the array to each parallel test to be used in the setup() function
return capabilities;
}
@Before
public void setUp() throws Exception {
DesiredCapabilities capability = new DesiredCapabilities();
String[] caps = capabilities.split(";");
int numberOfCapabilities = caps.length;
for(int i = 0; i < numberOfCapabilities; i++) {
String[] nameValues = caps[i].split(",");
String n = nameValues[0].toString();
String v = nameValues[1].toString();
capability.setCapability(n, v);
}
//use this to use Sauce Labs
this.driver = new RemoteWebDriver(new URL("http://" + authentication.getUsername() + ":" + authentication.getAccessKey() + "@ondemand.saucelabs.com:80/wd/hub"), capability);
//use this to use local configuration. Other portions of test case will also have to be commented out...
/*this.driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);*/
}
@Test
public void test() throws Exception {
driver.get("https://www.google.com/");
jobID = ((RemoteWebDriver) driver).getSessionId().toString();
updateSauceStatus(jobID);
}
@After
public void tearDown() throws Exception {
//TODO validate the driver is still active
driver.quit();
}
@Override
public String getSessionId() {
return sessionId;
}
public void updateSauceStatus (String jobID) {
SauceREST client = new SauceREST(authentication.getUsername(), authentication.getAccessKey());
Map<String, Object> updates = new HashMap<String, Object>();
//TODO
/*updates.put("name", "this job has a name");*/
updates.put("passed", true);
//TODO
/*updates.put("build", "1.0.2");*/
client.updateJobInfo(jobID, updates);
client.getJobInfo(jobID);
}
}
正如 cmets 所指出的,这方面还有很多工作要做……
【问题讨论】:
标签: java selenium junit selenium-grid saucelabs