概述
1、创建maven项目
1.1、设置maven坐标,包括:selenium、cucumber(cucumber-java、cucumber-testng、gherkin、cucumber-core) 、testng
仓库搜索:https://mvnrepository.com/
<dependencies>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>LATEST</version>
</dependency>
<!-- selenium依赖 -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>LATEST</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-core -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
<version>LATEST</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-gherkin -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-gherkin</artifactId>
<version>LATEST</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>4.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>LATEST</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>LATEST</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm-deps -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-jvm-deps</artifactId>
<version>LATEST</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>LATEST</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting -->
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>4.11.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cobertura/cobertura -->
<dependency>
<groupId>cobertura</groupId>
<artifactId>cobertura</artifactId>
<version>1.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
1.2创建项目结构
3.2.1、创建src/test/java下的stepDefinition包
3.2.2、创建src/test/resources下的Features文件夹
在该目录下创建文件xx.feature
3.2.3、为Gherkin安装Natural Eclipse Editor识别Gherkin语言
1、插件市场安装Natural
2、使用feature运行需要依靠这个插件
3.2.4、安装cucumber插件
1、插件市场搜索cucumber安装或者指定路径在线安装http://cucumber.github.com/cucumber-eclipse/update-site
2、为Gherkin语言提供用例场颜色标注
2、编写场景
2.1、使用cucumber创建场景用例
步骤:
在xx.feature中编写场景
格式:
Feature: 用例名-搜索
Background: 背景-用户在http://www.baidu.com/搜索selenium信息
Scenario: 用例描述-在http://www.baidu.com网址上搜索“selenium”
Given 打开网址 http://www.baidu.com
And 输入关键字: selenium
When 点击按钮
Then 搜索结果: selenium出现
3、根据feature编写steps
3.1、junit执行feature
步骤:
3.1.1 、创建TestRunnerWithJunit类,使用junit执行feature,根据提示补充steps
@RunWith(Cucumber.class)
@CucumberOptions(
features = "./src/test/resources/Features"
,glue = {"stepDefinition"}
)
public class TestRunnerWithJunit {
}
3.1.2 、使用junit运行,复制结果提示,补充step
在stepDefinition下创建类xxSteps
使用标注的方式把用例信息通过正则表达式获取关键字,并传入被标注方法的参数中
@Given("^打开网址: (.*)")
public void open(String url){
driver.get(url);
}
@When("^输入关键字: (.*)")
public void sendkey(String key){
driver.sendkey(..(key));
}
@Then("^搜索结果: (.*)出现")
public void result(String expected){
Assert.as(..);
}
4、使用selenium操作页面
步骤:
4.1、设置浏览器、驱动,注意版本
4.2、操作浏览器打开页面
4.3、定位元素,并操作
5、selenium操作结合xxSteps完成用例执行步骤
6、po模式
页面元素、操作和用例分离
7、结果报告
@CucumberOptions(中设置
plugin = {"pretty","json:target/cucumber-report/json-files/cucumber.json","html:target/cucumber-report/cucumber.html"}
,monochrome = true
最后
以上就是温暖过客为你收集整理的Cucumber+Junit+Selenium框架搭建1、创建maven项目2、编写场景3、根据feature编写steps4、使用selenium操作页面5、selenium操作结合xxSteps完成用例执行步骤6、po模式7、结果报告的全部内容,希望文章能够帮你解决Cucumber+Junit+Selenium框架搭建1、创建maven项目2、编写场景3、根据feature编写steps4、使用selenium操作页面5、selenium操作结合xxSteps完成用例执行步骤6、po模式7、结果报告所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复