Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果第一个测试用例使用selenium失败,如何执行第二个测试用例_Java_Selenium Webdriver - Fatal编程技术网

Java 如果第一个测试用例使用selenium失败,如何执行第二个测试用例

Java 如果第一个测试用例使用selenium失败,如何执行第二个测试用例,java,selenium-webdriver,Java,Selenium Webdriver,假设这是我的代码 Webdriver dr = new FireFoxdriver(); @test(prioity =1) { dr.findelement(By.id("element1_id")).click(); } @test(prioity =2) { dr.findelement(By.id("element2_id")).click(); } 如果dr.findelement(By.id(“element1_id”))。单击()抛出异常,然后如何不停止第二个测

假设这是我的代码

Webdriver dr = new FireFoxdriver();

@test(prioity =1)
{
    dr.findelement(By.id("element1_id")).click();
}
@test(prioity =2)
{
    dr.findelement(By.id("element2_id")).click();
}

如果
dr.findelement(By.id(“element1_id”))。单击()抛出异常,然后如何停止第二个测试用例的执行?

不确定您使用的是哪种语言,但参考您的示例,可以很容易地组合您的测试用例-这是一个java解决方案,也可以与其他语言一起使用:

WebElement element = null;
try{
   element = dr.findByElement(By.id("element1_id"));
}catch(NoSuchElementException e){
  // If no matching element for "element1_id" is found, search for the other one
   element = dr.findByElement(By.id("element2_id"));
  // you might want to check again the exception
}
if(element != null){
   element.click();
}
下面是findByElement方法的API文档及其返回值/异常


HTH

如果使用的是TestNG,则可以使用参数alwaysRun

@Test(priority = 1, alwaysRun = true)

无论在任何其他测试(或配置方法,请注意!)中发生了什么,这都将执行该方法。

它应该是
优先级
,而不是
优先级
。请编辑您的问题并添加stacktrace。这是JUnit的测试注释,还是TestNG的测试注释?我想这是TestNG,但我想确定一下。你问的问题不是selenium问题,而是关于你正在使用的测试框架的问题。此外,您使用的注释应该大写,如
@Test