This is a migrated thread and some comments may be shown as answers.
variables in HTMLFindExpression
1 Answer 59 Views
This is a migrated thread and some comments may be shown as answers.
Brandon
Top achievements
Rank 1
Brandon asked on 28 Feb 2012, 04:33 PM
I was working on generating my own Element Repository and I wanted to be able to create a page and set a property (in this case a link name) and find a span tag with the TextContent of that property. Unfortunately whenever a variable is used it ignores the TextContent expression and just finds the first span and returns that.

public HtmlSpan Link
{
    get
    {
        return Get<HtmlSpan>("TextContent=" + _link,"tagname=span");
    }
}

and

public class ExpressionDefinitions
{
    private HtmlFindExpression _link;
    public ExpressionDefinitions(string link)
    {
        string textcontent = "TextContent=" + link;
        _link = new HtmlFindExpression(textcontent, "tagname=span");
 
    }
    public HtmlFindExpression Link { get { return _link; } }
 
    public HtmlFindExpression LogOff = new HtmlFindExpression("tagname=button", "TextContent=Log Off");
}

I have tried various combinations of hard coded strings and variables and it appears only hard coded Strings work in these functions.  I don't know if it is an issue with the framework or a hole in my knowledge of c#.  Attaching the complete Pages class.

1 Answer, 1 is accepted

Sort by
0
Cody
Telerik team
answered on 06 Mar 2012, 01:46 AM
Hi Brandon,

I apologize for the delay responding back to you (we're slightly overloaded in support at the moment).

Can I safely assume you're trying to write code like this using your class?

WebEOC.Pages.Pages myPages = new WebEOC.Pages.Pages(Manager);
myPages.CtrlPanel.LinkText = "link text";
HtmlSpan span = myPages.CtrlPanel.Link;

The problem here is that every time you write the code "myPages.CtrlPanel" it's creating a brand new CtrlPanelPage object and returning that object to be used by .LinkText. These lines of code are doing this creation:
public CtrlPanelPage CtrlPanel
{
    get
    {
        return new CtrlPanelPage(_baseurl + "default.aspx", _manager.ActiveBrowser.Find);
    }
}

What happens in the end is that this object is literally thrown away immediately after being used.

Try these modifications:
1) Make your local variables static
2) Do not initialize _link in your constructor

public class CtrlPanelPage : HtmlPage
{
    private static string _link = "";
    private static string _textcontent = "TextContent=";
 
    public CtrlPanelPage(string url, Find find)
        : base(url, find)
    { }



P.S. You should change your _baseurl initialization to:
_baseurl = manager.Settings.Web.BaseUrl;

Greetings,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Asked by
Brandon
Top achievements
Rank 1
Answers by
Cody
Telerik team
Share this question
or