Quick example of testing login form with mechanize and Test::Unit

I started testing with Ruby. So i first use Test::Unit and then i want to test some functionality on web. I read some examples at mechanize documentation, which is here or at your ruby/gems directory. So let’s look at the code.
require ‘mechanize’
require ‘test/unit’

class LoginTest < Test::Unit::TestCase
def test_login

#creating mechanize object
agent = WWW::Mechanize.new

#setting url
page = agent.get(‘https://zaparka.cz/wp-login.php’)

# get first html form on page, u can use page.form(‘form_name’)
login_form = page.forms.first

#fill value <input type=”text” name=”log” /> with login name
login_form.log = ‘name’

#fill value <input name=”pwd” type=”password” /> with password
login_form.pwd = ‘secrets’

#submiting form and saving result page to value page
page = google_form.submit()

# test if login was succesflull
assert_equal ‘Dashboard ‹ Petr Zaparka — WordPress’, page.title

It’s very easy and simply. However you can’t test javasript functionality. Next time i post testing with WEBRAT.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.