In today’s web world, where elements/controls in the web
application changing on daily basis, it is very hard to change corresponding
automation scripts to make it run on changed web application. Hence, developing
robust automation scripts should always be the first priority. One key step
towards robustness in Telerik Test Studio automation script is, be cautious
while using any method or property in the scripts. Just think, in which class method/property
defined? and are we using same class object to call that method/properly? I hope,
answer for those questions gives better idea on what you are doing and what you
need to do.
I would always suggest not to use narrowed wrapper class
until it really require. Let’s take few common scenarios.
Scenario1: Verifying inner text of an element.
Assume, Element in
the DOM:
<span
id="dash_heading"> Dashboard page of eval user </span>
One way to get the inner text is (Script_1):
HtmlSpan
Dash_Heading =
ActiveBrowser.Find.ById<HtmlSpan>("dash_heading");
string Text =
Dash_Heading.InnerText;
Another way is (Script_2):
HtmlContainerControl
Dash_Heading1 =
ActiveBrowser.Find.ById<HtmlContainerControl>("dash_heading");
string Text1 =
Dash_Heading1.InnerText;
Both are the correct and gives inner text of “dash_heading”
element. Here Script_1 fails if developer changes element type from span to div
but not Script_2. Reason behind is “HtmlContainerControl” is base class for
both “HtmlDiv” and “HtmlSpan” classes and property “InnerText” is defined in “HtmlContainerControl”
not in “HtmlDiv” or “HtmlSpan” classes. So if your requirement is to get
innerText go with Script_2 not with Script_1 to make your script robust.
Scenario2: Verifying
whether button is disabled or not?
Assume, Element in the DOM:
<button id="btn">Submit
</ button >
One way to verify is (Script_3):
HtmlButton
Button_Ele = ActiveBrowser.Find.ById<HtmlButton>("btn");
Assert.isTrue(Button_Ele.
IsEnabled);
Another way is (Script_4):
HtmlControl Button_Ele
= ActiveBrowser.Find.ById<HtmlControl>("btn");
Assert.IsTrue(Button_Ele.IsEnabled);
Above both (Script_3&4) are the correct to assert
whether button is disabled or not. Here in Script_3 we are using derived
wrapper class to get the IsEnabled property value where as in Script_4 we are
using base wrapper class in which IsEnabled property defined. Hence, Script_3
fails if developer changes element type from “HtmlButton” to any of “HtmlInputButton”
or “HtmlInputSubmit” but not Script_4.So if your requirement is to check
whether button enabled or disabled, I suggest to go with Script_4 not with Script_3.
Scenario3: Click
on Element/Control.
Assume, Element in the DOM:
<button id="ele1">OK</
button >
One way to click on the element is (Script_5):
HtmlButton OK_Ele =
ActiveBrowser.Find.ById<HtmlButton>("ele1");
OK_Ele.Click();
Another way is (Script_6):
HtmlControl OK_Ele =
ActiveBrowser.Find.ById<HtmlControl>("ele1");
OK_Ele.Click();
Both scripts click on ele1 element but script_6 will pass
even though element type changes from button to div/submit/Image etc.in future
but not Script_5. The reason is, Click() is the method defined in HtmlControl
class and this class is base class for entire HTML wrapper classes. It will be
good to use Script_6 instead of script_5 to make your code robust.
Click Here to read more about HTML Control
Element Wrappers Suite.