This is a migrated thread and some comments may be shown as answers.

How can i transfer all element identification variable to another class?

5 Answers 56 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Anton
Top achievements
Rank 1
Anton asked on 30 Aug 2012, 03:19 PM

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");
}
but in tests i should specify type of element

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

Sort by
0
Cody
Telerik team
answered on 30 Aug 2012, 08:32 PM
Hi Anton,

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
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Anton
Top achievements
Rank 1
answered on 31 Aug 2012, 12:53 PM

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();

0
Cody
Telerik team
answered on 05 Sep 2012, 04:01 PM
Hello Anton,

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);
        }
    }
}

All the best,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Nazar
Top achievements
Rank 1
answered on 19 Apr 2013, 03:10 PM
Could you please give an example for Silverlight too? 
0
Boyan Boev
Telerik team
answered on 24 Apr 2013, 11:06 AM
Hello Nazar,

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
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Tags
General Discussions
Asked by
Anton
Top achievements
Rank 1
Answers by
Cody
Telerik team
Anton
Top achievements
Rank 1
Nazar
Top achievements
Rank 1
Boyan Boev
Telerik team
Share this question
or