Željko Filipin's Blog on Software and Testing

Test like you do not need the money.

Tests and How-To Documents

with 17 comments

You have a bunch of tests, right? With every build of your application under test, you run them all. If a test fails, application should be fixed, or you test should be fixed. Wouldn’t it be nice to make how-to documents from that tests?

Time for an example (Ruby, Watir):

def login_as(username, password)
ie.goto(“my.app/login”)
ie.should_contain(“please login”)

ie.text_field(:id, “username”).set(username)
ie.text_field(:id, “password”).set(password)
ie.button(:value, “login”).click
ie.should_contain(“logged in as #{username}”)
end

You run that test, and you get output like this (this is real output from my test):

log in
- go to ‘[application url]/login.aspx’
- (url should be ‘[application url]/login.aspx’)
- (there should be text ‘Please Login’)
- at text field ‘userName’ enter ‘[userName]‘
- at text field ‘password’ enter ‘[password]‘
- click button ‘Login’
- (url should be ‘[application url]/default.aspx’)
- (there should be text ‘Recent Contributions’)
- (there should be text ‘logged in as [first name] [last name]‘)

I posted this at wtr-general list and people recommended to take a look at rSpec and Systir.

Written by Željko Filipin

November 10th, 2006 at 1:18 pm

Posted in Watir

17 Responses to 'Tests and How-To Documents'

Subscribe to comments with RSS or TrackBack to 'Tests and How-To Documents'.

  1. I want to click on an image button “IMG SRC=’/images/agent/btn_save_continue.gif’ BORDER=0 STYLE=’cursor: pointer’ onClick=’change_has_been_handled = true; if

    (!this.clicked) {document.the_form.submit();; this.clicked = true;}’”

    How to do that?
    $ie.image(:src,/btn_save_continue/).flash
    $ie.button(:src,/btn_save_continue/).click

    both didnt worked…..

    Balaji

    20 Dec 06 at 4:41 pm

  2. Hi Balaji,

    Try this

    ie.image(:src, /btn_save_continue/).click

    I see that you have asked that question also at Watir forum, and I have already answered there.

    Željko Filipin

    20 Dec 06 at 4:52 pm

  3. Will the flash event works for checkboxes,radio buttons and text boxes?

    Meena

    5 Jan 07 at 9:16 am

  4. Balaji,

    Instead of giving like this

    $ie.button(:src,/btn_save_continue/).click

    Try “ie.show_images” this will show all the images in the website.

    Take the entire path of the image within double codes and apply the same in your code.This will work for sure.

    Format:

    ie.show_images
    ie.image(:src, “https://…. images/agent/btn_save_continue.gif”).click

    Meena

    5 Jan 07 at 9:39 am

  5. Meena,

    Yes, flash works for all elements.

  6. Željko Filipin,

    What’s the syntax for using flash in checkbox, radio buttons and text boxes.

    Meena

    5 Jan 07 at 10:41 am

  7. Meena,

    ie.checkbox(:how, what).flash
    ie.radio(:how, what).flash
    ie.text_field(:how, what).flash

    Take a look at WATIR User Guide (http://www.openqa.org/watir/watir_user_guide.html), search for flash.

  8. Is it possible to make a Watir code repeat the same task n number of times?

    For ex. repeat opening a website and click on a link n number of times.

    If so, how?

    Anonymous

    23 Jan 07 at 1:04 pm

  9. I do not want to discourage anybody from posting comments, but Watir related questions will probably get answered quicker at Watir mailing list (http://rubyforge.org/mailman/listinfo/wtr-general or http://forums.openqa.org/forum.jspa?forumID=5). I am monitoring that list, and I will answer your question there if I know the answer.

    That said, to answer your question (in the next comment). :)

    Željko Filipin

    23 Jan 07 at 2:27 pm

  10. If you have a page that has a link that points to itself, like this

    <a href=”page.htm”>text</a>

    you can click that link five time with this

    require ‘watir’
    ie = Watir::IE.start(“app.com”)
    n = 5
    for i in 1..n
    ie.link(:text, “text”).click
    end

    Željko Filipin

    23 Jan 07 at 2:38 pm

  11. Filipin,

    To use ie.refresh(), do we include any package as such?

    If so, could you tell the package with a sample code?

    Anonymous

    1 Feb 07 at 9:12 am

  12. Anonymous,

    I am not sure if I understood your question. You do not have to require any other file (except watir.rb, of course) to use ie.refresh.

  13. Hi Željko,
    FYI, to click that link 5 times you may consider ditching the for loop and using the more elegant:
    5.times do
    ie.link(:text, “text”).click
    end

    Claudio Perrone

    20 Mar 07 at 12:17 am

  14. Hi Željko,

    Is it possible to do a text search using watir code?

    For ex: I have a website. I want to search for a particular word “customer” in that page and if possible the no of occurances of that word.

    If it is possible, can u guide me how to do that?

    Anonymous

    18 May 07 at 9:34 am

  15. Anonymous,

    Of course it is possible. It is Ruby feature, not Watir, but you probably do not care. :)

    # put page text to variable t
    t = ie.text
    => “customer text customer”

    # the counter
    i=0
    => 0

    # increase i when you find “customer”
    t.gsub(“customer”) {i+=1}
    => “1 text 2″

    # It works!
    i
    => 2

    If you post this question at Watir forum somebody may have a better solution.

    Željko Filipin

    18 May 07 at 10:23 am

  16. [...] I have already written about Tests and How-To Documents. [...]

  17. have to admit very usefull comments. thank you, Željko!

    Pavel

    10 Apr 08 at 11:12 pm

Leave a Reply