Cucumber之四新建一个Cucumber项目

教程视频地址 是百度网盘,后续会放到微信公众号:【软测小生】里面,请关注公号更新相关文章和视频资源。
Cucumber之四新建一个Cucumber项目_第1张图片


本文目录

  • 新建Maven项目
  • 新建Selenium测试脚本
  • 转换为feature文件
  • 运行测试Cucumber
  • 新建 stepDefinitions类
  • 未完待续……

上一篇文章说到安装Eclipse插件,接下来创建一个Cucumber项目。

新建Maven项目

首先,创建一个Maven项目, 在pom文件里面导入相应的jar包。
Selenium-java 3.14.0
cucumber-java 4.3.1
cucumber-core 4.3.1
cucumber-testng 4.3.1
cucumber-junit 4.3.1
junit 4.12
testng6.13

Cucumber之四新建一个Cucumber项目_第2张图片
pom文件如下:
注意
你知道Maven GroupID: info.cukes和io.cucumber之间的区别吗?
info.cukes 为Cucumber1.2.5以下的版本
io.cucumber为 Cucumber 2.0.0以后的版本

之所以Cucumber在2.0.0之后更换了Maven 的group ID,是因为gherkin同样也更换了group ID。

有关更多细节,请参阅发布说明:https://github.com/cucumber/cucumber-jvm/blob/master/CHANGELOG.md.
黄瓜2已经发生了很大的变化。更多信息请参考这个-https://cucumber.io/blog/2017/08/29/announcing-cucumber-jvm-2-0-0

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.selenium.automation</groupId>
	<artifactId>myCucumber</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>myCucumber</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

<!-- 以下是2.0.0以上版本 groupId=io.cucumber -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>4.3.1</version>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
			<version>4.3.1</version>            
            <scope>test</scope>
        </dependency>
		<dependency>
			<groupId>io.cucumber</groupId>
			<artifactId>cucumber-java</artifactId>
			<version>4.3.1</version>
		</dependency>
		<dependency>
		    <groupId>io.cucumber</groupId>
		    <artifactId>cucumber-testng</artifactId>
		    <version>4.3.1</version>
		</dependency> 
		
<!-- 以下是1.2.5版本 groupId=info.cukes -->
<!--    
			<dependency>
			    <groupId>info.cukes</groupId>
			    <artifactId>cucumber-junit</artifactId>
			    <version>1.2.5</version>
			</dependency>
 		<dependency>
			<groupId>info.cukes</groupId>
			<artifactId>cucumber-java</artifactId>
			<version>1.2.5</version>
			<scope>test</scope>
		</dependency> 
		<dependency>
			<groupId>info.cukes</groupId>
			<artifactId>cucumber-testng</artifactId>
			<version>1.2.5</version>
		</dependency> 
		<dependency>
			<groupId>info.cukes</groupId>
			<artifactId>cucumber-picocontainer</artifactId>
			<version>1.2.5</version>
		</dependency> -->

		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>3.14.0</version>
		</dependency>

		<dependency>
			<groupId>org.checkerframework</groupId>
			<artifactId>checker-compat-qual</artifactId>
			<version>2.5.5</version>
		</dependency>

		<dependency>
			<groupId>com.google.errorprone</groupId>
			<artifactId>error_prone_annotations</artifactId>
			<version>2.3.3</version>
		</dependency>

		<dependency>
		    <groupId>com.beust</groupId>
		    <artifactId>jcommander</artifactId>
		    <version>1.72</version>
		</dependency>
		
	</dependencies>
</project>

提醒:
若返回如下信息,很可能是因为Jar的版本不匹配,你可以将版本改低点。比如4.3.1更改为4.0.0
io/cucumber/datatable/dependency/com/fasterxml/jackson/databind/JavaType

新建Selenium测试脚本

新建一个Selenium的测试脚本,该测试脚本主要是对TestClass进行登录与登出。
1. 启动浏览器
2. 导航到主页
3. 单击LogIn链接
4. 输入用户名和密码
5. 点击提交按钮
6. 打印成功消息
7. 退出应用程序
8. 打印成功消息
9. 关闭浏览器

Selenium测试脚本如下:

package com.selenium.automation.myCucumber;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class cucumberDemo {
	private static WebDriver driver = null;
	
	@BeforeClass
	public void beforeClass(){
		System.setProperty("webdriver.chrome.driver", "resources/driver/chromedriver.exe");
		 //Create a new instance of the Chrome driver
		 driver = new ChromeDriver();
	}
	
	@Test
	public void mainTest(){

        //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        //Launch the TestClass Website
        driver.get("http://www.testclass.net/");

        // Find the element that's href attribute is '/readers/sign_in'(用户登录) 
        driver.findElement(By.xpath(".//*[@href='/readers/sign_in']")).click();

        //Find the element that's ID attribute is 'reader_email' (电子邮箱)
        // Enter email on the element found by the above desc. 输入电子邮箱
        driver.findElement(By.id("reader_email")).sendKeys("[email protected]"); 

        // Find the element that's ID attribute is 'reader_password' (密码)
        // Enter Password on the element found by the above desc.
        driver.findElement(By.id("reader_password")).sendKeys("WOaideren520..,");

        // Now submit the form. WebDriver will find the form for us from the element 
        driver.findElement(By.xpath(".//*[@name='commit']")).click();

        // Print a Log In message to the screen
        System.out.println("Login Successfully");

        // Find the element that's ID attribute is 'navbarDropdown' >> xpath is './/*[@href='/readers/sign_out'(登出)
        driver.findElement (By.id("navbarDropdown")).click();
        driver.findElement (By.xpath(".//*[@href='/readers/sign_out']")).click();

        // Print a Log In message to the screen
        System.out.println("LogOut Successfully");

        // Close the driver
        driver.quit();
	}	
}

要启动测试,只需选择Run > Run As > TestNG Test。几秒钟后,Chrome浏览器将打开,Selenium将启动TestClass主页,进行登录 和登出。

转换为feature文件

接下来把 以上的Selenium测试 转换为Cucumber Feature文件。
在项目中的resources>> feature>>新建 LogIn_Test.feature

# language: en-US
Feature: Login Action

Scenario: Successful Login with Valid Credentials
    Given User is on Home Page
    When User Navigate to LogIn Page
    And User enters UserName and Password
    Then Message displayed Login Successfully

Scenario: Successful LogOut
    When User LogOut from the Application
    Then Message displayed LogOut Successfully
# language: zh-CN
功能: 登录操作

场景: 使用有效凭证成功登录
    假如 用户在主页上
    当 用户导航到登录页
    而且 用户输入用户名和密码
    那么 登陆成功的消息显示
    
场景: 登出成功
    当 用户从应用程序登出
    那么 登出成功的消息显示

Feature(功能):定义您将在下面的测试中测试的特性
Given(假如):测试的前提条件
And(和):定义测试的附加条件
Then(然后):声明条件。你可以说这是预期的测试结果。

运行测试Cucumber

现在我们已经定义了测试,现在是运行测试的时候了。
新建一个TestRunner的类如下:
并且给feature文件中的加上了@tag注解

package com.selenium.automation.myCucumber;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import cucumber.api.junit.Cucumber;

//@RunWith(Cucumber.class)//这个是使用Junit来运行的
@CucumberOptions(features={"src/test/resources/features/LogIn_Test.feature"},
				glue="com.selenium.automation.stepDefinitions",
				plugin={"pretty"},
				tags={"@Login", "@tag1"})
public class TestRunner extends AbstractTestNGCucumberTests{
	
}

要启动测试,只需选择Run > Run As > TestNG Test。会返回如下的信息:
Cucumber之四新建一个Cucumber项目_第3张图片
按照上面的提示“You can implement missing steps with the snippets below:(你可以用下面的代码片段来实现缺少的步骤:)”

新建 stepDefinitions类

接下来我们新建一个package和一个Setp 类。
com.selenium.automation.stepDefinitions>> StepDefinitions.java
Cucumber之四新建一个Cucumber项目_第4张图片

package com.selenium.automation.stepDefinitions;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class StepDefinitions {
	@Given("^User is on Home Page$")
	public void User_is_on_Home_Page() {
		// Write code here that turns the phrase above into concrete actions
		System.out.println("^User is on Home Page$");
	}

	@When("User Navigate to LogIn Page")
	public void User_Navigate_to_LogIn_Page() {
		// Write code here that turns the phrase above into concrete actions
		System.out.println("^User Navigate to LogIn Page$");
	}

	@When("User enters UserName and Password")
	public void User_enters_UserName_Password() {
		// Write code here that turns the phrase above into concrete actions
		System.out.println("^User enters UserName and Password$");
	}

	@Then("Message displayed Login Successfully")
	public void Message_displayed_Login_Successfully() {
		// Write code here that turns the phrase above into concrete actions
		System.out.println("^Message displayed Login Successfully$");
	}
}

然后再运行一遍,显示运行成功,截图如下:
Cucumber之四新建一个Cucumber项目_第5张图片
接下来的工作就是针对StepDefination进行一系列的定义以及验证了

未完待续……

可以参考Cucumber之五Step Definition

你可能感兴趣的:(#,Cucumber)