Selenium+TestNG Web自动化测试环境搭建5_高亮对象
来源:互联网 发布:java中jsonobject遍历 编辑:程序博客网 时间:2024/06/08 12:27
为了在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;//初始化driverpublic void initDriver() {System.out.println();driver = new ChromeDriver();System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");}//封装highlightpublic 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">
0 0
- Selenium+TestNG Web自动化测试环境搭建5_高亮对象
- Selenium+TestNG Web自动化测试环境搭建1_前言
- Selenium+TestNG Web自动化测试环境搭建2_软件安装
- Selenium+TestNG Web自动化测试环境搭建4_一个简单的例子
- Selenium+TestNG Web自动化测试环境搭建7_截取屏幕
- Selenium+TestNG Web自动化测试环境搭建8_处理一些不常见的场景
- Selenium+TestNG Web自动化测试环境搭建3_xpath学习
- Selenium+TestNG Web自动化测试环境搭建9_ANT自动化执行以及testNG报告优化
- selenium+testNG-自动化测试环境的搭建
- selenium+testNG-自动化测试环境的搭建
- Selenium终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG
- Selenium+Java+Appium+TestNg环境搭建——Web自动化测试与HTML5测试
- Selenium+Java+Appium+TestNg环境搭建——Web自动化测试与HTML5测试(2)
- Selenium+Java+Appium+TestNg环境搭建——Web自动化测试与HTML5测试(3)
- Selenium+Java+Appium+TestNg环境搭建——Web自动化测试与HTML5测试(4)
- Selenium+TestNG Web自动化测试环境搭建6_selenium中的等待
- 搭建基于Selenium+TestNG+Maven+Jenkins+SVN的Web端UI自动化测试环境
- 搭建基于Selenium+TestNG+Maven+Jenkins+SVN的Web端UI自动化测试环境
- RTSP交互命令简介及过程参数描述
- checkbox和说明文字对齐
- 用户登录之SharedPreferences保存用户名
- socket网络编程基础
- unexpected inconstancy
- Selenium+TestNG Web自动化测试环境搭建5_高亮对象
- redis的配置详解
- Android Tv电视机盒子使用Recyclerview实现飞框动画
- android 的activity中addContentView 和 setContentView 的区别
- H5调用手机的摄像头拍照上传以及手机相册选取照片
- 请求转发
- C语言实现字符串中的单词的逆转
- DXT and ETC Compression Texture
- SQLite增加字段