我正在尝试获取页面的标题,有时它们会更改,但名称仅在3个名称之间,使用下面的if函数可以在selenium中获取。
但我正在将其转换为测试脚本的软断言。基本上,如果标题介于学生档案或测试或测试2之间,它应该通过测试。
if(contr0l.getTitle().contentEquals("Student Profile") || contr0l.getTitle().contentEquals("Experiential Learning") || contr0l.getTitle().contentEquals("")){
System.out.println("CV Link is Correct!"+ 'n' + "Title is " + contr0l.getTitle() + 'n');
}else{
System.out.println("Title is incorrect, Current Title" + contr0l.getTitle() + 'n');
}
Softfail.assertEquals(contr0l.getTitle(), ("Student Profile") , ("test")); // << this line
contr0l.navigate().back();
//Softfail.assertAll((;
您可以使用hamcrest库来简化断言。这是你的依赖:
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
</dependency>
这是你的测试示例:
package click.webelement.so;
import org.testng.annotations.Test;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
public class TestTitle {
@Test
public void testTitle() {
String observed = "Title that you have received from driver";
String[] expectedTitles = {
"Test 1",
"Title that you have received from driver",
"Test 2"
};
assertThat(observed, is(in(expectedTitles)));
}
}
UPD:不使用hamcrest:
package click.webelement.so;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.Arrays;
public class Main {
@Test
public void testTitle() {
String observed = "Title that you have received from driver";
String[] expectedTitles = {
"Test 1",
"Title that you have received from driver",
"Test 2"
};
Assert.assertTrue(Arrays.asList(expectedTitles).contains(observed));
}
}