ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 创建您的第一个黄瓜脚本(2 个示例) > 原文: [https://www.guru99.com/your-first-cucumber-script.html](https://www.guru99.com/your-first-cucumber-script.html) 在本教程中,我们将创建黄瓜脚本来测试两种情况 * [黄瓜脚本 1:将 2 个数字相乘](#1) * [黄瓜脚本 2:输入或不输入电子邮件 ID 时验证输出](#2) ## 黄瓜脚本 1:两个数字相乘 **步骤 1)**通过 Windows 开始菜单打开 RubyMine Editor ![](https://img.kancloud.cn/29/55/295571cb9c3f94867ebbf7bcd0a40c47_268x458.png) **步骤 2)**在 Rubymine 编辑器中,单击“创建新项目” ![](https://img.kancloud.cn/17/1f/171ffcc9e9503fbcb41dc13755803fb9_1124x650.png) **步骤 3)**选择项目位置,然后单击“创建”。 [![](https://img.kancloud.cn/de/10/de102274bcbbf6ff9a5e643b1ab28d23_776x481.png) ](/images/1/062718_1022_FirstCucumb3.png) **步骤 4)**创建文件目录 [![](https://img.kancloud.cn/47/3d/473de9fc5df96548697f3660d065f78d_761x584.png) ](/images/1/062718_1022_FirstCucumb4.png) **步骤 5)**将目录命名为**“功能”** [![](https://img.kancloud.cn/63/fd/63fd5b7f37614d2a4af98cb5ec1e116f_797x439.png) ](/images/1/062718_1022_FirstCucumb5.png) **步骤 6)**在名称为“ yourfilename.feature”的“ yourfolder / features /”中创建并保存文件 [![](https://img.kancloud.cn/d2/d6/d2d69668c3f028c291056f11ea36a850_811x532.png) ](/images/1/062718_1022_FirstCucumb6.png) [![](https://img.kancloud.cn/88/23/882341ef693dad51bd249befaab53bd7_778x439.png) ](/images/1/062718_1022_FirstCucumb7.png) **步骤 7)**要执行我们的方案,请将以下程序保存在功能文件中 [![](https://img.kancloud.cn/31/cb/31cba800c3cf1dcb79523c3902635ebf_851x461.png) ](/images/1/062718_1022_FirstCucumb8.png) **代码:** ``` 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)**现在,让我们运行第一个功能文件! 单击“使用 Ruby 启动命令提示符” [![](https://img.kancloud.cn/da/94/da94985b150c0e923ebf4a772043362f_980x258.png) ](/images/1/062718_1022_FirstCucumb9.png) 您得到的输出是 [![](https://img.kancloud.cn/2b/8b/2b8b38f5c27f9fd27c937a547ff07f21_979x512.png) ](/images/1/062718_1022_FirstCucumb10.png) 您看到此错误是因为您必须为功能文件编写步骤定义文件 **步骤 7)**让我们为功能文件创建步骤定义文件! 在 Rubymine 编辑器中创建一个名为“ step_definition”的新文件夹 [![](https://img.kancloud.cn/09/df/09df43ac44e9859b0c6934076663f55d_899x606.png) ](/images/1/062718_1022_FirstCucumb11.png) [![](https://img.kancloud.cn/03/08/03084f25632d3aba9363dc32649faea9_693x307.png) ](/images/1/062718_1022_FirstCucumb12.png) **步骤 8)**将文件另存为“ test.step.rb”中的“ yourfolder / features / step_defines”,如下所示 [![](https://img.kancloud.cn/e6/f3/e6f3eafc8414b4625b6a01de3196d8d5_945x583.png) ](/images/1/062718_1022_FirstCucumb13.png) [![](https://img.kancloud.cn/9b/ae/9baee4468f69986a67734f50917343f8_702x311.png) ](/images/1/062718_1022_FirstCucumb14.png) **步骤 9)**将以下代码写入步骤文件 [![](https://img.kancloud.cn/11/b2/11b2a9fdc4e276e3dd26c02cf3b11a2a_800x480.png) ](/images/1/062718_1022_FirstCucumb15.png) **代码:** ``` 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 ``` **步骤 10)**现在,再次运行我们的功能文件: [![](https://img.kancloud.cn/f6/18/f618e1b4f7d6a55a13609557f7160954_293x458.png) ](/images/1/062718_1022_FirstCucumb16.png) 结果为 [![](https://img.kancloud.cn/89/47/89479a8af6ba2eb5aeb8a23aa112f7d5_818x381.png) ](/images/1/062718_1022_FirstCucumb17.png) ## 黄瓜脚本 2:输入或不输入电子邮件 ID 时验证输出 在此示例中,我们使用 Ruby **测试方案**:未输入电子邮件 ID 时验证输出 测试步骤: 1. 开启浏览器 2. 转到 [http://demo.guru99.com/](http://demo.guru99.com/) 3. **不是**输入电子邮件 ID 4. 点击提交 **测试场景**:输入电子邮件 ID 时验证输出 Test Steps: 1. 开启浏览器 2. 转到 [http://demo.guru99.com/](http://demo.guru99.com/) 3. 输入电子邮件地址 4. 点击提交 Code in Feature File ``` 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 "http://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 "http://demo.guru99.com" browser.text_field(:name,"emailid").set("This email address is being protected from spambots. You need JavaScript enabled to view it. ") browser.button(:name,"btnLogin").click end Then (/^login details shown$/)do puts " Sucessfully register" browser.close end ``` 在命令提示符下运行代码,您将获得 ![](https://img.kancloud.cn/62/4a/624ac8c6f1c205032a3dbfdfb6737f2f_624x369.png)