| 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
    start_sahi.bat


  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)
    end

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

    #close the browser on exit
    at_exit do
    browser.close
    end

    Given /^I am not logged in$/ do
    browser.navigate_to("http://sahi.co.in/demo/training/index.htm")
    end

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

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

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

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


  10. Run and watch the tests complete successfully

    cd D:\test\
    cucumber login.feature


  11. Done!

8 comments:

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
ext.http.proxy.enable=false
ext.http.proxy.host=localhost
ext.http.proxy.port=808
ext.http.proxy.auth.enable=false
ext.http.proxy.auth.name=kamlesh
ext.http.proxy.auth.password=password

# Use external proxy server for https
ext.https.proxy.enable=false
ext.https.proxy.host=localhost
ext.https.proxy.port=808
ext.https.proxy.auth.enable=false
ext.https.proxy.auth.name=kamlesh
ext.https.proxy.auth.password=password

# There is only one bypass list for both secure and insecure.
ext.http.both.proxy.bypass_hosts=localhost|127.0.0.1|*.internaldomain.com

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?

Regards,

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