Reusable Objects

Why do we need to create reusable objects??
We can create pure hand coding by keeping object definition(or test control definition) inside the test method. But if you want to use the same test control in another testMethod(), you again need to create object for the same control. This is a disadvantage. For example, if the developer changes the automation id of any control, you need to change every search property where ever you have created the object. So the ideal way of doing is, we create the test control object outside the TestMethod(). For that we can use shared object repository. You can create any number of shared object repositories based on functionality and based on web pages.

Here, we just take a google website and click on any control. Take a new project and add two separate folders for shared objects and testscripts. So that you can easily differentiate. Add a class file to the Shared objects folder and in this class we can add all control objects.

BrowserWindow br;

public LoginControls(BrowserWindow browserWindowObj)
{
br = browserWindowObj;
}

Generally when we create an object, we will pass browser window object to the control objects as a parent.
So here first we have initialized a browser object and then using a constructor we can get the browser window object. That means when ever we create UI test objects using this class, the constructor automatically initializes the browser object. Then we need to add all the controls which we want to interact with. Since we are using web application, we need to add namespace at the beginning.
ie,
using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;

Now for example, we just want to click on ‘Gmail’ link. so we need to spy the control and create the object.

Screenshot (16)

Then,

private HtmlHyperlink gmailLink; // Vairable
public HtmlHyperlink GmailLink // Property
{
get
{
if (gmailLink == null)
{
gmailLink = new HtmlHyperlink(br);
gmailLink.SearchProperties.Add(“Id”,”gb_23″);
}

return gmailLink;
}
}
The control type is ‘Hyperlink’, so the class name will be ‘HtmlHyperlink’ and initialized a link control. Then we are just creating a property for that and encapsulating it. We created only get() method and skipped Set() method. That means we are just allowing somebody to access this control and they are not allowed to add any property for this particular control and this is a reusable control.
So in get() method, using if condition we are verifying whether the control is already created or not. And if not, we are creating that control and we are passing browser object. Then we can add proper search properties. So when ever you refer to the control variable, entire get method will execute. That means we can reuse this control where ever we want to use it.
When we click on gmail link, it will redirect to the gmail login page. There we will enter login id and password and click on sign in button. So for that controls also we will create reusable objects.

Screenshot (17)

You can see the properties of emailid textbox properties in the above image.
Then,

private HtmlEdit emailEdit; // Vairable
public HtmlEdit EmailEdit // Property
{
get
{
if (emailEdit == null)
{
emailEdit = new HtmlEdit(br);
emailEdit.SearchProperties.Add(“Id”, “Email”);
}

return emailEdit;
}
}
private HtmlEdit passwrordEdit; // Vairable
public HtmlEdit PasswrordEdit // Property
{
get
{
if (passwrordEdit == null)
{
passwrordEdit = new HtmlEdit(br);
passwrordEdit.SearchProperties.Add(“Id”, “Passwd”);
}

return passwrordEdit;
}
}
Then create an object for signin button.

Screenshot (18)

private HtmlButton signInBtn; // Vairable
public HtmlButton SignInBtn // Property
{
get
{
if (signInBtn == null)
{
signInBtn = new HtmlButton(br);
signInBtn.SearchProperties.Add(“Id”, “signIn”);
signInBtn.SearchProperties.Add(“TagName”, “INPUT”);
}

return signInBtn;
}
}
We can add any unique search properties here. Like this we can create any number of reusable objects and use them in anywhere within the project. After click on signin button, we can verify if the login has successfully completed or not. For that just checkout some common control objects are present there or not(like inbox, username on top right, etc..). So we are adding some other controls here for verification purpose.

Screenshot (20)

Then,

private HtmlHyperlink userIcon; // Vairable
public HtmlHyperlink UserIcon // Property
{
get
{
if (userIcon == null)
{
userIcon = new HtmlHyperlink(br);
userIcon.SearchProperties.Add(HtmlHyperlink.PropertyNames.Class, “gb_s gb_W gb_k”);
userIcon.SearchProperties.Add(HtmlHyperlink.PropertyNames.AbsolutePath, “/u/0/me”);
}

return userIcon;
}
}
Here in search properties, instead of hardcoding the property names we are using predefined properties. Once you click on this link you should be able to see your email id over there. For that we will create an object. You can see in below image.

Screenshot (21)

then,
private HtmlDiv useremailIdDiv; // Vairable
public HtmlDiv UseremailIdDiv // Property
{
get
{
if (useremailIdDiv == null)
{
useremailIdDiv = new HtmlDiv(br);
useremailIdDiv.SearchProperties.Add(HtmlDiv.PropertyNames.Class, “gb_6”);
}

return useremailIdDiv;
}
}

And finally we click on signout link.

private HtmlHyperlink signOutLink; // Vairable
public HtmlHyperlink SignOutLink // Property
{
get
{
if (signOutLink == null)
{
signOutLink = new HtmlHyperlink(br);
signOutLink.SearchProperties.Add(HtmlHyperlink.PropertyNames.Id, “gb_71”);
}

return signOutLink;
}
}
Like this we can create any number of objects and reuse them in our whole project.

How do we use reusable object?
Just create a TestMethod() and we will use the shared repositary in this testmethod.
Inside the testMethod(),
Public TestMethod[]
{
BrowserWindow.CurrentBrowser = “ie”;
BrowserWindow br = BrowserWindow.Launch(“www.google.com”);

LoginControls lControls = new LoginControls(br);

Mouse.Click(lControls.GmailLink);

lControls.EmailEdit.Text = “codedui.trainer@gmail.com”;
lControls.PasswrordEdit.Text = “currectPassword@1”;

Mouse.Click(lControls.SignInBtn);

Mouse.Click(lControls.UserIcon);

string expectedEmaild = “codedui.trainer@gmail.com”;
string actualEmaild = lControls.UseremailIdDiv.InnerText;

Assert.AreEqual(expectedEmaild, actualEmaild, “loged in email id not displayed when user icon is clicked!..”);

Mouse.Click(lControls.SignOutLink);

}
Here first we are launching the browser by creating browser object like we did before. Here we want to use reusable object which we created before. In general whenever we want to use a class variable in another class in a project, we can directly use it. In this case also we are using two classes, but we are referring a variable from different folder(ie, for shared objects and for testmethods we have added two folders in the beginning). So we need to refer to that particular folder from which we are using the class. So just add a namespace at the beginning.
ie,
using Framework.SharedUITestControls;

Here ‘framework’ is the project name and ‘SharedUITestControls’ is folder name. Thus we can easily refer to the classes which comes under ‘SharedUITestControls’ folder. In this case also, using ‘LoginControls’ class we are creating an object. So that all the controls under ‘LoginControls’ are available in this class also and we can easily refer to them. And in the next line, using Mouse.click() method we are clicking on gmail hyperlink. In this method we will associate ‘loginControl’ class object with control object. This method will call get method of ‘GmailLink’ and return the object.

Then we need to enter details emailid and password into the fields. To enter text into textbox we use ‘Text’ property of control object. so here using ‘lControls.EmailEdit.Text’ and ‘lControls.PasswrordEdit.Text’ we are providing some details. Again using Mouse.click() method clicking on signin button. Once we click on, it should verify whether it is logged in or not. So we are clicking on user icon. And we also want to verify if the logged in emailid is same with the given emailid. So we need to compare expected value with the actual value. Here expected value is what we are expecting. And actual value will be taken from the inner property of control object. You can checkout below.

Screenshot (22)

Assert method will compare both expected and actual values. If both are not equal, the error message will be displayed as a test result(you can see error message as a3rd parameter in assert method) or else if both values are equal the signout link will be clicked finally. Just run your test method and checkout.

The browser will be closed immediately when signout link is clicked. So if you want to wait till the signout process
completes and return to the home page you need to use the following line of code. We can check whether a control is
exist or not. So in this case we are taking emailid textbox.

lControls.EmailEdit.WaitForControlExist(60000)

‘WaitForControlExist’ is used to tell the control to wait for 60000 milliseconds.

Another Example

Let us take another example which uses shared control repository. For this we will test compose a new mail. First we
need to create objects for various controls. Just put all these objects in different class under shared objects folder.

BrowserWindow br;

public ComposeControls(BrowserWindow browserWindowObj)
{
br = browserWindowObj;
}

private HtmlDiv composeEmail;
public HtmlDiv ComposeEmail
{
get
{
if (composeEmail == null)
{
composeEmail = new HtmlDiv(br);
composeEmail.SearchProperties.Add(HtmlDiv.PropertyNames.InnerText, “COMPOSE”);
composeEmail.SearchProperties.Add(HtmlDiv.PropertyNames.Class, “T-I J-J5-Ji T-I-KE L3”);
}

return composeEmail;
}
}

private HtmlEdit toEmailEdit;
public HtmlEdit ToEmailEdit
{
get
{
if (toEmailEdit == null)
{
toEmailEdit = new HtmlEdit(br);
toEmailEdit.SearchProperties.Add(“Name”, “to”);
toEmailEdit.SearchProperties.Add(“TagName”, “TEXTAREA”);
}

return toEmailEdit;
}
}

private HtmlEdit subjectEmailEdit;
public HtmlEdit SubjectEmailEdit
{
get
{
if (subjectEmailEdit == null)
{
subjectEmailEdit = new HtmlEdit(br);
subjectEmailEdit.SearchProperties.Add(“Name”, “subject”);
}

return subjectEmailEdit;
}
}

private HtmlDocument bodyEmailDoc;
public HtmlDocument BodyEmailDoc
{
get
{
if (bodyEmailDoc == null)
{
bodyEmailDoc = new HtmlDocument(br);
bodyEmailDoc.SearchProperties.Add(“Class”, “editable LW-avf”);
}

return bodyEmailDoc;
}
}

private HtmlDiv sendBtn;
public HtmlDiv SendBtn
{
get
{
if (sendBtn == null)
{
sendBtn = new HtmlDiv(br);
sendBtn.SearchProperties.Add(“InnerText”, “Send”);
}

return sendBtn;
}
}

private HtmlDiv messageSentNotificationDiv;
public HtmlDiv MessageSentNotificationDiv
{
get
{
if (messageSentNotificationDiv == null)
{
messageSentNotificationDiv = new HtmlDiv(br);
// messageSentNotificationDiv.SearchProperties.Add(“Class”, “vh”);
messageSentNotificationDiv.SearchProperties.Add(HtmlDiv.PropertyNames.InnerText, “Your message has

been sent.”, PropertyExpressionOperator.Contains);
}

return messageSentNotificationDiv;
}
}

Here, we have created objects for compose mail link, to enter sender name, to enter subject line of mail, To write
message in the body section, to click on send link and finally to verify if the message has been sent.
Let us take another testmethod[]. In that we will write script for composing a new mail.

ComposeControls cControls = new ComposeControls(br);

Mouse.Click(cControls.ComposeEmail);

cControls.ToEmailEdit.Text = “codedui.trainer@gmail.com”;
cControls.SubjectEmailEdit.Text = “text – april 4 1”;

Keyboard.SendKeys(cControls.BodyEmailDoc, “hello!… how are you doing today??”);

Mouse.Click(cControls.SendBtn);

cControls.MessageSentNotificationDiv.WaitForControlExist(60000);

Assert.IsTrue(cControls.MessageSentNotificationDiv.Exists);

You can use the above code in earlier testscript(ie, after you log on to the gmail account). Here using class name we are creating object(1st line). So that all the control objects in that class are available in this testmethod[]. Then using Mouse.click() method we are clicking on compose mail link. Then by using text property, we are providing some details to enter into mailid and subject line textboxes. The body section is not a textbox, so we could not use text property here. Using ‘Sendkeys’ property of keyboard, we can pass message(5th line). Then we are clicking on send button. Here again we are using waitForControlExist property to wait till the message has been sent. And finally if the message sent notification is displayed, the test execution will be continued.

Like this you can create and reuse shared control objects instead of creating objects each time and every where.

Leave a comment