Selenium IDE export working strangely with Test::Unit and Ruby

45 views
Skip to first unread message

Jyrki Mizaras

unread,
Apr 7, 2014, 7:31:10 AM4/7/14
to seleniu...@googlegroups.com

Hi,


I tried to post this topic on Saturday and I am not sure has this question already appeared on the group page; I think it did not appear on the Group page on Sat.


I'm new to Selenium and I think I got Selenium IDE to work quite nicely, but something strange is happening (perhaps a bug) when I export Selenium IDE (2.5.0) Test Case to a Ruby Test::Unit / WebDriver test. My test works fine when I run it from Selenium IDE, but after I exported it to Ruby the following code doesn't work:


verify { assert_match /.*Welcome/, @driver.find_element(:xpath, "//title").text }


On Selenium IDE the same test is defined as:

Command: verifyText

Target: //title

Value: regexp:.*Welcome

and it works fine (attached screenshot)


Below is the error message for: verify { assert_match /.*Welcome/, @driver.find_element(:xpath, "//title").text } . So there is some kind of problem with the RegEx I guess. The title should end with the word 'Welcome'



jyrki@ubu:~/rails_projects/p3$ rake test test/selenium/employer_signup_ruby2_test.rb

Loaded suite /home/jyrki/.rvm/gems/ruby-2.1.0/bin/rake

Started

F

===============================================================================

Failure:

test_employer_signup_ruby2(EmployerSignupRuby2)

/home/jyrki/rails_projects/p3/test/selenium/employer_signup_ruby2_test.rb:17:in `teardown'

<[]> expected but was

<[#<Test::Unit::AssertionFailedError: <""> expected to be =~

</.*Welcome/>.>]>


diff:

+ [#<Test::Unit::AssertionFailedError: <""> expected to be =~

? [ ]

? </.*Welcome/>.>

===============================================================================



Finished in 12.747676835 seconds.


1 tests, 2 assertions, 1 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications

100% passed


0.08 tests/s, 0.16 assertions/s

Run options: --seed 25544


# Running tests:




Finished tests in 0.003802s, 0.0000 tests/s, 0.0000 assertions/s.


0 tests, 0 assertions, 0 failures, 0 errors, 0 skips


Below is the code that works apart from the one line of the code

require "json"

require "selenium-webdriver"

gem "test-unit" #?

require "test/unit"


class EmployerSignupRuby2 < Test::Unit::TestCase


def setup

@driver = Selenium::WebDriver.for :firefox

@base_url = "http://0.0.0.0:3000/"

@accept_next_alert = true

@driver.manage.timeouts.implicit_wait = 30

@verification_errors = []

end

def teardown

@driver.quit

assert_equal [], @verification_errors

end

def test_employer_signup_ruby2

@driver.get(@base_url + "/employer_signup")

@driver.find_element(:id, "employer_first_name").clear

@driver.find_element(:id, "employer_first_name").send_keys "tester"

@driver.find_element(:id, "employer_last_name").clear

@driver.find_element(:id, "employer_last_name").send_keys "login-tester"

@driver.find_element(:id, "employer_email").clear

@driver.find_element(:id, "employer_email").send_keys "tester012345678901[a]test.com" # this line has been altered for this post, @ changed to [a]

@driver.find_element(:id, "employer_password").clear

@driver.find_element(:id, "employer_password").send_keys "foobar"

@driver.find_element(:id, "employer_password_confirmation").clear

@driver.find_element(:id, "employer_password_confirmation").send_keys "foobar"

@driver.find_element(:name, "commit").click

verify { assert_match /.*Welcome/, @driver.find_element(:xpath, "//title").text }

end

def element_present?(how, what)

@driver.find_element(how, what)

#${receiver}.find_element(how, what)

true

rescue Selenium::WebDriver::Error::NoSuchElementError

false

end

def alert_present?()

@driver.find_element(how, what)

#${receiver}.switch_to.alert

true

rescue Selenium::WebDriver::Error::NoAlertPresentError

false

end

def verify(&blk)

yield

rescue Test::Unit::AssertionFailedError => ex

@verification_errors << ex

end

def close_alert_and_get_its_text(how, what)

alert = @driver.switch_to().alert()

#alert = ${receiver}.switch_to().alert()

alert_text = alert.text

if (@accept_next_alert) then

alert.accept()

else

alert.dismiss()

end

alert_text

ensure

@accept_next_alert = true

end

end




Actually, I can get the test to work (sort of) when I replace the one line of code with this:

verify { assert @driver.find_element(:xpath, '//title[contains(.,"Welcome")]') } #works!!


However if the “Welcome” text is missing (=test is failing), then the fix will remain in a loop indefinitely and I have interrupt (Ctrl + C) the test. So far this is the best fix I have found. If you know a better fix, please let me know, thanks :-)


My Gemfile has the following rails/selenium/test versions:

gem 'rails', '4.0.2'

gem 'selenium-webdriver', '~> 2.41.0' #this is the latest version

gem 'test-unit', '2.5.5'

gem 'rake', '10.2.2'


$ bundle show json

The gem json has been deleted. It was installed at:

/home/jyrki/.rvm/rubies/ruby-2.1.0/lib/ruby/gems/2.1.0/gems/json-1.8.1


Ruby 2.1.0


In addition, the export creates ruby code: ${receiver}.find_element(how, what) that doesn't work; so I needed to change it to @driver.find_element(how, what) to make it work.


Also, export creates a gem statement in addition to require statement for test-unit (lines 3-4):

gem "test-unit" #?

require "test/unit"


I have attached screenshot.


BR Jyrki


selenium_p3.jpg
Reply all
Reply to author
Forward
0 new messages