我手头有一个任务,要测试一个使用selenium的网站。我必须检查所有破碎的链接和图片在所有网页的网站。我在junit中使用selenium,我编写了两个测试用例,在我网站的HomePage上测试坏链接和图像,测试用例运行良好。现在,我想将这些测试用例应用到网站的所有页面上,但我缺乏想法,因为我对selenium和junit完全陌生。据我所知,我无法控制junit测试的调用,否则,我正在考虑编写一个函数,在这个函数中对浏览器中的所有网页进行迭代,并调用每个网页的测试,但我相信这是行不通的。任何想法都是受欢迎的,但我不能改变我的工具,我需要一些在java中适用于selenium的建议。
发布于 2016-02-05 09:29:15
您可以这样轻松地循环您的测试:
public class SeleniumLoop {
// make your list of urls, can be static for example:
private static List<String> urls;
static {
urls = new ArrayList<>();
urls.add("http://www.test/1");
urls.add("http://www.test/2");
urls.add("http://www.test/3");
}
// Java 8 style
@Test
public void testAllUrls() {
urls.stream().forEach(url -> {
yourTest(url);
});
}
private void yourTest(String url) {
// your selenium webbdriver
driver.get(url);
// your test here
}
}请注意,这只是一个普通的-未经测试的例子。它只是表明,您可以轻松地循环您的Junit测试。
发布于 2016-02-05 10:08:50
如果您想为几个不同的变量(例如不同的URL)运行相同的测试用例,那么可以尝试JUnit参数。
@RunWith(Parameterized.class)
public class ParameterDemo {
protected static final String[] URLS = {"http://url1.com", "http://url2.com", "http://url3.com"};
@Parameters
public static Collection<Object[]> data() {
Collection<Object[]> data = new ArrayList<Object[]>();
for (String i : URLS) {
data.add(new String[] {i});
}
return data;
}
@Parameter public String url;
@Test
public void test() {
driver.get(url);
//perform test actions
}
}https://stackoverflow.com/questions/35220100
复制相似问题