Cucumber no output screen - ruby

I'm pretty new to cucumber automated testing. When I first started, a co-worker had set up everything for me on my computer, and (now that he's gone) I've run into a problem that I can't seem to figure out.
I'm using cucumber to test a web application. In the past when I run the script, an internet explorer pops up, and I can see each line of the script being executed.
I recently had to reinstall cucumber, ruby, watir, etc., and that internet explorer screen no longer pops up.
I installed Ruby 1.9.3, cucumber (gem install cucumber), watir (gem install watir). Am I missing something? Is it an extra plug in? The script still runs. However, instead of taking say 1 min + to run a 320 step script, it now takes 1.5 seconds. There are no error messages. When run from the command window, it literally looked like it just scrolled through the script instead of going through each step.
What is the pop up screen called anyways? A scenario screen? Output screen?
It was really difficult for me to look it up on google because I had no idea how to refer to that screen.
Any help is appreciated. and I realize I might not have described the problem well enough. Just leave a comment, and I can try to clarify it more.
Feature:
'To go to a webpage'
Scenario:
# ----------
# GO TO PAGE
# ----------
Given that I have gone to the Login page at "url"
#
# ----------
# LOG IN
# ----------
When I add "username" to the Username
When I add "password" to the Password
And click the Login button
Then "Welcome" should be mentioned on the page
script definitions:
require "rubygems"
require "watir"
puts "Browser is running..."
END {
puts "Closing browser..."
}
BEGIN {
puts "Starting browser..."
}
Given /^that I have gone to the Login page at "(.*)"$/ do |item|
#browser = Watir::IE.start(item)
lnk_found = 0
#browser.links.each do |lnk|
if lnk.id.to_s.matches("overridelink")
lnk_found += 1
end
end
if lnk_found > 0
#browser.link(:id, "overridelink").click
end
# puts "Watir Version: #{Watir::IE::VERSION}"
#browser.maximize
end
#
#
#
When /^I add "(.*)" to the Username$/ do |item|
#browser.text_field(:name, "loginName").set(item)
end
#
#
When /^I add "(.*)" to the Password$/ do |item|
#browser.text_field(:name, "passwd").set(item)
end
#
#
#
Then /^"(.*)" should be mentioned on the page$/ do |item|
if #browser.text.include?(item)
# puts "TEST PASSED. FOUND >#{item}<"
else
puts "*** TEST FAILED ***. >#{item}< was not found."
end
end
Directory Structure
Cucumber
Testing
lib
login.rb
login.feature

I don't know what exactly fixed it but after taking suggestions and advices from different people I basically uninstalled and reinstalled watir several times.
I also updated the code, took out "require 'rubygems'" and replaced Watir::IE.start with Watir::Browser.start.
It seems like the problem was the existing scripts I worked with was based off of older versions of watir/cucumber/ruby, so when I had to reinstall everything, the scripts were no longer compatible with newer versions of everything.

Related

How do you use cucumber with zeus?

When I start up zeus, it does not offer zeus cucumber as one of the possible commands. Others seem to get this by default; At least I have seen a couple zeus write-ups that show the output from zeus start including zeus cucumber, and they don't say anything about that having been special or required extra configuration.
I don't really even know where to start to troubleshoot this; I have googled and searched here for "use cucumber with zeus." I get no setup discussions. The only results I get are from people who seem to take for granted that it should be there, and are investigating problems with it not functioning correctly.
You should use this custom plan file from Zeus. Save it as custom_plan.rb at the root of your application:
require 'zeus/rails'
# 1. Add the cucumber methods (below) to your custom plan (or take this file if
# you don't have an existing custom_plan).
#
# 2. Add the following line to the test_environment section of your zeus.json:
#
# "cucumber_environment": {"cucumber": []}
class CucumberPlan < Zeus::Rails
def cucumber_environment
::Rails.env = ENV['RAILS_ENV'] = 'test'
require 'cucumber/rspec/disable_option_parser'
require 'cucumber/cli/main'
#cucumber_runtime = Cucumber::Runtime.new
end
def cucumber(argv=ARGV)
cucumber_main = Cucumber::Cli::Main.new(argv.dup)
had_failures = cucumber_main.execute!(#cucumber_runtime)
exit_code = had_failures ? 1 : 0
exit exit_code
end
end
Zeus.plan = CucumberPlan.new

Cucumber ./features/step_definitions/calculator_steps.rb:15 in '/^the calculator is run$/

I want to make it clear this problem is not about backticks. I am new to Cucumber and am trying to run the code from second chapter of The Cucumber Book: Behavior-Driven Development for Testers and Developers. I am using Cucumber 1.3.19 with ruby 1.9.3p551 in Windows 7 with Ansicon x64 1.60. Cucumber works fine with other code I have received from others, so the configuration is good. I have read several posts about problems with this tutorial in regards to the backticks already; however, I have copied the code directly from the Cucumber Book website with the correct backticks (not single quotes) and am still getting the error.
Command failed! <RuntimeError>
./features/step_definitions/calculator_steps.rb:15 in '/^the calculator is run$/
features\adding.features:5:in 'When the calculator is run'
Failing Scenarios:
cucumber features\adding.feature: 3
My code for adding.feature looks like
Feature: Adding
Scenario: Add two numbers
Given the input "2+2"
When the calculator is run
Then the output should be "4"
My code for the features/step_definitions/calculator_steps.rb looks like:
Given /^the input "([^"]*)"$/ do |input|
#input = input
end
When /^the calculator is run$/ do
#output = `ruby calc.rb #{#input}`
raise('Command failed!') unless $?.success?
end
Then /^the output should be "([^"]*)"$/ do |expected_output|
#output.should == expected_output
end
Is there something with the newer version of Cucumber that could render this code bad?
Thanks for the help. I figured the problem out. calc.rb needed to be in the root directory which was above features and I was putting it in the step_definitions :P.

Ruby - Error: "unexpected keyword_end, expecting $end"

First off, I'm new to ruby so I apologize if I did something that's not conventional. I am just trying to run a simple script that interacts with twitter. It works perfectly fine on my macbook but when I try to run it on my raspberry pi I get this error: "unexpected keyword_end, expecting $end." I found posts involving this error, but I didn't feel like the answers helped me.
It's mainly just throwing me off because it's not happening on my mac.
This is the script (or at least a portion of it) I'm having problems with:
#encoding: UTF-8
require 'tweetstream'
require 'rubygems'
require 'oauth'
require 'json'
puts TweetStream::VERSION
c_key = 'xxxx'
c_secret = 'xxxx'
oa_token = 'xxxx'
oa_token_secret = 'xxxx'
TweetStream.configure do |config|
config.consumer_key = c_key
config.consumer_secret = c_secret
config.oauth_token = oa_token
config.oauth_token_secret = oa_token_secret
config.auth_method = :oauth
end
consumer_key = OAuth::Consumer.new( c_key, c_secret)
access_token = OAuth::Token.new( oa_token, oa_token_secret)
client = TweetStream::Client.new
keywords = ['word','word']
client.userstream do |status|
if keywords.any? {|str| status.text.downcase.include? str} && status.user.id.to_s != '11111111111'
unless status.text[0,4].include? 'RT #'
puts "#{status.id}: #{status.text}\n"
end
end
end
Any help or guidance would be much appreciated!
Okay so I just copied over my code from my other computer again and it's working fine. I have no clue what happened because I never really fiddled with it when I brought it over the first time. Thanks for the help though!
How to find elusive syntax errors in Ruby
I'm not going to tell you where your syntax error is, but I'll tell you how to find out.
Start commenting out code. This is easiest done with an editor that allows you to highlight a block of code and comment it out with one command (for example, in Emacs, c-C c-C toggles whether or not the highlighted block is commented-out). Start with the part of your code where you most recently made a modification (if you're not sure, check your version control system to see what you've changed). Pick some part of it--perhaps half, to comment out. Be sure that you're commenting out belongs together--for example, don't comment out half of a while loop--comment out the whole thing.
Now load your program. Do you still get the syntax error? If not, you've just identified where it must be. Take some of the code you commented out, and un-comment it out. If so, find another block of code and comment it out.
Lather, rinse repeat.
You don't have to worry about whether your program runs -- if it loads without a syntax error, it will no doubt fail because of all the commented out code. All you're doing is finding out where the syntax error is.
Funny story....I had the same error before and it was driving me NUTS for like an hour. No kidding. I refused to give up. I just could not figure it out! Why the error? My code was on-point.
And then....I happen to hit the scroll-down bar only to see that there was already an "end" inserted below like 2 lines down!! I couldn't see it on the screen. There was an extra!!! Smh...
Once I took it out...my "bundle exec rake test" passed. Sometimes it's the little things :)
change
unless status.text[0,4].include? 'RT #'
puts "#{status.id}: #{status.text}\n"
end
to:
if !status.text[0,4].include? 'RT #'
puts "#{status.id}: #{status.text}\n"
end
or:
puts "#{status.id}: #{status.text}\n" unless status.text[0,4].include? 'RT #'
unless something is equivalent to if !something
unless does not need an end for it.

how to run capybara sinatra

I was given a sample sinatra project with a hello world for capybara testing in akephalos. I understand the concept by looking at the code, but how do i run it? If I run rackup config.ru, and then go to :9292 I just see a hello world. Great, what is that telling me? How do I run the test? The project is bare bones, but below is a file called example_spec.rb. How can I see it fail, for example by looking for "Hi world" and watching it fail? Hope this is enough info. Thought I would check here before I ask the dude that supplied me with the test, thanks!
# describe and context blocks are optional but help organize things
describe 'the index page' do
include x
# :js => true is used to run the test in Firefox. Otherwise it runs headless
# and without JS support
it 'can view the index page', :js => true do
visit '/'
# check to see if the page has the following text (ignoring tags)
page.should have_content('Hello, world!')
# visit https://github.com/jnicklas/capybara to see a complete list of
# assertions
end
You need to set Capybara.app = <your Sinatra class>. Perhaps something like this:
setup do
Capybara.app = Main
end
bundle exec rspec spec, This means run "bundle exec rspec" on the "spec" directory

How to upload a file with watir and IE?

I am writing a watir script to test an upload form.
But the script does not automatically choose the file that is to be uploaded from my harddrive.
Instead IE stops with the file chooser dialog open. As soon as I manually select the to be uploaded file in the dialog and click ok, watir continues as desired. I wonder why it stops.
This is my watir script:
require 'test/unit'
require 'watir'
# runs on win3k, IE 6.0.3790; ruby 1.8.6, watir
class EpcHomePage < Test::Unit::TestCase
def test_upload
ie = #browser
htmlfile = "C:\\testing\\upload.html"
uploadfile = "C:\\testing\\upload.html"
ie.goto(htmlfile)
ie.file_field(:name,"file1").set(uploadfile)
assert_equal uploadfile, ie.file_field(:name,"file1").value
ie.button(:name, 'upload').click
end
def setup
#browser = Watir::IE.new
end
def teardown
#browser.close
end
end
I got the code from this page: http://wiki.openqa.org/display/WTR/File+Uploads
This is the form:
<html><body>
<form name="form1" enctype="multipart/form-data" method="post" action="upload.html">
<input type="file" name="file1">
<input type="submit" name="upload" value="ok">
</form>
</body></html>
I have found this manual http://svn.openqa.org/svn/watir/trunk/watir/unittests/filefield_test.rb also. I am using IE 6 and also IE 7for the testing.
Edit: I have uploaded my simple example here (3 files that live in c:\testing\ on my machines, just start the cmd file):
http://dl.dropbox.com/u/1508092/testing.rar
It fails on 3 different machines (all windows 2003, 2x IE 6 and 1 x IE 7). I have also changed the sleep time in the script c:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.5\lib\watir\input_elements.rb from 1 second to 5 seconds, like suggested by Željko Filipin in his answer:
def set(path_to_file)
assert_exists
require 'watir/windowhelper'
WindowHelper.check_autoit_installed
begin
Thread.new do
sleep 5 # it takes some time for popup to appear
system %{ruby -e '
...
This is where it stops (please note that I did manually navigate to the directory in the file dialog once. From that point on IE always shows the open dialog with this directory, but that does not mean that the script selected the directory. I think it means that IE always shows the last directory where it left):
this is where it stops http://dl.dropbox.com/u/1508092/upload-dialog.JPG
Edit:
I found that that the ole32 code looks for the english title:
POPUP_TITLES = ['Choose file', 'Choose File to Upload']
I installed IE 7 english version now. Still no success. But I think it has something to do with the localization, because input_elements.rb searches the window titles. I wonder why it still fails now. This is the code from input_elements.rb:
class FileField < InputElement
INPUT_TYPES = ["file"]
POPUP_TITLES = ['Choose file', 'Choose File to Upload']
# set the file location in the Choose file dialog in a new process
# will raise a Watir Exception if AutoIt is not correctly installed
def set(path_to_file)
assert_exists
require 'watir/windowhelper'
WindowHelper.check_autoit_installed
begin
Thread.new do
sleep 2 # it takes some time for popup to appear
system %{ruby -e '
require "win32ole"
#autoit = WIN32OLE.new("AutoItX3.Control")
time = Time.now
while (Time.now - time) < 15 # the loop will wait up to 15 seconds for popup to appear
#{POPUP_TITLES.inspect}.each do |popup_title|
next unless #autoit.WinWait(popup_title, "", 1) == 1
#autoit.ControlSetText(popup_title, "", "Edit1", #{path_to_file.inspect})
#autoit.ControlSend(popup_title, "", "Button2", "{ENTER}")
exit
end # each
end # while
'}
end.join(1)
rescue
raise Watir::Exception::WatirException, "Problem accessing Choose file dialog"
end
click
end
end
The text "Choose file" now appears in the title of my new IE. Anything else that should be localized or changed here? I updated the screenshot to the english version.
I knew about that problem, and completely forgot! Go to input_elements.rb file in your gems directory, and add the title of the file upload window in your language to POPUP_TITLES (line 443).
Example:
before
POPUP_TITLES = ['Choose file', 'Choose File to Upload']
after
POPUP_TITLES = ['Choose file', 'Choose File to Upload', 'File upload in my language']
I installed windows xp in english now, and it works! (The error occured on a localised windows server 2003)
I guess it was the localisation issue. I will just run watir on the english computer from now on.
I had the same problem today (March1,2012) and landed here via Google.
Thanks Željko for pointing me in the right direction, however the solution of changing the [POPUP_TITLES] didn't work. In fact, this array seems not to exist anymore in the current version of the gem (watir-2.0.4), or maybe I just misread.
I solved the problem in watir-2.0.4/lib/watir/dialogs/file_field.rb:
Here, the various window and button titles are defined as regular expressions. Change the regexps in the following methods
open_button()
cancel_button()
file_upload_window()
to match your localized window names. After reloading the gem, it worked flawlessly.
I would suggest that you take a look at FileField#set in input_elements.rb (in your Ruby gems directory), and change sleep 1 to sleep 2 (or some higher number). I have noticed that on slower machines it takes more than a second for file upload pop up to appear.
#modal = #browser.driver.switch_to.alert #Switch to open windows modal
key_to_send = "C:\\Users\\singhku\\Calabash_doc.pdf" #Path and name of file
#modal.send_keys(key_to_send)
require 'win32ole'
wsh = WIN32OLE.new('Wscript.Shell')
wsh.AppActivate('Choose File to Upload') #Name of the modal that is open
wsh.SendKeys('{ENTER}')

Resources