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

Finding Dynamic id for identifying object

3 Answers 217 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Chakravarthy
Top achievements
Rank 1
Chakravarthy asked on 11 Feb 2011, 01:50 PM
Hi Telerik Team,

I have sceanrio where the controls in page are dynamic & those controls are in table of the Web page. I have used the below code snippet for identfying objects. But  i am having a problem of identfying object because the "id" we are using for identifying object is changing dynamically. For Example id is "attribute_2_3003_3128" when we scripted, but it is change now to atrribute_4_3003_3128. Every time the single digit number in between is changed. Rest of the ID remains same. Please let me know how can we solve this issue.

Browser

 

 

ie = Manager.ActiveBrowser;
FramesCollection f = ie.Frames;
Browser b = f.ById("RAD_SPLITTER_PANE_EXT_CONTENT_ContentRadPane");
HtmlTable mainTable = b.Find.ById<HtmlTable>("tblContainer");
HtmlTableRow tablerow = mainTable.Find.ById<HtmlTableRow>("tblRel");
HtmlTable table = tablerow.Find.ById<HtmlTable>("tblmain");
int count = table.BodyRows.Count;
string proddes = ((string)(System.Convert.ChangeType(Data["ProductDesc"], typeof(string))));
string enabled = ((string)System.Convert.ChangeType(Data["Enabled"], typeof(string)));
for (int i=1; i < count - 1; i++){
HtmlTableRow row = table.BodyRows[i];
HtmlTableCell cell = row.Cells[1];
if (cell.InnerText.Equals("Product Description")){
HtmlTableCell cell1 = row.Cells[3];
/
/Regex regex = new Regex("""[?0-9]{1}");
//Console.WriteLine(regex.ToString());
//Console.ReadLine();
HtmlInputText text = cell1.Find.ById<HtmlInputText>("attribute_2_3003_3128"

);
text.Text = proddes;
}

 

 



Regards,
Kalyan Uppalapati
Riversand Technologies.

3 Answers, 1 is accepted

Sort by
0
Stoich
Telerik team
answered on 11 Feb 2011, 03:52 PM
Hi Chakravarthy,
    we have a KB article on this. Check it out here:
http://www.telerik.com/automated-testing-tools/support/kb/test-automation/working-around-dynamiclly-generated-id-attributes.aspx

In your case the solution should be pretty straightforward. I assume this piece of code is causing you trouble:

Find.ById<HtmlInputText>("attribute_2_3003_3128");

We want to search by ID but we need something a bit more flexible. Replace the above expression with this:
Find.ByExpression<HtmlInputText>("id=?_3003_3128");

=? in this expression means ends with. So you're searching for an element who's ID ends with _3003_3128.
This should avoid dynamic ID problems since you state that this part of the ID remains constant.

You can find more info on using Find.ByExperssion here:
http://www.artoftest.com/support/webaii/topicsindex.aspx?topic=byexpression

Let me know if you're having trouble getting this to work!

One question: why are you using so much code? WebUI Test Studio is designed to avoid code as much as possible. You could record all of these actions from the GUI without writing a single line of code.

Greetings,
Stoich
the Telerik team
Interested in Agile Testing? Check out Telerik TV for a recording of Automated Testing in the Agile Environment
0
Chakravarthy
Top achievements
Rank 1
answered on 14 Feb 2011, 07:11 AM
Hi Stoich,

Thanks for Reply, But i am having problem in using Find.ByExpression, beacause when i am using Find.ByExpression it is not identifying the object irrespective i gave complete correct "id" of the textbox or id = ?_3003_3128. Please let me know how can i use tip with Find.ByID. I tried with the below, but it didn't worked out.

HtmlInputText

 

 

text = cell1.Find.ById<HtmlInputText>("?_3003_3128");

Note: The below link for KB art

Regards,
Kalyan Uppalapati

0
Stoich
Telerik team
answered on 14 Feb 2011, 03:12 PM
Hi Chakravarthy,
    unfortunately you can't Find.ById in this way.

However, I assure you that Find.ByExpression works.

Try this simple example: record a test that navigates to www.google.com. The source code for main google page includes and element with id=gb_8

Try all of the following expression and you'll see they'll all return the correct element:
Find.ByExpression("id=gb_8"); //search by complete ID      
Find.ByExpression("id=~b_8"); //search by contains      
Find.ByExpression("id=?b_8"); //search by endsWith

 
There's absolutely no reason for ByExpression not to work if ById works - please recheck.

Let me know how it goes!     

Kind regards,
Stoich
the Telerik team
Interested in Agile Testing? Check out Telerik TV for a recording of Automated Testing in the Agile Environment
Tags
General Discussions
Asked by
Chakravarthy
Top achievements
Rank 1
Answers by
Stoich
Telerik team
Chakravarthy
Top achievements
Rank 1
Share this question
or