创建你的第一个 Cucumber 脚本(2 个示例)
在本教程中,我们将创建 Cucumber 脚本来测试两个场景
Cucumber 脚本 1:两个数字相乘
步骤 1) 通过 Windows 开始菜单打开 RubyMine 编辑器
步骤 2) 在 Rubymine 编辑器中,点击“创建新项目”
步骤 3) 选择项目位置并点击“创建”。
步骤 4) 创建文件目录
步骤 5) 将目录命名为“features”
步骤 6) 在“你的文件夹/features/”中创建并保存文件,文件名为“你的文件名.feature”
步骤 7) 为了执行我们的场景,将以下程序保存在 Feature 文件中
代码
Feature: Multiplication I multiply two numbers Scenario: multiply a and b Given I have variable a And I have variable b When I multiplication a and b Then I display the Result
步骤 8) 现在让我们运行我们的第一个 feature 文件!
点击“启动带有 Ruby 的命令提示符”
你得到的输出是
你看到了错误,因为你必须为 feature 文件编写步骤定义文件
步骤 9) 让我们为我们的 Feature 文件创建步骤定义文件!
在 Rubymine 编辑器中创建一个名为“step_definition”的新文件夹
步骤 10) 在“你的文件夹/features/step_ definitions”中保存文件,文件名为 test_step.rb
步骤 11) 将以下代码写入步骤文件
代码
Given(/^I have variable a$/) do @a = 50 end And(/^I have variable b$/) do @b = 70 end When(/^I multiplication a and b$/) do @mul = @a * @b end Then(/^I display the Result$/) do puts "Multiplication of #{@a} and #{@b} is #{@mul}" end
步骤 12) 现在,再次运行我们的 feature 文件
结果是
Cucumber 脚本 2:验证输入或未输入电子邮件 ID 时的输出
在此示例中,我们使用Ruby
测试场景:验证未输入电子邮件 ID 时的输出
测试步骤
- 打开浏览器
- 访问https://demo.guru99.com/
- 不输入电子邮件 ID
- 点击提交
测试场景:验证输入电子邮件 ID 时的输出
测试步骤
- 打开浏览器
- 访问https://demo.guru99.com/
- 输入电子邮件 ID
- 点击提交
Feature 文件中的代码
Feature: guru99 Demopage Login To Login in Demopage we have to enter login details Scenario: Register On Guru99 Demopage without email Given I am on the Guru99 homepage When enter blank details for Register Then error email shown Scenario: Register On Guru99 Demopage with valid email Given I am on the Guru99 homepage When enter details for Register Then login details shown
步骤定义文件中的代码
require 'watir-webdriver' require 'colorize' browser = Watir::Browser.new Given (/^I am on the Guru99 homepage$/)do browser.goto "https://demo.guru99.com" end When (/^enter blank details for Register$/)do browser.text_field(:name,"emailid").set(" ") browser.button(:name,"btnLogin").click end Then (/^error email shown$/)do puts " Email is Required".red browser.close end When (/^enter details for Register$/)do browser = Watir::Browser.new browser.goto "https://demo.guru99.com" browser.text_field(:name,"emailid").set("guru99@gmail.com") browser.button(:name,"btnLogin").click end Then (/^login details shown$/)do puts " Sucessfully register" browser.close end
在命令提示符中运行代码,你将得到