[Selenium + Ruby] Working with conditions

61 views
Skip to first unread message

Rafael C.

unread,
Dec 24, 2014, 10:27:27 AM12/24/14
to seleniu...@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
"Current Expense" 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? 

http://thebuddhatree.com/

unread,
Dec 24, 2014, 11:52:22 AM12/24/14
to seleniu...@googlegroups.com
not sure about Ruby but in Java, we can solve using try/catch block. In case of error code goes to catch block. Inside catch block, you can write your 2nd flow code.

Matthew Metzger

unread,
Dec 24, 2014, 11:41:10 PM12/24/14
to seleniu...@googlegroups.com
You appear to be using an rspec expectation for flow control. Rspec is going to raise an exception when that expectation is not met. Don't use exception handling for flow control.

Matthew Metzger

unread,
Dec 24, 2014, 11:43:11 PM12/24/14
to seleniu...@googlegroups.com
I think you're suggesting he use a try / catch block to achieve flow control with exception handling. This is usually a bad idea.

Selenium Framework

unread,
Dec 30, 2014, 3:41:44 PM12/30/14
to seleniu...@googlegroups.com
try catch in java is almost similar to begin, rescue block in Ruby. So you can try your code something like this

 begin

  expect
(@driver.find_element(:xpath, "//*[@href='/gallery/']").displayed?).to be_truthy
 rescue NoSuchElementException: e
    puts
"The element is not found within the timeout"
 
end
Reply all
Reply to author
Forward
0 new messages