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

6 posts, 0 answers
  1. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 30 Aug 2012 Link to this post

    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?

  2. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 30 Aug 2012 Link to this post

    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
  3. Anton
    Anton avatar
    54 posts
    Member since:
    Sep 2011

    Posted 31 Aug 2012 Link to this post

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

  4. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 05 Sep 2012 Link to this post

    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
  5. Nazar
    Nazar avatar
    3 posts
    Member since:
    Dec 2012

    Posted 19 Apr 2013 Link to this post

    Could you please give an example for Silverlight too? 
  6. Boyan Boev
    Admin
    Boyan Boev avatar
    1040 posts

    Posted 24 Apr 2013 Link to this post

    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
Back to Top