版权声明:本文为AutotestOps原创文章,未经允许禁止转载。
Tool Tips是网页中比较常见的一种普通元素。当你把鼠标在某个元素上的时候,它就可以出现。
SuperXi带大家直接看一个例子:http://www.autotestops.com/test/ToolTip.html
这是专门为大家准备的测试网页。当你把鼠标停留在 “悬浮在我上面”这个链接上时,你会看到提示语的出现。 当你把鼠标停留在input元素上面的时候,你也会看到提示语的出现。
我们工作中有时需要去验证提示语的正确与否,如何去定位到提示语呢,我们看下这个元素:
<a data-toggle="tooltip" href="http://www.autotestops.com" title="提示框的文字!">悬浮在我上面</a>
可以看到,要获得提示语其实就是获得链接中的title属性,获取html属性代码如下:
String tooltiplink = driver.findElement(By.xpath("//a[contains(.,'悬浮在我上面')]")).getAttribute("title"); 获取input元素的提示语,方法也是一样的。
有了思路后,我们可以写出下面完整的代码:
public class ToolTipTest {
WebDriver driver;
@BeforeTest
public void setup() throws Exception {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.autotestops.com/test/ToolTip.html");
}
@Test
public void clickBeforeLoad() {
// 从链接里获取提示语.
String tooltiplink = driver.findElement(By.xpath("//a[contains(.,'悬浮在我上面')]")).getAttribute("title");
System.out.println("Tootltip text on link Is : " + tooltiplink);
//验证链接的提示语.
Assert.assertEquals(tooltiplink, "提示框的文字!");
// 从获取编辑框的提示语.
String tooltipinput = driver.findElement(By.xpath("//input[@id='tooltip-1']")).getAttribute("title");
System.out.println("Tootltip text on textbox Is : " + tooltipinput);
//验证编辑框的提示语.
Assert.assertEquals(tooltipinput, "输入你的名字");
}
}
这种问题在AutoTestOps系列课程中只能属于最基本的问题,只要你基本功扎实,思路有条理,解决这类问题相当轻松。如果你想系统的学习,赶紧报名参加AutoTestOps精品课程吧。
Happy Auto Testing
SuperXi
版权声明:本文为AutotestOps原创文章,未经允许禁止转载。