0

I wish to check value on UI with the provided value in script. And on comparing these two, it should display message "test passed" in console.

I have written following code: -

browser.find_element(:xpath,"//*[@id="total"]/tbody/tr/td[4]/span").value.should == "$466,634,599.67"

But it does not display anything on console. What could be required next?

Thanks! Abhishek

  • Are you using a test framework - example Cucumber, RSpec, etc.? Getting the framework to output to console might be different than a non-framework script. – Justin Ko Apr 29 '14 at 14:16
  • @JustinKo I haven't used any test framework. This is my first attempt at automation and I have plain script that requires Selenium Webdriver and commands like above that do the job. – SoftwareTestingEnthusiast Apr 30 '14 at 04:01

2 Answers2

1

Assertions, eg the .should==, are typically used within a test framework such as RSpec, MiniTest, Cucumber, etc. These test frameworks are designed to have reporting of the assertion's pass or fail result.

Given that you are not using a test framework, you will need to manually handle the output of results.

The most straightforward way would be to drop the assertion part and use an if statement:

element_value = browser.find_element(:xpath,"//*[@id="total"]/tbody/tr/td[4]/span").text
if element_value == "$466,634,599.67"
  puts 'Test passed'
else
  puts 'Test failed'
end

Note that the puts statements are used to output results to the console (unless you have changed the default output location).

Alternatively, if you do want to use the assertion, you could do:

element_value = browser.find_element(:xpath,"//*[@id="total"]/tbody/tr/td[4]/span").text
element_value.should == "$466,634,599.67"
puts 'Test passed'

In this approach, the assertion will raise an exception if the test fails. The assertion would stop the execution of the code and would therefore not output the 'Test passed' message (as expected). If the test passes, the 'Test passed' message would get outputted.

Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • I tried using .value but it prints the value nil in stead of actual value. browser.find_element(:css,"#total > tbody > tr > td:nth-child(4) > span").attribute('value') browser.find_element(:css,"#total > tbody > tr > td:nth-child(4) > span").value Neither works! – SoftwareTestingEnthusiast May 02 '14 at 18:19
  • You probably want to do `.text` instead of `.value`. There apparently is no `.value` method for spans in Selenium-Webdriver. `attribute('value')` gets the value of the attribute value, which your span element likely does not have. – Justin Ko May 02 '14 at 18:39
0

I use capybara gem for this purpose. It can check almost all front-end possibilities. Code should look something like this:

describe "User pages" do
  subject { page }
  describe "index" do
    before(:each) do
      sign_in user
      visit users_path
    end
    it { expect(page).to have_selector('li', text: user.name) }
end
iSunRise
  • 106
  • 3
  • I am newbie to automation, do I have to write entire script again in Capybara? – SoftwareTestingEnthusiast Apr 28 '14 at 22:53
  • Add "gem 'capybara'" to Gemfile first and run bundle install. Then follow [this](https://github.com/jnicklas/capybara#using-capybara-with-rspec) instruction. And at the end you may be need to add some hack from [here](http://stackoverflow.com/questions/8862967/visit-method-not-found-in-my-rspec) to get capybara work (last answer works for me). Good luck. – iSunRise Apr 28 '14 at 23:01
  • But would I have to write entire test script again in Capybara or just integrate Capybara and continue further? – SoftwareTestingEnthusiast Apr 28 '14 at 23:08
  • Capybara just adds some methods to your specs. You need to set subject { page }, and visit URL in before block for using it. – iSunRise Apr 29 '14 at 07:41