0

I'm creating tests using Selenium 2 Web Driver with C#.Net. After reading through a lot of the Selenium documentation, I am not sure if I'm followign the correct design pattern and feeling unsure on how to go about testing using PageObject design patterns.

here is my current code that I'm using on my page and its working

WaitForElement(By.CssSelector("input#ctl00_ctl00_signinControl_txtUsername")).SendKeys("abc123");
WaitForElement(By.CssSelector("input#ctl00_ctl00_signinControl_txtPassword")).SendKeys("password");

SelectElement select;
IWebElement selElement = WaitForElement(By.CssSelector("select#ctl00_ctl00_ddlGoTo"));
select = new SelectElement(selElement);
select.SelectByText("Homepage");

*<more code .....>*

also I have told that I can not use Select page element using pageFactory.

Do I need to change my code the way I have coded? any feedback would be great.

Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • Why can't you use the `Select` class? I see no reason why not. The Page Object pattern is merely there to split your tests from their elements and how they interact. Your test *shouldn't care* how an element is found, this is the responsibility of your page object. – Arran Feb 13 '13 at 09:24
  • If you use a PageFactory, each screen control is instantiated automatically as a `WebElement` (not the appropriate subclass e.g. `Select`). You can cast the `WebElement` to `Select` at runtime when you're about to interact with the screen control - but it's not very clean; it'd be a lot nicer if you could instantiate it as a `Select` in the first place. – Vince Bowdren Apr 30 '13 at 11:13

1 Answers1

0

The idea of the page object pattern is to have an object that represents the page. You are essentially writing an API for how to interact with the page.

For example a login page object may have the following methods:

  • enterUserName(String userName);
  • enterPassword(String password);
  • clickLoginButton();

The person using the page object to interact with the page does not need to care about how selenium finds elements and interacts with them. If the id on a field changes you would just need to change the locator on the page object and would not need to change all tests that call the associated page object public method.

jdumonti
  • 26
  • 2
  • I'll simply point out here that the described object does **not** follow the Page Object pattern. At best, it would be called a Page Wrapper. A Page Object would have a method like `login(String userName, String password)`, and the credentials would be passed into the method. – JimEvans Feb 14 '13 at 20:46
  • Thanks for the correction. I didn't realize te difference until you pointed it out. I had methods like the login method you described but found my tests needed to control the order of entering each of the fields to the point where almost every method was just working with a field or two. I guess the Page Wrapper in the end works better for me. – jdumonti Feb 15 '13 at 04:19
  • yes what JimEvans said. I've written a post that explains a way you can approach the page object pattern. Check it out http://www.paulsodimu.co.uk/Post/Implementing-the-Page-Object-Pattern-in-C-using-Selenium-WebDriver – bayological Jan 06 '15 at 17:21