Selenium 方法封装 一
2024-08-29 08:47:38
Selenium 封装
Selenium 封装
WebDriver对页面的操作,需要找到一个WebElement,然后再对其进行操作,比较繁琐:
- WebElement element =driver.findElement(By.name("q"));
- element.sendKeys("Cheese!");
我们可以考虑对这些基本的操作进行一个封装,简化操作。比如,封装代码:
- protected void sendKeys(By by, String value){
- driver.findElement(by).sendKeys(value);
- }
那么,在测试用例可以这样调用:
sendKeys(By.name("q"),”Cheese!”);
类似的封装还有:
- import java.util.List;
- import java.util.NoSuchElementException;
- import java.util.concurrent.TimeUnit;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.remote.RemoteWebDriver;
- import org.openqa.selenium.support.ui.WebDriverWait;
- public class DriverAction {
- protected RemoteWebDriver driver;
- protected WebDriverWait driverWait;
- private int WAIT_ELEMENT_TO_LOAD=10;
- protected boolean isWebElementExist(By selector) {
- try {
- driver.findElement(selector);
- return true;
- } catch(NoSuchElementException e) {
- return false;
- }
- }
- protected String getWebText(By by) {
- try {
- return driver.findElement(by).getText();
- } catch (NoSuchElementException e) {
- return "Text not existed!";
- }
- }
- protected void clickElementContainingText(By by, String text){
- List<WebElement>elementList = driver.findElements(by);
- for(WebElement e:elementList){
- if(e.getText().contains(text)){
- e.click();
- break;
- }
- }
- }
- protected String getLinkUrlContainingText(By by, String text){
- List<WebElement>subscribeButton = driver.findElements(by);
- String url = null;
- for(WebElement e:subscribeButton){
- if(e.getText().contains(text)){
- url =e.getAttribute("href");
- break;
- }
- }
- return url;
- }
- protected void click(By by){
- driver.findElement(by).click();
- driver.manage().timeouts().implicitlyWait(WAIT_ELEMENT_TO_LOAD,TimeUnit.SECONDS);
- }
- protected String getLinkUrl(By by){
- return driver.findElement(by).getAttribute("href");
- }
- protected void sendKeys(By by, String value){
- driver.findElement(by).sendKeys(value);
- }
- }
最新文章
- java对象与XML相互转化
- Android开发:第四日番外——Assets文件夹和RAW文件夹区别
- Android UI设计系统-android selector 开始自定义样式
- Windows 8 应用开发 - 异步调用
- 织梦不仅是链接到其他调用next
- C#拾遗(二、函数)
- 微信自定义菜单url默认80端口问题解决
- SQL语句执行性能
- UTF8 &; GBK之间的转换
- [py]python的继承体系
- jsapi微信扫一扫
- Zabbix监控介绍及安装配置
- Sybase数据库应用系统调优的五大领域
- Apache rewrite 出现 400 Bad Request 的解决方法
- Djano + Nginx + docker配置与管理
- linux下安装使用虚拟环境
- 【转发】Build Squid with SSL Bump and ICAP Client
- v4l2驱动文档之——streaming IO【转】
- <;<;= 什么意思?|=什么意思?
- ios界面笔记(二)