简体   繁体   中英

How to get javascript with cucumber?

I meet some problems when using cucumber.

Here is my arborescence

-- app1
    |-- features
    |   |-- addition.feature
    |   |-- step_definitions
    |   `-- support
    |       `-- env.js
    |-- public
    |   `-- javascripts
    |       |-- Player.js
    |       `-- Song.js
    |-- Rakefile
    `-- spec
        `-- javascripts
            |-- helpers
            |   `-- SpecHelper.js
            |-- PlayerSpec.js
            `-- support
                |-- jasmine_config.rb
                |-- jasmine_runner.rb
                `-- jasmine.yml

Here is my file "addition.feature"

Feature: Addition
  In order to avoid silly mistakes
  As a math idiot
  I want to be told the sum of two numbers

  Scenario: Add two numbers
    Given I have entered 50 into the calculator
    And I have entered 70 into the calculator
    When  press add
    Then the result should be 120 on the screen

When I run

cucumber features/addition.feature

I get

Feature: Addition
  In order to avoid silly mistakes
  As a math idiot
  I want to be told the sum of two numbers

  Scenario: Add two numbers                     # features/addition.feature:6
    Given I have entered 50 into the calculator # features/addition.feature:7
    And I have entered 70 into the calculator   # features/addition.feature:8
    When press add                              # features/addition.feature:9
    Then the result should be 120 on the screen # features/addition.feature:10

1 scenario (1 undefined)
4 steps (4 undefined)
0m0.003s

You can implement step definitions for undefined steps with these snippets:

Given /^I have entered (\d+) into the calculator$/ do |arg1|
  pending # express the regexp above with the code you wish you had
end

When /^press add$/ do
  pending # express the regexp above with the code you wish you had
end

Then /^the result should be (\d+) on the screen$/ do |arg1|
  pending # express the regexp above with the code you wish you had
end

If you want snippets in a different programming language, just make sure a file
with the appropriate file extension exists where cucumber looks for step definitions.

Why is the output in ruby language, am I not supposed to get javascript, considering I have a ".js" file in "support/" directory?

The solution:

gem install therubyracer

found on http://css.dzone.com/articles/writing-cucumber-step

As far as i know you can use capybara to test javascript. A quick search on google: http://openmonkey.com/2010/04/09/javascript-testing-with-cucumber-capybara/

You might be interested in Cucumber.js . It is the official port of Cucumber to JavaScript.

Keep in mind that it's still missing several features compared to cucumber-ruby (background, tables, tags, etc.).

The scenario you're exposing here should run just fine against cucumber.js, though.

如果您使用的是Windows 10,则可以安装CukeTest。只需在Windows Store中搜索CukeTest,它就可以直观地编辑小黄瓜文件并自动生成JavaScript函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM