How to initialize SelectElements while using PageFactory / FindsBy in Selenium C#? How to initialize SelectElements while using PageFactory / FindsBy in Selenium C#? selenium selenium

How to initialize SelectElements while using PageFactory / FindsBy in Selenium C#?


First, there's no "built-in wait" in the .NET PageFactory implementation. You can easily specify one in the call to InitElements (more on that in a bit). At present, the best option for you would be your option 3, though I wouldn't expose the IWebElement member; I'd make it private, since the PageFactory can enumerate over private members just as easily as public ones. So your page object would look like this:

[FindsBy(How = How.Id, Using = "MonthDropdown")]private IWebElement dropDown;public SelectElement MonthDropdownElement{    get { return new SelectElement(dropdown); }}

How do you get the actual IWebElement when you need it? Since SelectElement implements IWrappedElement, you can simply call the WrappedElement property if you need access to the methods and properties of the element provided by the IWebElement interface.

Recent versions of the .NET bindings have restructured the PageFactory to be more extensible. To add the "built-in wait" you desire, you could do the following:

// Assumes you have a page object of type MyPage.// Note the default timeout for RetryingElementLocator is// 5 seconds, if unspecified.// The generic version of this code looks like this:// MyPage page = PageFactory.InitElements<MyPage>(new RetryingElementLocator(driver), TimeSpan.FromSeconds(10));MyPage page = new MyPage();PageFactory.InitElements(page, new RetryingElementLocator(driver, TimeSpan.FromSeconds(10))); 

Additionally, if you really need to customize how things work, you're always welcome to implement IPageObjectMemberDecorator, which allows you to fully customize how attributes are enumerated and values set to the properties or fields decorated with those attributes. One of the (non-generic) overloads of PageFactory.InitElements takes an instance of an object implementing IPageObjectMemberDecorator.

I'll leave aside that proper implementations of the Page Object Pattern as strictly defined shouldn't expose any WebDriver objects outside of each page object. Otherwise, all you're implementing is a "page wrapper," which is a perfectly valid approach, just not what one would call a "page object."