Selenide + chrome 打开一个页面
2017-12-06 本文已影响0人
Ksewen
Selenide简介
Selenide是一个比Selenium更强大的自动化测试工具,具有以下优势:
- 简洁流畅的API
- 稳定的Ajax支持
- 更强大的selectors
- 简单的配置
使用Selenide不需要考虑如何关闭浏览器、处理超时和StaleElement异常或日志,只关注业务逻辑。
Demo
新建maven工程的结构
maven工程结构pom.xml配置
只列出<dependencies>标签的配置
<dependencies>
<!-- Selenide依赖 -->
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>selenide</artifactId>
<version>4.8</version>
</dependency>
<!-- Junit4依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- log4j2及相关依赖 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>
Selenide使用slf4j处理日志,以上dependencies配置中配置了log4j2,以便于运行用例时正确输出相关日志,log4j2相关模块可以不用配置,也不影响用例正常运行。
可选 - log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<ThresholdFilter level="info" onMatch="ACCEPT"
onMismatch="DENY" />
<!--输出日志的格式 -->
<PatternLayout
pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="log" fileName="./Logs/build.error" append="false">
<ThresholdFilter level="error" onMatch="ACCEPT"
onMismatch="DENY" />
<PatternLayout
pattern="%d{YYYY-MM-dd HH:mm:ss.SSS} %-5level %L %M - %msg%xEx%n" />
</File>
</Appenders>
<Loggers>
<Logger name="main.java.com.interf.eyee.utils.Log" level="all">
<AppenderRef ref="Console" />
<AppenderRef ref="log" />
</Logger>
<Root level="trace">
<AppenderRef ref="Console" />
<AppenderRef ref="log" />
</Root>
</Loggers>
</Configuration>
Junit4测试用例代码
import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.WebDriverRunner;
import static com.codeborne.selenide.Selenide.*;
public class TestSelenide {
// chromedriver路径
private static final String CHROME_DRIVER_PATH = "d:/webdrivers/chromedriver.exe";
@Before
public void before() {
System.setProperty("webdriver.chrome.driver", CHROME_DRIVER_PATH);
Configuration.browser = WebDriverRunner.CHROME;
Configuration.baseUrl = "https://www.zhihu.com/";
}
@Test
public void test() {
open("/");
}
}