在pom.xml中加入如下依赖
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>4.4.1</version>
<scope>test</scope>
</dependency>
常用配置:
- selenide默认使用Firefox浏览器,如何切换成chrome浏览器?
System.setProperty("webdriver.chrome.driver", driverPath);
Configuration.browser="chrome";
- selenide如何设置代理?
Proxy proxy = new Proxy();
proxy.setHttpProxy("127.0.0.1:8080");
WebDriverRunner.setProxy(proxy);
- selenide如何修改报告默认生成路径?
Configuration.reportsFolder = "target/reports/test"+Configuration.browser;
- selenide如何下载非
<a>
标签的文件?
//默认值是FileDownloadMode.HTTPGET,此值只用作用于<a>标签
Configuration.fileDownload = FileDownloadMode.PROXY;
- 如何保持浏览器运行完不关闭?
//默认值是false
Configuration.holdBrowserOpen = true;
- 如何使用自己实例化的
driver
?
WebDriverRunner.setWebDriver(driver);
此处需要注意,如果使用自己设置的
driver
,selenide
不会自动关闭brower
,需要我们手动关闭!
常见操作:
- 打开一个url:
open("https://www.baidu.com/");
- 鼠标点击:
//默认使用css selector的方式来查找元素
Selenide.$(element).click();
Selenide.$(By.xpath(element)).click();
- 文本框赋值:
Selenide.$(By.xpath(element)).doubleClick();
- 鼠标事件:
Selenide.$(By.xpath(element)).contextClick();
- 鼠标右键事件:
Selenide.$(By.xpath(element)).setValue(value);
- 判断元素是否存在:
Selenide.$(By.xpath(element)).exists();
- 下载文件的操作:
Selenide.$(By.xpath(element)).download();
默认只能下载
<a>
标签的元素
若想下载其他标签里面的元素则需另外设置
- 上传文件的操作:
Selenide.$(By.xpath(element)).uploadFile(file);
uploadFile的参数为File类型
- 查询元素的文本内容
Selenide.$(By.xpath(element)).text();
- 等待直到存在某个元素
Selenide.$(By.xpath(element)).waitUntil(exist , timeoutSeconds*1000);
- 等待直到元素在页面可见
Selenide.$(By.xpath(element)).waitUntil(visible , timeoutSeconds*1000);
- 等待直到满足两个条件中的任意一个,就相当于
if(a || b){}
这种写法,第一个参数是备注
Selenide.$(By.xpath(element)).waitUntil(or("有一个相等", text(param1), text(param2)) , timeoutSeconds*1000);
- 等待直到某个元素不存在
Selenide.$(By.xpath(element)).waitUntil(hidden, timeoutSeconds*1000);
- 等待直到匹配到文本内容
Selenide.$(By.xpath(element)).waitUntil(matchesText(text),timeoutSeconds*1000)
此处matchesText里面可以匹配正则表达式
- 不存在某个元素
Selenide.$(By.xpath(element)).shouldNot(exist);
- 移动鼠标到某个元素上
Selenide.$(By.xpath(element)).hover()
- 获取当前页面title
Selenide.title()
- 获取当前页面url
WebDriverRunner.url()
- 切换到另一个页面
Selenide.switchTo().window(nameOrHandle)
- 获取当前driver
WebDriverRunner.getWebDriver();
- 清除浏览器cookie
Selenide.clearBrowserCookies();
- 刷新当前页面
Selenide.refresh();
- 出现alert时候出来情况
Selenide.confirm();
Selenide.dismiss();```
网友评论