views:

969

answers:

1

I'm using Selenium to ease my testing burden and I have about 1,000 different drop down list combinations (spread across multiple pages and drop down lists) that need to be tested. Basically, what I would like to do is select each <option> inside of a <select>, click the Submit button, select an item (first, second, third, etc.) in the drop down list on the resulting page, click submit, and then go back and select the next item, in sequence. Each time, it should assert that a certain value (related to the drop down list value selected) is present on the final page. Does anybody know if this kind of logic is possible in Selenium?

I'm having a hard time explaining this, so hopefully this pseudo code clears things up

foreach option in select
  select option
  submit form

    foreach option in select
    select option
    submit form

      assert that page contains text that matches selected values

Edit: I have selected values from the drop down list while the recorder is playing, but it seems like the recorder isn't picking up the selected drop down list values. Nor have I been able to figure out how to perform the operation for each <option> in a <select>.

The first question I have is whether or not it's even possible. If it is, could somebody please point me in the right direction to get me started?

Edit 2: I'm not opposed to using another web automated testing utility. If anybody has any recommendations for a free alternative, please feel free to make that recommendation.

+3  A: 

What language are using Selenium in? If you're just using Selenium by writing HTML, I'd recommend switching to a programming language and using Selenium RC -- bindings are available for a wide variety of languages, such as Java and Python. In Java, I believe the following would do what you want:

void test(Selenium browser, String startPageUrl,
          String firstFormLocator, String firstSelectLocator,
          String secondFormLocator, String secondSelectLocator) {
    browser.open(startPageUrl);
    for (String option : browser.getSelectOptions(firstSelectLocator)) {
        browser.open(startPageUrl);
        browser.select(firstSelectLocator, "label=" + option);
        browser.submit(firstFormLocator); // Or click the submit button
        for (String subOption : browser.getSelectOptions(secondSelectLocator) {
            browser.open(startPageUrl);
            browser.select(firstSelectLocator, "label=" + option);
            browser.submit(firstFormLocator); // Or click the submit button
            browser.select(secondSelectLocator, "label=" + subOption);
            browser.submit(secondFormLocator); // Or click the submit button
            // Do your assertions
        }
    }
}

The code isn't exactly readable, so it might be worth some time abstracting the page away slightly using the Page Object pattern. This also helps make the code more maintainable, for instance when you change the ID of an element, you only need to change it in the page object rather than every test.

Also bear in mind that doing this 1000 times isn't going to be quick. It might be worth seeing if you do similar testing just below the web interface to allow quicker feedback from tests, and then test the web interface is using the lower layer correctly. Also, do you really need 1000 tests? It seems that there's some redundancy in testing here -- is the 1000th test going to fail if the last 999 have passed?

Michael Williamson
You can also use the `index=` functionality if you don't want to do a foreach e.g. for(int i=0;i<browser.getSelectOptions(firstSelectorLocator).length;i++){browser.select(firstSelectLocator,"index=" + i.toString();....}
AutomatedTester