Selenium+TestNG Web自动化测试环境搭建5_高亮对象
2016-09-09 13:28
225 查看
为了在case执行的时候,能更直观地显示出当前操作的元素,我们利用javascript来高亮显示元素。
继续使用前一篇文章的例子。
原理就是利用javascript给当前操作的元素加上背景色或者边框等等。
说明:当前元素往往伴随着某一个具体的操作,因此可以将highlight封装成一个公共函数。然后在执行某个具体操作时调用它就可以了。
JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].setAttribute('style', arguments[1]);", webElement, "background: yellow; color: red; border: 2px solid red;");
这段代码的意思就是:给webElement加上style:背景为黄色,内容和边框位红色。
(如果觉得这样的style有点复杂,可以进行调整:比如 去掉background等)
附上完整代码:
package selTest.web.testBD;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class A123 {
public WebDriver driver = null;
public WebElement webelement = null;
//初始化driver
public void initDriver() {
System.out.println();
driver = new ChromeDriver();
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
}
//封装highlight
public WebElement highLight(WebElement webElement) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', arguments[1]);", webElement, "background: yellow;color: red; border: 2px solid red;");
return webElement;
}
@Test(priority = 0)
public void openBD(){
initDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
}
@Test(priority = 10)
public void searchCSDN() throws InterruptedException{
webelement = driver.findElement(By.xpath("//input[@id = \"kw\"]"));
highLight(webelement);
webelement.sendKeys("csdn");
webelement = driver.findElement(By.xpath("//*[@id=\"su\"]"));
highLight(webelement);
webelement.click();
Thread.sleep(3001);
}
@Test(priority = 20)
public void checkSearchResult(){
String pagesorce = driver.getPageSource();
Assert.assertTrue(pagesorce.contains("全球最大中文IT社区,为IT专业技术人员提供最全面的"));
}
}
效果截图:
</pre><pre name="code" class="java">
相关文章推荐
- socket网络编程基础
- 用户登录之SharedPreferences保存用户名
- android 2016进阶路线与愿景
- sql 转义字符
- checkbox和说明文字对齐
- RTSP交互命令简介及过程参数描述
- linux的Make使用的重定向
- 【MySQL】计算 TPS,QPS 的方式
- linux redis 和 windows redis 的安装
- run only in unchained transaction mode
- webbrowser 内核切换
- Redis常用命令
- 绕过XSS过滤规则 : Web***测试高级XSS教程
- 1087. All Roads Lead to Rome (30)-PAT甲级真题-Dijkstra + DFS
- STL:map和BOOST:unordered_map 实现简单比较
- ADO at a glance
- 开启mysql定时任务,关闭过了求助时间的帖子
- 使用js来获取cookie的值的方法
- angularjs “坑” 小结
- Android Service :利用Binder实现通信