1. selenide简介
- selenide是一款基于webdriver的ui自动化工具;算是对selenium的进一步封装,让api更简洁,更方便使用,个人感觉还不错。
- 官文文档地址 https://selenide.org/documentation.html, 不过文档介绍较少,是因为selenide觉得聪明的软件不需要文档,都可以上手,而selenide就属于这样的软件。
2. selenide安装
- 首先下载和浏览器匹配的webdriver,下载地址:http://chromedriver.storage.googleapis.com/index.html
- 使用maven下载selenide依赖,顺便可以下个testng和selenium的,可以在mvn仓库进行下载;
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>5.2.8</version>
</dependency>
</dependencies>
3. selenide主要的api
-
selenide类下的基本方法
open(String url) – 打开浏览器并加载url;
如:open(https://selenide.org/documentation.html)
(StringcssSeltor)–单个元素定位,默认为css定位;setValue()方式,如:(String cssSeltor) – 单个元素定位,默认为css定位; setValue() 方式,如:(StringcssSeltor)–单个元素定位,默认为css定位;setValue()方式,如:(byXpath("//*[@id=‘loginName’]")).setValue(“1222")
Click() 方法,如:$(By.xpath("//span[text()=‘登 录’]")).click(); -
Conditiion类,断言方法(这是selenide改进的最有意义的一点)
断言元素的属性hidden,exist,isabled等;
should(Condition) ,如:("input").should(exist);shouldBe(Condition),如:("input").should(exist); shouldBe(Condition) ,如:("input").should(exist);shouldBe(Condition),如:(“input”).shouldBe(visible);
断言元素的值等;
shouldHave(Condition),如:$(“input”).shouldHave(exactText(“Some text”)); -
Selectors类,元素定位,比起selenium增加了用户text定位,很适合初学者
byText- 通过精确文本定位元素
withText- 通过包含的文本(子字符串)定位元素
by(attributeName,attributeValue) -按属性名称和值定位
byTitle- 按属性“标题”定位
byValue- 按属性“值”定位
byXpath 等,如,$(byXpath(“input”)) -
Configuartion 类,基本的一些配置功能;
比如指定浏览器,Browser(ie, chrome, firefox 等),Configuration.browser = “chrome”;
了解以上的api基本就可以开始写简单的用例了,想了解更多的api,可以直接看源码。
4. 跟selenium的对比
以下是写的一个简单的selenide的例子,如果有selenium的基础大概一眼就可以看出它的实现方法了,还是selenium的老套路,但是却比selenium简单很多,定位,输入值或是点击,然后验证。
当然觉得selenide不能满足需求,想直接使用selenium, WebDriver driver = new ChromeDriver(); 就可以直接使用selenium的方法了。
@Test
public void testDemo(){
System.setProperty("webdriver.chrome.driver", "D:\\project\\selenidedemo\\src\\test\\resources\\chromedriver.exe");
Configuration.browser = "chrome";
open("https://abc.com/ecd/login");
$(byXpath("//*[@id='loginName']")).setValue("232323");
$(byXpath("//*[@id='loginPwd']")).setValue("343434");
$(byXpath("//*[@id='root']//button[span[text()='登 录']]")).click();
$(byXpath("/html/body//form/button[span[text()='提 交']]")).click();
$(byXpath("//*[@id='root']//div[@class='username ant-dropdown-trigger']")).should(Condition.text("232323"));
}
使用selenium写的相同的测试用例,从代码篇幅来说就比selenide要多很多;
selenide还有一个优点,就是优化了很多selenium的方法,比如click功能,下面有一个click直接点是点不到的,需要用js方式点击,但是selenide的click方法直接包含js点击的;
如下直接使用selenium的定位弹框的提交按钮元素是老是定位不到,需要先聚焦到当前弹框下才可以,但是selenide也直接优化了此方法。
@Test
public void testSeleniumDemo() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\project\\selenidedemo\\src\\test\\resources\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://abc.com/ecd/login");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@id='loginName']")).sendKeys("213123");
driver.findElement(By.xpath("//*[@id='loginPwd']")).sendKeys("34443");
WebElement loginBtn = driver.findElement(By.xpath("//*[@id='root']//button[span[text()='登 录']]"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();",loginBtn);
Thread.sleep(2000);
driver.getWindowHandle();
driver.findElement(By.xpath("/html/body//form/button[span[text()='提 交']]")).click();
Thread.sleep(2000);
Assert.assertEquals("213123", driver.findElement(By.xpath("//*[@id='root']//div[@class='username ant-dropdown-trigger']")).getText());
driver.close();
}```
总体来说selenide跟selenium相比有以下优点;
1. api更简洁,更方便使用,比如上面说的click和定位方法,用例执行完浏览器自动关闭;
2. 自己封装了断言方法,每个should断言都会有一个默认4秒的等待时间,因为浏览器各方面原因,期望的元素不一定按时出现,所以在4秒钟,它会一直不停的check,直到元素出现;
3. 错误后会自动截图;
4. 元素定位增加了text的定位方式,简单容易;
唯一的缺点就是目前仅支持 java语言。