How to use ExpectedConditions in Ruby?

1,123 views
Skip to first unread message

H M

unread,
Feb 28, 2017, 10:11:19 PM2/28/17
to Selenium Users
Hi,

I'm in trouble using Selenium Webdriver in ruby.

I made a test code below

      @webdriver.navigate.to @target_url
     
@wait.until( ExpectedConditions.frameToBeAvailableAndSwitchToIt('some FrameLocator'))

but unfortunately it ends like below:
NameError:
       uninitialized constant ExpectedConditions

I found a lot of 'ExpectedConditions' examples for java, but I cannot find it in ruby.

How can I write it in Ruby?


Titus Fortner

unread,
Feb 28, 2017, 11:30:50 PM2/28/17
to seleniu...@googlegroups.com
To quote Simon Stewart:

"ExpectedConditions and Wait were designed as demo code. I never really expected people to use them"

So, Ruby doesn't implement expected conditions. It waits for the result of a 'truthy' value, and for convenience rescues NoSuchElementError. This allows you to do:

@wait.until { driver.find_element(id: frame_id) }
@webdriver.switch_to.frame(frame_id)

Also of note, most people who use Ruby bindings for Selenium use it in conjunction with a higher level library like Watir or Capybara or Page Object gem (which is now using Watir exclusively, but I mention it because it has its own DSL that some people prefer). All of these will do that wait automatically for you.

In Watir you don't even have to manage frames. It automatically moves you into and out of browser level contexts based on the elements you want to interact with:

@browser.frame(id: frame_id).div(id: div_inside_frame).text
@browser.div(id: div_in_top_level_context).click

The Selenium Ruby community (and Watir devs especially) is relatively active on Selenium Slack in the #ruby-ruby-ruby-ruby and #watir channels if you'd like to have more in-depth conversations there: http://seleniumhq.herokuapp.com/

Titus


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/885192a2-a305-4c03-b1f3-f49ce11338b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mwil...@herringbank.com

unread,
Sep 19, 2018, 4:28:53 PM9/19/18
to Selenium Users
I have the same question, from what I can tell it might be possible to import/require/link to something or create the class ourself in the ruby script. I want to use the following because a modal is getting in the way, either that or I need the developer to look more closely at why my script keeps failing at this point in various different runs. 

Ex.- WebDriverWait wait = new WebDriverWait(driver, 5);
 
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id()));

but ruby doesn't like WebDriverWait(driver,15) or any variation I've tried to fix with 

selenium/data_load.rb:81: syntax error, unexpected tCONSTANT, expecting keyword_do or '{' or '('
WebDriverWait wait = new WebDriverWait{driver, 5}
                                                                   ^
selenium/data_load.rb:81: syntax error, unexpected '}', expecting :: or '[' or '.'
selenium/data_load.rb:194: syntax error, unexpected keyword_end, expecting '}'

Titus Fortner

unread,
Sep 19, 2018, 4:56:55 PM9/19/18
to seleniu...@googlegroups.com
This looks like Java code, not Ruby code. :)

Take a look at the Ruby documentation:
https://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/Wait
You can see examples here:
https://github.com/SeleniumHQ/selenium/blob/master/rb/spec/unit/selenium/webdriver/wait_spec.rb
> --
> You received this message because you are subscribed to the Google Groups "Selenium Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
> To post to this group, send email to seleniu...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/b53971ed-dbc4-4187-8c1b-10f6565b4327%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages