Željko Filipin's Blog on Software and Testing

Test like you do not need the money.

Archive for the ‘code’ tag

Watir, Ruby, objekti i varijable

without comments

Živim od testiranja web aplikacija. Služim se Rubyjem i Watirom za automatiziranje testova funkcionalnosti. Moje skripte su narasle, pa sam napravio mali framework (ne znam kako ovo pametno prevesti) kako bi ih pojednostavnio (sav kod u ovom članku je pojednostavljen i neisproban).

Klasa Page olakšava provjeravanje url-a i tekst(ov)a stranice.

class Page
  def initialize(ie, url, texts)
    @ie, @url, @texts = ie, url, texts
  end
  def check
    @texts.each do |text|
      assert(@ie.contains_text(text))
    end
    assert_equal(@url, @ie.url)
  end
end

“Pogledaj element” stranica u aplikaciji koju testiram ima tekst (breadcrumb – opet ne znam kako bi ovo smisleno preveo) {vrsta_elementa} > View (primjer: File > View).”

Nakon što je element dodan/promijenjen, na toj stranici se nalazi i tekst {vrsta_elementa} {akcija}. (primjer: File added.).

Klasa File, naravno, testira datoteke.

class File
  def initialize
    @page_view = Page.new(
        "app.com/view.aspx",
        ["File > View"])
    @page_view_after_add = Page.new(
        "app.com/view.aspx",
        ["File > View", "File added."])
    @page_view_after_edit = Page.new(
        "app.com/view.aspx",
        ["File > View", "File edited."])
  end
  def add
    # add file
    @page_view_after_add.check
  end
  def edit
    # edit file
    @page_view_after_edit.check
  end
  def view
    # go to file
    @page_view.check
  end
end

I sve baš lijepo radi. Sve što trebam napisati je ovo.

file = File.new
file.add
file.edit
file.view

Postoje tri objekta. Jedan za svaku (@page_view…) varijablu. “id” u “Page:{id}” je različit za svaku varijablu.


#<Page:0x2b5ac28 @url="app.com/view.aspx", @texts=["File > View"]>
#<Page:0x2b5ab98 @url="app.com/view.aspx", @texts=["File > View", "File added."]>
#<Page:0x2b5ab08 @url="app.com/view.aspx", @texts=["File > View", "File edited."]>

Ali, dupliciranje me muči.

Želio bi da File#initialize izgleda ovako.

@page_view = @page_view_after_add = @page_view_after_edit = Page.new(
  "app.com/view.aspx",
  ["File > View"])
@page_view_after_add.texts < < "File added."
@page_view_after_edit.texts << "File edited."

Ali (iznenađenje!), to ne radi. Pa, prilično sam novi u objektno orijentiranom svijetu, pa mi to i nije iznenađenje.

Na ovaj način stvoren je samo jedan Page objekt, i sve tri varijable pokazuju na njega. Bez obzira koju varijablu promijenim, ona promijeni objekt na koji pokazuje. Rezultat je da su sve tri varijable iste. To nije ono što sam želio.


#<Page:0x2820048 @url="app.com/view.aspx", @texts=["File > View", "File added.", "File edited."]>
#<Page:0x2820048 @url="app.com/view.aspx", @texts=["File > View", "File added.", "File edited."]>
#<Page:0x2820048 @url="app.com/view.aspx", @texts=["File > View", "File added.", "File edited."]>

Isprobao sam nekoliko alternativa, ali niti jedna nije uspjela. Za sad mogu živjeti s duplikacijom, ali bi je volio ukloniti iz mojih skripti.

Written by Željko Filipin

February 7th, 2006 at 11:46 am

Posted in Hrvatski (Croatian),Ruby,Watir

Tagged with

Watir, Ruby, Objects and Variables

without comments

I test web applications for living. I use Ruby and Watir for automating functional tests. My scripts have grown, so I made a little framework to make them simpler (all code in this post is simplified and not tested).

Class Page makes checking page url and text(s) easier.

class Page
  def initialize(ie, url, texts)
    @ie, @url, @texts = ie, url, texts
  end
  def check
    @texts.each do |text|
      assert(@ie.contains_text(text))
    end
    assert_equal(@url, @ie.url)
  end
end

View item page at application under test has text (breadcrumb) {item_type} > View (example: File > View).

After item is added/edited, at that page there is also text {item_type} {action}. (example: File added.).

Class File tests files, of course.

class File
  def initialize
    @page_view = Page.new(
        "app.com/view.aspx",
        ["File > View"])
    @page_view_after_add = Page.new(
        "app.com/view.aspx",
        ["File > View", "File added."])
    @page_view_after_edit = Page.new(
        "app.com/view.aspx",
        ["File > View", "File edited."])
  end
  def add
    # add file
    @page_view_after_add.check
  end
  def edit
    # edit file
    @page_view_after_edit.check
  end
  def view
    # go to file
    @page_view.check
  end
end

And it works just nice. All I have to write is this.

file = File.new
file.add
file.edit
file.view

There are three objects. One for each (@page_view…) variable. “id” in “Page:{id}” is different for each variable.


#<Page:0x2b5ac28 @url="app.com/view.aspx", @texts=["File > View"]>
#<Page:0x2b5ab98 @url="app.com/view.aspx", @texts=["File > View", "File added."]>
#<Page:0x2b5ab08 @url="app.com/view.aspx", @texts=["File > View", "File edited."]>

But, duplication bugs me.

I would like that File#initialize looks like this.

@page_view = @page_view_after_add = @page_view_after_edit = Page.new(
  "app.com/view.aspx",
  ["File > View"])
@page_view_after_add.texts < < "File added."
@page_view_after_edit.texts << "File edited."

But (surprise!), it does not work. Well, I am pretty new in object oriented world, so this was not really a surprise for me.

This way only one Page object is created, and all three variables point to it. No matter which variable I change, it changes the object it points to. Result is that all three variables are the same. Not what I wanted to do.


#<Page:0x2820048 @url="app.com/view.aspx", @texts=["File > View", "File added.", "File edited."]>
#<Page:0x2820048 @url="app.com/view.aspx", @texts=["File > View", "File added.", "File edited."]>
#<Page:0x2820048 @url="app.com/view.aspx", @texts=["File > View", "File added.", "File edited."]>

I have tried a few alternatives, but none worked. I will live with duplication for now, but I would like to remove it from my scripts.

Written by Željko Filipin

February 7th, 2006 at 11:46 am

Posted in Ruby,Watir

Tagged with

Instaliraj Ruby on Rails na računalu koje nema pristup Internetu

without comments

Đžogljavko pored pruge (engl. rail).

Pisao sam o tome kako napraviti Ruby on Rails web aplikaciju.

Želio sam prenijeti tu aplikaciju na drugo računalo (engl. computer). Postupak je iznimno jednostavan.

Na drugom računalu sam napravio korake 2-7 (od dva do sedam) iz spomenutog članka. Prenio sam mapu (engl. folder) u kojoj se nalazi moja aplikacija (C:\rails\blog) s mog računala na drugo računalo. Pokrenuo sam aplikaciju na drugom računalu. Sve radi.

Mapu možete prenijeti usb-om, spržiti (engl. burn) na cd, sažeti (engl. compress) pa postati e-poštom (engl. e-mail)…

Problem se pojavio kad sam htio pokrenuti Ruby on Rails aplikaciju na računalu koje nema pristup Internetu. Dugo sam se s tim mučio, i sad kad sam našao rješenje, što drugo nego ga objaviti. :)

Na svom računalu

Napravi C:\install (možeš odabrati i manje štrebersko ime). Tu češ natrpati sve što ti treba:

  1. ruby182-15.exe
  2. sqlite-3_2_7.zip
  3. sqlitedll-3_2_7.zip
  4. sqlite3-ruby-1.1.0-mswin32.gem
  5. rails-0.14.4.gem
  6. rake-0.6.2.gem
  7. activesupport-1.2.4.gem
  8. activerecord-1.13.1.gem
  9. actionpack-1.11.1.gem
  10. actionmailer-1.1.4.gem
  11. actionwebservice-0.9.4.gem
  12. mapu u kojoj se nalazi tvoja aplikacija (C:\rails\blog)

Prebacivanje

Prebaci C:\install sa svog računala u C:\install na željenom računalu koristeći svoj omiljeni način (usb, cd, e-pošta…).

Na drugom računalu

  1. instaliraj ruby
    1. dvaput klikni na C:\install\ruby182-15.exe
  2. instaliraj SQLite
    1. prebaci C:\install\sqlite-3_2_7.zip i C:\install\sqlitedll-3_2_7.zip u C:\ruby\bin
    2. raspakiraj obje zip datoteke (engl. file)
  3. otvori Command Prompt
    1. start > Run
    2. utipkaj cmd
    3. pritisni enter
  4. utipkaj u Command Prompt

    c:
    cd \install
    gem install sqlite3-ruby
    gem install rails
    gem install rake
    gem install activesupport
    gem install activerecord
    gem install actionpack
    gem install actionmailer
    gem install actionwebservice
    cd blog
    ruby script\server

Ako ti nešto nije jasno, pročitaj prvo prethodni članak. :)

Written by Željko Filipin

December 9th, 2005 at 12:45 pm

Posted in Hrvatski (Croatian),Ruby

Tagged with