简体   繁体   中英

Automated test with Ruby: select an option from drop-down list

I write automated test with Ruby(Selenium framework) and I need to know how can I select an option from drop-down list.

Thanks in advance!

building on floehopper's answer :

selenium.addSelection(locator, value)
or
selenium.select(locator, value)

You almost certainly want "id=my_select_box_id" (with the quotes) for locator , though other CSS selectors will work. value is the literal text value (not the display value) of the option to be selected.

Easiest way of doing this: select(selectLocator,optionLocator) as suggested above.

selectLocator : name or xpath for dropdown object optionLocator : name or xpath for dropdown option to be selected

Eg

@selenium.select "Language", "label=Ruby"

It sounds like you are writing a functional test here. Selecting it probably won't do you much good on its own. You need to submit the form in order to test the controller. :) It might help people answering to know which testing framework you are using, because there are several to choose from. If you are using RSpec, check out this screencast .

Hope that helps anyway.

Aside from functional tests, if you're looking for something that acts a bit more like the real app, have a look at WebRat . For non-AJAXed integration tests, it has a very nice DSL for selection your DOMs and taking appropriate actions against them. (link-clicking form-filling etc.).

On the other hand if your App is an external Web App that you just want to do acceptance tests on, you can also check Selenimum or Watir .

Note that WeRat is heavily web framework based where as Selenimum and Watir use the browser to interact with your web app directly (like a real user).

I think you want this command :-

select(selectLocator, optionLocator)
  • selectLocator identifies the drop down list
  • optionLocator identifies the option within the list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM