Selenium - Cucumber (BDD 框架)


在本教程中,您将学习如何集成 Cucumber - Selenium 网络驱动程序。

什么是 Cucumber?

Cucumber 是一种支持行为驱动开发(BDD)的测试方法。它使用 Gherkin 语言以简单的英文文本解释应用程序的行为。

了解更多信息请访问 – https://www.guru99.com/cucumber-tutorials.html

什么是 Selenium?

Selenium 是一种自动化工具 功能测试 基于 Web 的应用程序。 Selenium 支持不同的语言,如 java、ruby、python C# 等。

了解更多信息请访问 – https://www.guru99.com/selenium-tutorial.html

为什么使用 Cucumber - Selenium?

Cucumber 和 Selenium 是两种流行的技术。

大多数组织使用 Selenium 进行功能测试。这些组织正在使用 Selenium,想要整合 Selenium - Cucumber as Cucumber & 它易于阅读和理解应用程序流程。

Cucumber 该工具基于行为驱动开发框架, 充当桥梁 下列人员之间:

  1. 软件工程师和业务分析师。
  2. 手动测试仪和自动化测试仪。
  3. 手动测试员和开发人员。

Cucumber BDD 框架也 有利于客户理解应用程序代码 因为它使用纯文本的 Gherkin 语言。组织中的任何人都可以理解软件的行为。Gherkin 的语法是简单的文本,易于阅读和理解。

Cucumber - Selenium

使用前提条件 Cucumber - Selenium

开始之前 Cucumber Selenium 集成,我们需要以下项目:

  • Selenium jar文件:
  • Selenium-服务器独立

可从以下网址下载 http://www.seleniumhq.org/download/

使用前提条件 Cucumber - Selenium

Jar 文件 Cucumber :

  • Cucumber-core
  • Cucumber-html
  • cobertura 代码覆盖率
  • Cucumber-java
  • Cucumber-junit
  • Cucumber-jvm-依赖项
  • Cucumber-reporting
  • 汉姆克雷斯特核心
  • 小黄瓜
  • 朱尼特

可从以下网址下载 https://mvnrepository.com/search?q=Cucumber

您需要逐个搜索文件并下载。

例如,我们将向您展示如何下载其中一个 jar 文件,即“Cucumber-核。”

单击上面的下载链接。它会重定向到以下网站。现在搜索特定的 jar,即'Cucumber 核心’如下图所示:

使用前提条件 Cucumber - Selenium

在下一页中,单击版本 1.2.2,

使用前提条件 Cucumber - Selenium

在下一个屏幕中,单击下载以获取'Cucumber 核心’ jar 文件。

使用前提条件 Cucumber - Selenium

备注:为了您的方便,我们捆绑了需要从 Maven 下载的 jar 文件 点击这里。这些 jar 可能会随着时间推移而更新,变得不兼容。请使用上面说明的方法下载它们。

使用自动化测试 Cucumber - Selenium.

让我们研究一下使用步骤 Cucumber 一步步使用 selenium。这里我们将介绍 3 种场景:

  • 场景一:在控制台中打印文本。
  • 场景二:输入登录凭证并重置值。
  • 场景 3:在 Guru99 上输入登录凭据并重置值。对 3 组数据执行此操作。

场景一:在控制台中打印文本。

在这种情况下,我们只需使用以下命令在控制台中打印文本 Cucumber.

步骤1) 在 eclipse 中创建项目。

创建 Java 项目名称为“Cucumber通过Selenium”如下面的屏幕截图所示。

使用自动化测试 Cucumber - Selenium

使用自动化测试 Cucumber - Selenium

步骤2) 在项目中添加 Jar 文件。

右键单击项目>选择属性>转到 Java 构建路径。添加之前下载的所有库。

使用自动化测试 Cucumber - Selenium

步骤3) 创建功能文件

要创建功能文件,首先创建功能文件夹,如下面的屏幕截图所示。

使用自动化测试 Cucumber - Selenium

现在输入文件夹名称“Features”并单击“完成”按钮。

使用自动化测试 Cucumber - Selenium

使用自动化测试 Cucumber - Selenium

现在,在“Features”文件夹中创建名为“MyTest.feature”的功能文件——过程类似于创建文件夹

使用自动化测试 Cucumber - Selenium

请注意: 您可能需要安装 Cucumber Eclipse 插件可让此功能正常工作。转到 — 帮助->安装新软件->复制粘贴链接 http://cucumber.github.io/cucumber-eclipse/update-site/ 并安装

步骤4) 写场景。

下面的几行是使用 Gherkin 语言在“MyTest.feature”文件中编写的,如下所示:

Feature: Reset functionality on login page of Application 


Scenario: Verification of Reset button 


Given Open the Firefox and launch the application			


When Enter the Username and Password			


Then Reset the credential			

代码说明

第 1 行) 在这一行中我们编写业务功能。

第 2 行) 在这一行中我们编写一个场景来测试。

第 3 行) 在这一行中我们定义了先决条件。

第 4 行) 在这一行中,我们定义了需要执行的操作。

第 4 行) 在这一行中,我们定义了预期的结果或成果。

步骤5) 编写 selenium testrunner 脚本 Selenium Cucumber 框架设计

这里我们创建“TestRunner”包,然后在其下创建“Runner.java”类文件。

package TestRunner;		

import org.junit.runner.RunWith;		
import cucumber.api.CucumberOptions;		
import cucumber.api.junit.Cucumber;		

@RunWith(Cucumber.class)				
@CucumberOptions(features="Features",glue={"StepDefinition"})						
public class Runner 				
{		

}

在上面 Cucumber Java 示例代码,我们运行 黄瓜测试 通过使用以下注释:

@RunWith() 注释讲述了测试运行器类开始执行我们的测试。

@CucmberOptions() 注释用于为我们的黄瓜测试设置一些属性,如功能文件、步骤定义等。

TestRunner 文件的屏幕截图。

使用自动化测试 Cucumber - Selenium

步骤6) 创建步骤定义脚本。

现在我们在这里创建“StepDefinition”包,然后在其下创建“Steps.java”脚本文件。在这里我们实际上编写了一个 selenium 脚本来执行下面的测试 Cucumber 方法。

package StepDefinition;		

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

public class Steps {				

     
    @Given("^Open the Firefox and launch the application$")				
    public void open_the_Firefox_and_launch_the_application() throws Throwable							
    {		
        System.out.println("This Step open the Firefox and launch the application.");					
    }		

    @When("^Enter the Username and Password$")					
    public void enter_the_Username_and_Password() throws Throwable 							
    {		
       System.out.println("This step enter the Username and Password on the login page.");					
    }		

    @Then("^Reset the credential$")					
    public void Reset_the_credential() throws Throwable 							
    {    		
        System.out.println("This step click on the Reset button.");					
    }		

}

在上面的代码中,创建的类名为“Steps”。 Cucumber 使用annotation来与feature文件进行映射,各个annotation方法定义如下:

@Given 注释定义方法来打开 Firefox 并启动应用程序

@什么时候 注释定义输入用户名和密码的方法

@然后 注释定义方法来重置凭证

在每种方法下,我们只打印一条消息。

下面是“Steps.java”脚本和项目树的屏幕截图,它看起来是这样的。

使用自动化测试 Cucumber - Selenium

请注意: 步骤定义不过是您想要在此 Cucumber 方法下执行的步骤。

步骤7) 执行脚本。

用户可以从测试运行器脚本(即“Runner.java”)执行该脚本,如下面的屏幕截图所示。

使用自动化测试 Cucumber - Selenium

步骤8) 分析输出。

执行“Runner.java”脚本时,它会在控制台上显示文本。它与“Steps.java”脚本中定义的文本相同。

使用自动化测试 Cucumber - Selenium

场景二:输入登录凭证并重置值。

在这里我们只需在 Guru99 演示登录页面上输入 Credential 并重置值

对于场景 2,我们只需要更新“Steps.java”脚本。在这里,我们实际上编写了 selenium 脚本,如下所示。首先,我们需要添加 Selenium 将jar文件添加到该项目。

输入登录凭据并重置值

步骤1) 在这里我们更新“Steps.java”脚本,如下面的代码和屏幕截图所示。

package StepDefinition;		

import org.openqa.selenium.By;		
import org.openqa.selenium.WebDriver;		
import org.openqa.selenium.firefox.FirefoxDriver;		

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

public class Steps {				

    WebDriver driver;			
    		
    @Given("^Open the Firefox and launch the application$")					
    public void open_the_Firefox_and_launch_the_application() throws Throwable							
    {		
       System.setProperty("webdriver.gecko.driver", "E://Selenium//Selenium_Jars//geckodriver.exe");					
       driver= new FirefoxDriver();					
       driver.manage().window().maximize();			
       driver.get("http://demo.guru99.com/v4");					
    }		

    @When("^Enter the Username and Password$")					
    public void enter_the_Username_and_Password() throws Throwable 							
    {		
       driver.findElement(By.name("uid")).sendKeys("username12");							
       driver.findElement(By.name("password")).sendKeys("password12");							
    }		

    @Then("^Reset the credential$")					
    public void Reset_the_credential() throws Throwable 							
    {		
       driver.findElement(By.name("btnReset")).click();					
    }		
}		

上述 selenium 脚本的屏幕截图。

输入登录凭据并重置值

步骤2) 执行脚本。

更新后,我们运行Runner.java。

步骤3) 分析输出。

在输出中你可以看到以下内容:

  • 浏览器已启动。
  • Guru99银行演示网站上线。
  • 用户名和密码放在登录页面上。
  • 重置值。

输入登录凭据并重置值

场景 3:在 Guru99 上输入登录凭据并重置值。对 3 组数据执行此操作。

这里我们需要更新“Step.java”和功能文件。

步骤1) 更新功能文件如下所示:

Here we update the feature file with 'Scenario Outline' and  'examples' syntax.

Feature: Reset functionality on login page of Application				


Scenario Outline: Verification of reset button with numbers of credential


Given Open the Firefox and launch the application				


When Enter the Username <username>and Password <password>				


Then Reset the credential						

Examples:                      		

|username  |password         |		

|User1     |password1        |		

|User2     |password2        |		

|User3     |password3        |

// In this line we define the set of data.

输入登录凭证

步骤2) 现在更新Step.java脚本。

这里我们更新了传递参数的方法,更新后的脚本如下所示:

package StepDefinition;		

import org.openqa.selenium.By;		
import org.openqa.selenium.WebDriver;		
import org.openqa.selenium.firefox.FirefoxDriver;		

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

public class Steps {				

    WebDriver driver;			
    		
    @Given("^Open the Firefox and launch the application$")					
    public void open_the_Firefox_and_launch_the_application() throws Throwable							
    {		
       System.setProperty("webdriver.gecko.driver", "E://Selenium//Selenium_Jars//geckodriver.exe");					
       driver= new FirefoxDriver();					
       driver.manage().window().maximize();			
       driver.get("www.demo.guru99.com/v4");					
    }		

    @When("^Enter the Username \"(.*)\" and Password \"(.*)\"$")			
    public void enter_the_Username_and_Password(String username,String password) throws Throwable 							
    {		
       driver.findElement(By.name("uid")).sendKeys(username);					
       driver.findElement(By.name("password")).sendKeys(password);					
    }		

    @Then("^Reset the credential$")					
    public void	Reset_the_credential() throws Throwable 							
    {		
       driver.findElement(By.name("btnReset")).click();					
    }		
}		

输入登录凭证

步骤3) 现在执行更新后的脚本。

下面的屏幕显示了脚本的成功执行以及每组数据所花费的时间。

输入登录凭证

步骤4) 分析输出。

在输出中你可以看到以下内容:

以下输出根据数据集的数量(即 3 组)重复。

  • 浏览器已启动。
  • Guru99银行演示网站上线。
  • 用户名和密码放在登录页面上。
  • 重置值。

输入登录凭证

总结

Cucumber 是一个非常流行的 BDD 工具。它易于阅读,包括技术人员和非技术人员在内的所有利益相关者都可以理解。

Cucumber 可以与 Selenium 使用以下 3 个步骤

  1. 创建功能文件,使用 Gherkin 语言逐步定义功能和场景。
  2. 创建 Testrunner 文件。在此文件中,我们集成了 Cucumber 使用 BDD 框架 Selenium.我们执行这个脚本。
  3. Creat Step 定义,该包下定义的实际的 selenium 脚本。

阅读更多 readmore