Coded UI- Accessing page objects in actual test

In my previous article, I was discussing about the concept of "Page Objects" and its use in Coded UI tests.

In this article, lets discuss how we can make use of them in our actual tests

Remember in Page Object Model, we see each screen/dialog of the application as a series of objects. (for example, username text field, password field, log in button etc). Also it has few definite advantages

                    1. Its reusable- same object can be reused by many
                    2. If any change to a control (object), there is one place to change (improve maintainability)

For explanation, I'll take a scenario- "Log in" page of a web application. It has a Username field, Password field and a Log-in button. It may contain many other objects but for our test of functionality of "Log-in" we are interested in only those 3 objects.

In our solution, we create a folder called "Object Repository", and add a class file to that. We are going to hold our objects inside methods of this class having return types'

Here, in the method "Public static HtmlEdit username(BrowserWindow browser)", I create an instance of HtmlEdit (This type is supplied by UITesting.HtmlControl namespace), whose parent control is a browser window (instance of BrowserWindow) (which browser window this object actually exists) which should be supplied at the time of this method is invoked in out test.

Also in the same method, I'm searching this control in the given web page using its ID. You may use one or more search criteria here if one is insufficient. This is the most important statement in this method.

txtusername.SearchProperties.Add(HtmlEdit.PropertyNames.Id, "_ctl0_LoginPlaceHolder_soLogin_UserName");

Lets see how we can refer this objects in the test

BrowserWindow mybrowser = BrowserWindow.Launch(new Uri("http://mywebsite.com")); statement supplies you with the browser object that you will be dealing with the Log-in scenario.

You can invoke the above method like TestPageobjectClass.username(mybrowser) where required browser object argument is supplied at the time of the test.

Then you can continue manipulating control's various functionalists to get your work done!


No comments:

Post a Comment