SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Watir, MySQL, Windows

    I'm working on a PC with Windows XP and I have started to use WATIR for writing tests for my Web applications. (PHP) I'm a Ruby newbie, and have no experience with this language.

    My problem is that I want to use Watir to test HTML forms to check if the submitted value actually is registered in the MySQL database. How can I connect Watir(Ruby) to MySQL and make this comparison?

    I use the Ruby One-Click installer for Windows, and the Watir One-Click installer.

    I'd be very grateful for a detailed description on how to solve this problem.

    Regards.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've never used watir, but if you just want to connect to the database, you have a few options:

    - Use low level functions
    - Use ActiveRecord
    - Use Og

    I'll focus on the second one:

    Install activerecord with:
    $ gem install activerecord

    Now open your watir file, and put this in it:
    Code:
    require_gem 'activerecord'
    
    ActiveRecord::Base.establish_connection(
      :adapter  => "mysql",
      :host     => "localhost",
      :username => "root",
      :password => "******",
      :database => "dbname"
    )
    Now you have to create classes for your table. If you have a table called posts, you add this class:
    Code:
    class Post < ActiveRecord::Base
    end
    You can get data out like this:
    Code:
    Post.find(1) #=> Find post with id=1
    Post.find_by_title("foo") #=> Find post with title="foo"
    More info:

    http://rubyonrails.com/rails/classes...cord/Base.html

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    Install activerecord with:
    $ gem install activerecord
    But this is for *nix systems I presume? How can I install this when I use Windows and the "Ruby One-Click installer for Windows"

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    gem works on Windows if you've installed RubyGems.

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's the same for windows. $ just means: open console and type the following.

    Ruby one Click installer contains Rubygems.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx! That worked.

    Could someone give me the syntax for inserting in the database and storing the id in a variable too? That would be great!

  7. #7
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume that you have a posts table and a Post model class.

    Code:
    a_post = Post.create(:title => 'The title', :content => 'The content')
    a_post.id
    The first line creates a new post and puts 'The title' in the title column, and 'The content' in the content column. The second line gives you the id of the post.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    I had som problems with the MySQL connection, but this page (http://wiki.rubyonrails.org/rails/pa...ndingsOnWin32/) solved the problem.

  9. #9
    SitePoint Member gluefish's Avatar
    Join Date
    Jul 2001
    Location
    Aurora, OR
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ruby / Watir / SQL Server?

    Fenir2, Your reply to the Ruby / Watir / MYSQL was fantastic. Do you have an equivalent advice for SQL Server users?

    I typically use Excel for front end input, then generate Ruby code from the back end of Excel and execute that, then come back to the Excel script to do the Sql Server query. I'd actually rather to do the query inside the Ruby code.
    I'd rather have a free bottle in front of me than a prefrontal lobotomy

  10. #10
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    You'll have to do two things:

    1. Install http://rubyforge.org/projects/ruby-dbi/
    2. Change the adapter to sqlserver, like this:

    Code:
    ActiveRecord::Base.establish_connection(
      :adapter  => "sqlserver",
      :host     => "the_host
      :username => "root",
      :password => "******",
      :database => "dbname"
    )
    I don't know if it works though...

    Here is more information:

    http://wiki.rubyonrails.com/rails/pa...osoftSQLServer


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •