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

Find textbox by label.

7 Answers 186 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jason Evans
Top achievements
Rank 1
Jason Evans asked on 29 Mar 2011, 05:06 PM
Hi there.

In WatIn, you can do stuff like this:

WebBrowser.Current.TextFields.First(Find.ByLabelText(labelText)).TypeText(value);

Which finds a textbox by it's associated label and sets the text for that textbox. Is there a WebAii way of doing the above i.e. finding a textbox by the label associated to it?

Cheers.
Jas.

7 Answers, 1 is accepted

Sort by
0
Jason Evans
Top achievements
Rank 1
answered on 30 Mar 2011, 09:40 AM
Hi there.

OK, I think I worked it out. I use a HtmlFindExpression to do the job:

HtmlFindExpression expression = new HtmlFindExpression("tagname=label", "textcontent=Username");
 
var label = Find.ByExpression(expression);
 
if (label != null)
{
    if (label.ContainsAttribute("for"))
    {
        string textBoxName = label.GetAttribute("for").Value;
 
        Find.ById<HtmlInputText>(textBoxName).Text = "Got you!";
    }
}


In my tests, I have replaced the hardcoded

'textcontent=Username'

to

'textcontent=' + paramLabelText

This looks to be working fine for me.

Cheers.
Jas.
0
Stoich
Telerik team
answered on 31 Mar 2011, 05:18 PM
Hi Jason,
    Find Expressions are definitely the way to go. Check out this page if you haven't done so already:
http://www.artoftest.com/support/webaii/topicsindex.aspx?topic=byexpression

You can rewrite this:
HtmlFindExpression expression = new HtmlFindExpression("tagname=label", "textcontent=Username");
  
var label = Find.ByExpression(expression);
To look like this:
HtmlControl label = Find.ByExpression<HtmlControl>("TextContent=Female", "tagname=label");
But of course they both do the exact same thing.

Let me know if you require any additional assistance!

Best wishes,
Stoich
the Telerik team
0
Jason Evans
Top achievements
Rank 1
answered on 01 Apr 2011, 12:32 PM
Hi there.

Many thanks for the code tip, I will give that a go.

Cheers.
Jas.
0
Sanjay
Top achievements
Rank 1
answered on 02 Jul 2014, 10:53 PM
How to get the value of label from HtmlControl.
0
Cody
Telerik team
answered on 07 Jul 2014, 03:56 PM
Hello Sanjay,

Do you have a snippet of HTML code as an example of the kind of text/value you want to get? Here's a simple example taken from this tutorial of how a Label is typically used:
<form action="demo_form.asp">
  <label for="male">Male</label>
  <input type="radio" name="sex" id="male" value="male"><br>
  <label for="female">Female</label>
  <input type="radio" name="sex" id="female" value="female"><br>
  <input type="submit" value="Submit">
</form>

There are two labels on this page, one for the Male radio button and one for the Female radio button. Notice that both are simple HTML <label> elements with text content. Once you have an element you get the text value via the TextContent property like this:
Element maleLabel = ActiveBrowser.Find.ByAttributes("for=male");
string text = maleLabel.TextContent;

I hope this helps.

Regards,
Cody
Telerik
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
0
Sanjay
Top achievements
Rank 1
answered on 12 Aug 2014, 04:41 PM
I Coddy I have HTML Something like


<span class="x-panel-header-text" id="ext-gen308">rBook - Live Retail Market View</span>

I would like to get the " rBook - Live Retail Market View ".
How can i achieve that?

Thanks
0
Cody
Telerik team
answered on 14 Aug 2014, 05:55 PM
Hi Sanjay,

That value is what we call the "TextContent" of the element. This can be done either via non-coded step as either a Verify Text Content or Extract Text Content into Variable X. You should use our Elements Menu to create either one of those test steps.

You could also get it via code if you prefer:
HtmlSpan mylabel = ActiveBrowser.Find... [code here to find the right element]
string text = mylabel.TextContent;


Regards,
Cody
Telerik
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
Tags
General Discussions
Asked by
Jason Evans
Top achievements
Rank 1
Answers by
Jason Evans
Top achievements
Rank 1
Stoich
Telerik team
Sanjay
Top achievements
Rank 1
Cody
Telerik team
Share this question
or