Cucumber, how to write this line - ruby

I did some selenium-webdriver ruby code. It works, but now I want to use cucumber to take advantage of the html report that looks good with green and red colors. When I started to use some code in the file called custom_steps.rb, it did not work.
Can you please tell me how to write this line in this file:
require 'selenium-cucumber'
# Do Not Remove This File
# Add your custom steps here
# $driver is instance of webdriver use this instance to write your custom code
divs = $driver.find_elements(:xpath,"//div[#class='col-xs-12 check-box-container']/a/div");
How is the code from custom_steps.rb called in .feature file?

It is not called by that file, your step definition interprets meaning from the .feature file.
In your step definition file you need to write regular expressions to match the lines in the .feature file.
Here is an example of the syntax in action from dispora, a large rails app with many cucumber tests https://github.com/diaspora/diaspora/blob/21980681b156bc163c616cf0344bcdf3f3a195db/features/step_definitions/search_steps.rb.
Also see https://github.com/cucumber/cucumber-ruby

Related

Regex for Ruby code extraction out of plain text?

I want to extract ruby code snippets out of plain text.
Using the gem https://github.com/Erol/yomu makes it possible to extract the text of a PDF document. Now I want to get just the well-formed ruby code out of, for instance, a ruby-programming-book.
Any idea how a regex for multi-line matches of ruby methods and classes could look like?
I tried many different expressions, but did not get the results, that I expected.
Try this
Go through the file line by line and try to parse each line as Ruby code
If a line parses as Ruby start adding more lines to it until they don't parse as Ruby code anymore
Voila, here is your first code snippet
Maybe apply some filter to exclude trivial snippets like single words
Repeat
This is the common best practice to extract source code from unstructured text like emails and what not. This has been used to scan millions of emails for research projects.
Use the ripper core library to parse Ruby code.

How to write any variable created in a test to the jasmine-reporters output file using Protractor?

In parts of my test, I have some variables I dynamically create that simply capture some strings. I have jasmine-reporters set up and working, and writing to an output.xml file. How do I get any variables I create in my tests to write to that output file?
For example, if I do a search in my test, the results display number of lines in a string as part of what's returned. I do a getText() on that and store in a variable. I have figured out how to write to console, but it would be great to get it to write to the output file instead.
Yes like #bloveridge mentioned, jasmine does not allow you to add data from the tests into the report, and you should not try to do it as it's not the concern of the test reporter. If you want to use protractor to collect some kind of information while testing, you should write into your own (i.e. separate) file (http://nodejs.org/api/fs.html) in your test.

Can a Cucumber feature pass a constant to a step definition?

I have a library of XPATHs for a site whose XPATHs regularly change. I've written it because instead of going through every feature file and changing the XPATH it sends, I can simply change the value of the variables I have within my .rb library.
Is it possible to pass these constants to step definitions through the .feature file?
Example .feature feature file:
Scenario: I want to test a button
When I go to url "blah"
And I click on the XPATH: XPATH_CONSTANT_VARIABLE
Example .rb step definition:
When /^I click on the XPATH: {I DON'T KNOW WHAT TO PUT HERE}$/ do |path|
#driver.find_element(:xpath, path).click
end
Example XPATH .rb library:
XPATH_CONSTANT_VARIABLE = "//*[#id="blahblah"]/div[1]/div/div[2]/div/div[1]/div/div[5]/div/div/div/div[2]"
Your scenarios are very imperative. I advice you to make them more declarative and don't use (or refer) to XPathes in scenarios. Read:
You're Cuking It Wrong
Imperative vs Declarative Scenarios in User Stories
If you really want to leave your scenarios as they are, you can use:
When /^I click on the XPATH: \w+$/ do |constant|
xpath = Kernel.const_get constant
#driver.find_element(:xpath, xpath).click
end
But putting all constants to global space as you did seems ugly to me. It may be better to put them to YAML file.

How can you get vim to add a header comment to new files?

I write a lot of Rails apps these days and would like to have vim add header comments to all the code I work on..
I tend to store my projects in
~/Development/Repos/Personal
And
~/Development/Repos/Work
Can I get vim to use different copyrights etc based on where abouts the file is being created?
You can just save a header template as a plain text file and read it into a new file with :read. As for checking the path, just write a Ruby script to produce the desired text and invoke it with :read!. Creating a true vim plugin is also an option. However, why waste time learning a new language and API when you already know how to deal with text and paths in Ruby? Although, a bash script would create even less friction if you are comfortable with it.
I suggest you to use one of the many snippet plugins, like XPTemplate or snipMate, to create a 'header' snippet and then use it. The force of these plugins is that you just have to type a word and then press tab to get the expanded snippet.
Here's a snippet from my vimrc which puts in boilerplate when I create a file named test_something.rb. You can probably use a similar autocmd to conditionally add the copyright you desire. You may have to check for the expanded path in the function, but it seems doable with some vimscripting.
" Autocommands
autocmd BufNewFile *test*.rb call MakeRubyUnitTester()
"
" Functions
" Fill in the boilerplate for Ruby Unit Tests
function! MakeRubyUnitTester()
exec "normal irequire 'test/unit'
class TC_Simple < Test::Unit::TestCase"
endfunction

Cucumber: Automatic step file creation?

When i run cucumber it displays the
possible steps that i should define, an example from the RSpec book:
1 scenario (1 undefined)
4 steps (4 undefined)
0m0.001s
You can implement step definitions for undefined steps with these snippets:
Given /^I am not yet playing$/ do
pending
end
When /^I start a new game$/ do
pending
end
Then /^the game should say “Welcome to CodeBreaker”$/ do
pending
end
Then /^the game should say “Enter guess:”$/ do
pending
end
Is there a way that it will automaticly create the step definitions file, so i don't have to
rewrite or copy paste by hand but i can just to customize them to be more generic?
Cucumber doesn't offer this feature. Probably because you would have to tell it where to put the step definitions file, and what to name it.
Like Kevin said, Cucumber would have to know the name of the file to put it in, and there are no good defaults to go with, other than using the same file name as the feature file. And that is something I consider an antipattern: http://wiki.github.com/aslakhellesoy/cucumber/feature-coupled-steps-antipattern
Intellij Idea or RubyIDE does exactly what you are asking for:
Detects missing step definitions
Creates missing step definitions in a new file (you choose the name of the file) or in one of the existing step definition files
Highlights matched step parameters
see http://i48.tinypic.com/10r63o4.gif for a step by step picture
Enjoy
There is a possibility this kind of feature could be useful, but as Kevin says, it doesn't exist at present. But it could also get quite messy, quite quickly.
Maybe you already do this, but there's nothing stopping you cut and pasting the output direct into your text editor, or even piping the output direct to your text editor if you're so inclined. Then at least you're getting pretty much most of the way there, bar creating the file and naming.
try this https://github.com/unxusr/kiwi it auto generate your feature file and make the step definitions file for you and you just fill in the steps with code.
In later version will write the code of the steps and run the test all of that automagically
You can use a work around way to generate steps file
all you have to do is to run the Cucumber on a feature doesn't have defined steps by identify a specific feature as the following command:
1) using path
bundle exec cucumber {PATH}
note path would start with features/....
for example
features/users/login.feature
1) using tags
bundle exec cucumber --tags=#{TAG}
note tag should be above your scenario in the steps file
for example
#TAG
Scenario:
And you will have the suggested steps in the console with pending status

Resources