Working with conditions

12 views
Skip to first unread message

Rafael s

unread,
Dec 26, 2014, 2:15:10 PM12/26/14
to rs...@googlegroups.com
Currently my test check if element is present in the screen. If element is present then run a specific action, else the test continuous normally. See my code:

----------------------------------------------------------------------------
require "selenium-webdriver"
require "rspec"
require 'rspec/expectations'


describe "element" do


before(:all) do
@driver = Selenium::WebDriver.for :firefox
@base_url = "http://the-internet.herokuapp.com/disappearing_elements"
@driver.manage.window.maximize
end

after(:all) do
@driver.quit 
end


it "Check icon" do
@driver.get(@base_url)
if expect(@driver.find_element(:xpath, "//*[@href='/gallery/']").displayed?).to be_truthy 
@driver.find_element(:xpath, "//*[@href='/gallery/']").click
sleep 2
puts "element appears"
else 
puts "element NOT appears"
end
end
end

-----------------------------------------------------

When the element is present, the message appears, but when the element not is present in the page, occurs an error and the message of "ELSE" not run.

someone help me?

Myron Marston

unread,
Dec 27, 2014, 1:46:01 AM12/27/14
to rs...@googlegroups.com
Expectations are not meant to be used in conditionals like this -- they communicate pass/fail by raising an exception or not rather than returning boolean values.   So, just use the expression you've wrapped in `expect` directly in a conditional:

if @driver.find_element(:xpath, "//*[@href='/gallery/']").displayed?
  @driver.find_element(:xpath, "//*[@href='/gallery/']").click
  sleep 2
  puts "element appears"
else 
  puts "element NOT appears"
end

HTH,
Myron
Reply all
Reply to author
Forward
0 new messages