Hello.
now i have main class with tests code and another class with elements.
class
MainTestPageObjects
{
public
static
readonly
HtmlFindExpression FooterLink =
new
HtmlFindExpression(
"id=footer"
,
"|"
,
"tagname=a"
);
}
Assert.AreEqual(
"http://mysite.com/"
,
Find.ByExpression<HtmlAnchor>(MainTestPageObjects.FooterLink).HRef);
How can i do it without specifying type of elements?
5 Answers, 1 is accepted
The Find.ByExpression allows you to not specify the type. It will always return an "Element" object. The complication I see you may run into is that the .HRef property only exists on an HtmlAnchor object. This if you remove it from that line of code you will get a compile error stating there is no definition for .HRef.
You could instead split it up like this if you desire:
Element myEleme = Find.ByExpression(MainTestPageObjects.FooterLink);
Assert.AreEqual(
"http://mysite.com/"
, myEleme.As<HtmlAnchor>().HRef);
Kind regards,
Cody
the Telerik team
Test Studio Trainings
I need something else:
I want to specify all y elements in another class but with type of element so there was no need to write type of element in the tests.
for example:
Element myElement = Find.ByExpression<HtmlAnchor>(MainTestPageObjects.FooterLink);
and in my another class with tests i only write:
[classname].myElement.Click();
when i use selenium it was very simply:
class
MainTestPageObjects
{
public
static
readonly
By FooterLink = By.Id(
"copyId"
);
}
Driver.FindElement(MainTestPageObjects.FooterLink).Click();
I apologize for the delay getting back to you.
I am not familiar with Selenium (other than the name and what it is) so I have no way to compare it to Test Studio. Looking at your code sample it appears they're not using any type casting, that everything is some sort of generic element or object.
The Telerik Testing Framework includes a lot of typecast classes you can use to take advantage of extended functionality. All of our typecast classes are derived from the "Element" class. With this in mind you have two design approaches you could use. You need to decide which works best for you and follow that approach:
1) Create your Elements class to return specific high level typecast objects such as HtmlTable, HtmlAnchor, etc. Then write your code that will expect this typecast and use it appropriately:
using
ArtOfTest.WebAii.Core;
using
ArtOfTest.WebAii.Controls.HtmlControls;
using
ArtOfTest.Common.UnitTesting;
namespace
MySampleTests
{
static
class
MyStaticElements
{
static
public
HtmlTable Table1
{
get
{
return
Manager.Current.ActiveBrowser.Find.ByExpression<HtmlTable>(
"id=table1"
); }
}
static
public
HtmlTable Table2
{
get
{
return
Manager.Current.ActiveBrowser.Find.ByExpression<HtmlTable>(
"id=table2"
); }
}
static
public
HtmlAnchor Anchor1
{
get
{
return
Manager.Current.ActiveBrowser.Find.ByExpression<HtmlAnchor>(
"id=anchor1"
); }
}
}
class
MyTests
{
public
void
Test1()
{
MyStaticElements.Table1.Click();
MyStaticElements.Table2.Click();
Assert.AreEqual<
int
>(5, MyStaticElements.Table1.Rows.Count);
MyStaticElements.Anchor1.Click();
Assert.AreEqual<
string
>(
"url1"
, MyStaticElements.Anchor1.HRef);
}
}
}
2) Create your Elements class to always return the generic Element object and typecast it in the calling code:
using
ArtOfTest.WebAii.Core;
using
ArtOfTest.WebAii.Controls.HtmlControls;
using
ArtOfTest.Common.UnitTesting;
using
ArtOfTest.WebAii.ObjectModel;
namespace
MySampleTests
{
static
class
MyStaticElements
{
static
public
Element Table1
{
get
{
return
Manager.Current.ActiveBrowser.Find.ByExpression(
"id=table1"
); }
}
static
public
Element Table2
{
get
{
return
Manager.Current.ActiveBrowser.Find.ByExpression(
"id=table2"
); }
}
static
public
Element Anchor1
{
get
{
return
Manager.Current.ActiveBrowser.Find.ByExpression(
"id=anchor1"
); }
}
}
class
MyTests
{
public
void
Test1()
{
MyStaticElements.Table1.As<HtmlTable>().Click();
MyStaticElements.Table2.As<HtmlTable>().Click();
Assert.AreEqual<
int
>(5, MyStaticElements.Table1.As<HtmlTable>().Rows.Count);
MyStaticElements.Anchor1.As<HtmlAnchor>().Click();
Assert.AreEqual<
string
>(
"url1"
, MyStaticElements.Anchor1.As<HtmlAnchor>().HRef);
}
}
}
Cody
the Telerik team
Test Studio Trainings
Here is the first scenario which returns specific high level typecast:
namespace
MySampleTests
{
static
class
MyStaticElements
{
static
public
TextBlock Text1
{
get
{
return
Manager.Current.ActiveBrowser.SilverlightApps()[0].Find.ByAutomationId<TextBlock>(
"textblock1"
) ; }
}
static
public
TextBlock Text2
{
get
{
return
Manager.Current.ActiveBrowser.SilverlightApps()[0].Find.ByExpression(
new
XamlFindExpression(
"AutomationID=textblock2"
)).As<TextBlock>(); }
}
}
class
MyTests
{
public
void
Test1()
{
MyStaticElements.Text1.User.Click();
MyStaticElements.Text2.User.Click();
Assert.AreEqual<
string
>(
"text"
, MyStaticElements.Text1.Text);
}
}
}
Here is the second approach with the generic FrameworkElements:
namespace
MySampleTests
{
static
class
MyStaticElements
{
static
public
FrameworkElement Text1
{
get
{
return
Manager.Current.ActiveBrowser.SilverlightApps()[0].Find.ByAutomationId(
"textblock1"
) ; }
}
static
public
FrameworkElement Text2
{
get
{
return
Manager.Current.ActiveBrowser.SilverlightApps()[0].Find.ByExpression(
new
XamlFindExpression(
"AutomationID=textblock2"
)); }
}
}
class
MyTests
{
public
void
Test1()
{
MyStaticElements.Text1.User.Click();
MyStaticElements.Text2.User.Click();
Assert.AreEqual<
string
>(
"text"
, MyStaticElements.Text1.Text);
}
}
}
Hope this helps. Kind regards,
Boyan Boev
the Telerik team
Test Studio Trainings