TestStack / TestStack.Seleno

Seleno helps you write automated UI tests in the right way by implementing Page Objects and Page Components and by reading from and writing to web pages using strongly typed view models.
http://teststack.github.com/TestStack.Seleno/
MIT License
180 stars 60 forks source link

stupid question. how to set a value in a dropdown (html's select) with seleno? #201

Open isaldarriaga opened 9 years ago

isaldarriaga commented 9 years ago

I couldn't find any documentation for this simple task.

I tried with click() over the control, but doesn't make the dropdown to display the options.

The selector is returning an OpenQA.Selenium.Remote.RemoteWebElement object.

thanks.

mwhelan commented 9 years ago

Hi @isaldarriaga There's no such thing as a stupid question! And dropdowns are a lot trickier with Selenium than one might expect.

On your Page Object, you have a couple of methods for selecting items in a dropdown, either by value or by text:

Input.SelectByOptionValueInDropDown(x => x.RequiredEnum, value);
Input.SelectByOptionTextInDropDown(x => x.RequiredEnum, value);

You can see them both illustrated in our acceptance tests, on this Page Object: https://github.com/TestStack/TestStack.Seleno/blob/master/src/TestStack.Seleno.AcceptanceTests/PageObjects/Form1Page.cs

And these two acceptance tests in this file: https://github.com/TestStack/TestStack.Seleno/blob/master/src/TestStack.Seleno.AcceptanceTests/PageObjects/Controls/IndividualWritingTests.cs

Selecting_option_by_its_value_in_a_Drop_Down Selecting_option_by_its_text_in_a_drop_down

Hope that helps.

isaldarriaga commented 9 years ago

Hi @mwhelan. thanks for your response.

I'm seeing in the test acceptance examples they're using strongly-typed page objects.

This is not my case. I just use weakly-typed ones and use Find.Element to look for controls by ID, name, etc.

When i try to assign the returned RemoteWebElement to a DropDown, i get a System.InvalidCastException. They shouldn't be compatible.

I'm new to the repo. Is there an example there with weakly typed?

thanks.

robdmoore commented 9 years ago

Try using

HtmlControlFor<DropDown>("nameOfSelectElement").SelectElementByText("Name of option to select");
// or
HtmlControlFor<DropDown>("nameOfSelectElement").SelectElement(value);
isaldarriaga commented 9 years ago

Thank you @robdmoore

It worked pretty well.

It would be helpful that HtmlControlFor<> receives a selector instead of an Id, and work similar to Find.Element