| Share

Monday, May 10, 2010

Ruby Sahi with Cucumber

What is Cucumber?
Cucumber lets software development teams describe how software should behave in plain text. The text is written in a business-readable domain-specific language and serves as documentation, automated tests and development-aid - all rolled into one format.
- From http://cukes.info/

Follow the steps below to get started with Ruby Sahi and Cucumber.

  1. Install Java

  2. Install Ruby

  3. Install cucumber:
    gem install cucumber

  4. Install Sahi proxy: Download Sahi from sourceforge and unzip to some location. (say D:\sahi)

  5. Start Sahi:

    cd D:\sahi\userdata\bin

  6. Install Sahi Ruby client:
    gem install sahi

  7. Create a file D:\test\login.feature, add the content below and save it.

    Feature: Login
    In order to access the system
    As a user
    I want to be able to login

    Scenario: Login with valid credentials
    Given I am not logged in
    When I try to login with "test" and "secret"
    Then I should be logged in

    Scenario: Login with invalid credentials
    Given I am not logged in
    When I try to login with "test" and "wrongpassword"
    Then I should not be logged in
    And I should be shown error message "Invalid username or password"

  8. Run this feature:

    cd D:\test\
    cucumber login.feature

    There will be a lot of messages with hints on implementing the right steps.

  9. Implement the steps:

    Create a file D:\test\login.rb, add the content below and save it

    require 'sahi'

    def init_browser()
    #Use the correct paths from your system
    userdata_dir = "D:/sahi/userdata"
    browser_path = "C:\\Program Files\\Mozilla Firefox\\firefox.exe"
    browser_options = "-profile #{userdata_dir}/browser/ff/profiles/sahi0 -no-remote"
    return Sahi::Browser.new(browser_path, browser_options)

    #open the browser at the start
    browser = init_browser()

    #close the browser on exit
    at_exit do

    Given /^I am not logged in$/ do

    When /^I try to login with "([^\"]*)" and "([^\"]*)"$/ do |username, password|
    browser.textbox("user").value = username
    browser.password("password").value = password

    Then /^I should be logged in$/ do
    if !browser.button("Logout").exists?()
    raise "Not logged in"

    Then /^I should not be logged in$/ do
    if !browser.submit("Login").exists?()
    raise "Logged in"

    Then /^I should be shown error message "([^\"]*)"$/ do |msg|
    value = browser.div("errorMessage").text()
    if value != msg
    raise "Incorrect message: #{value}"

  10. Run and watch the tests complete successfully

    cd D:\test\
    cucumber login.feature

  11. Done!


hudson said...

Will this run on jruby also?
We use cuke4duke & Java to write the implementations for cucumber features.

V. Narayan Raman said...

Yes, Ruby Sahi should work with jruby too. There is also a Java driver for Sahi which you can use if you want.

hudson said...

In one of my current projects, we are evaluating both Selenium & Sahi, for Test Automation. Since the web application is available as "https" and accessing through a proxy, I came to know that Selenium cannot support this, through the post {http://www.thoughtworks-studios.com/twist-agile-test-automation/1.0/help/how_do_i_use_selenium_to_test_https_sites.html}.

Wanted to know whether Sahi can support automating https sites which are behind a proxy. If so let me know the link/document, which specifies the same.

V. Narayan Raman said...

Follow this http://sahi.co.in/w/https-ssl to see how to get SSL working.

For proxy tunneling, have a look at the error message you get when you try to access external sites. It gives you full details.

The gist is: You need to open sahi/userdata/config/userdata.properties and change these settings properly.

# Use external proxy server for http

# Use external proxy server for https

# There is only one bypass list for both secure and insecure.

hudson said...

That worked like magic!!!

Thanks for your answer. The cucubmer features just run fine, with Sahi as the automation tool to drive through the web pages. I could make this run in IE, however, on Firefox, it goes to the URL and waits at the line where the user name box is to be typed {browser.textbox("...").setValue(".....")}

What could be the reason??

V. Narayan Raman said...

Hi Hudson,

Glad to know Sahi did work for you https + proxy tunneling case.

Looks like you are using the java driver? Could you post the details on the forums so that we can dive deeper into the issue?


hudson said...

Yes we are using the Java Driver. Changing the browserPath for firefox, in the Java file, I thought this will automatically run the test in firefox, but that was not the case.

I have found that the Sahi Controller does not come up {when I do Ctrl + Alt & double click} for a site starting with https. However it comes up on the same (firefox) browser for any site starting with http {say http://www.google.com}. I am using Firefox 3.5.1

For my application under test, it is available only as https://www..com. Let me know whether any solutions exist.

V. Narayan Raman said...

For SSL, follow the instructions given here: http://sahi.co.in/w/https-ssl