Ruby Capybara select random value from dropdown

1 min


648
3 comments, 648 points
select random value from dropdown
select random value from dropdown

As a QA one of the most common action on web might be to select values from dropdown but sometimes you have huge lists and all does the same thing in the end so you might want to select random value from dropdown without bothering too much about values is gonna be. I will try to cover this topic in a project that is using Ruby Capybara api but you can adapt it to any other framework/languages.

Imagine a simple scenario , you want to find out whats the estimate for your house that you live in London:

Given Im on Zoopla home values page
When I click on button "Get a Zoopla estimate"
And I type a post code "NW3 5DR"
When I click on button Locate address
And I choose an address
...
...
......

So on the 5th step you might have a bunch of 50 addresses , choosing any of them will take us to the same page for a property ID, so we don’t really need to hardcode the address we can randomise it. Some people might criticise me but excuse me I’m a practical guy.

Solution for select random value from dropdown
   def choose_random_option_from_drop_down(type, locator)
        options = find(type, locator).all(:option)
        if options.last.text.include? 'My address is not listed'
          tmp_options = options.drop(1)
          tmp_options2 = tmp_options.first options.size - 1
          tmp_options2[rand(tmp_options2.count)].select_option
        elsif options.count <= 2
          options[1].select_option
        else
          tmp_options3 = options.drop(1)
          tmp_options3[rand(tmp_options3.count)].select_option
        end
      end

Simple as that , we get all the options , in my case I get read of the option with text “My address is not listed” and I get read of the first option named “Select” , also if there are just 2 options , first will be “Select” and second will be the actual address we select the address.

This article is just the beginning of the a big selenium Tips suite that will be posted on testing repository. Meanwhile you can check How to getText from the disabled input field in selenium webdriver.

Happy testing guys!


Like it? Share with your friends!

648
3 comments, 648 points

What's Your Reaction?

cute cute
0
cute
scary scary
0
scary
wtf wtf
1
wtf
geek geek
0
geek
WIN WIN
0
WIN
Love Love
1
Love
OMG OMG
0
OMG
Angry Angry
0
Angry
Lol Lol
0
Lol
Test engineer

3 Comments

Your email address will not be published. Required fields are marked *

Choose A Format
Trivia quiz
Story