I need to Click on a link using an Expression rather than the Element. The href has a id reference that changes for every test. Since I can control the Text content, I figured that I should be able to find the string and then click on it. This will also allow the script to be data driven.
Here is what gets recorded:
Here is some code that I have tried.
I have attached a sample page, the link will result in a 404 error.
Here is what gets recorded:
Pages.HttpLocalhostTestHtml.SubmissionTwoLink.Click(
false
);
Here is some code that I have tried.
//HtmlFindExpression submission = new HtmlFindExpression("TextContent=Submission Two");
//submission.Click(false);
//HtmlAnchor sumission = new Find.ByExpression<HtmlAnchor>("TextContent=Submission Two");
//HtmlAnchor submission = Find.ByExpression("TextContent=Submission Two");
I have attached a sample page, the link will result in a 404 error.
7 Answers, 1 is accepted
0
Hi Garry,
you can edit the logic that WebUI uses to find your link without writing any code at all. Here's how it works:
open the test and click on a regular (i.e. not Coded) step that uses the element. When you click on the step you'll notice that in the Elements Explorer on of the elements becomes highlighted by a yellow arrow (see screenshot 1). Right-click on this element and choose "Edit Element". The Find Expression Builder comes up. From here you can change the way WebUI Test Studio identifies this object. Since you mention that the ID is the the problem you will want to change or remove this part of the Find Expression:
Id exact "something"
If only a part of the ID is different for each test you can do this:
Id contains "constant part of id"
However, if it's completely different then remove it. However, you might need to add something else just to make sure your element will be identified successfully.
For instance if the Find Expression consisted of two parts:
id="something" + tagname="a"
If you remove the first part of this expression WebUI will simply get the very first <a> element it finds and your test will most likely fail. To rectify the situation you'll need to add a different second condition. Specifically with an <a> I would look for a specific href string but there are many other possibilities.
This info should be enough to get you going - let me know if you need assistance with this (specifically with the Expression Builder).
Greetings,
you can edit the logic that WebUI uses to find your link without writing any code at all. Here's how it works:
open the test and click on a regular (i.e. not Coded) step that uses the element. When you click on the step you'll notice that in the Elements Explorer on of the elements becomes highlighted by a yellow arrow (see screenshot 1). Right-click on this element and choose "Edit Element". The Find Expression Builder comes up. From here you can change the way WebUI Test Studio identifies this object. Since you mention that the ID is the the problem you will want to change or remove this part of the Find Expression:
Id exact "something"
If only a part of the ID is different for each test you can do this:
Id contains "constant part of id"
However, if it's completely different then remove it. However, you might need to add something else just to make sure your element will be identified successfully.
For instance if the Find Expression consisted of two parts:
id="something" + tagname="a"
If you remove the first part of this expression WebUI will simply get the very first <a> element it finds and your test will most likely fail. To rectify the situation you'll need to add a different second condition. Specifically with an <a> I would look for a specific href string but there are many other possibilities.
This info should be enough to get you going - let me know if you need assistance with this (specifically with the Expression Builder).
Greetings,
Stoich
the Telerik team
Interested in Agile Testing?
Check out Telerik TV for a recording of
Automated Testing in the Agile Environment
0
Garry
Top achievements
Rank 1
answered on 26 Jan 2011, 12:40 AM
Thanks, that works.
Is there a way to Data Drive an element?
Is there a way to Data Drive an element?
0
Hi Garry,
are you referring to Data-Driven Find Expressions? If so this is already logged as a Feature Request but the functionality doesn't exists at the moment.
If you were referring to something else please clarify!
All the best,
are you referring to Data-Driven Find Expressions? If so this is already logged as a Feature Request but the functionality doesn't exists at the moment.
If you were referring to something else please clarify!
All the best,
Stoich
the Telerik team
Interested in Agile Testing?
Check out Telerik TV for a recording of
Automated Testing in the Agile Environment
0
Accepted
Something I need to calrify:
you can have a Data-Driven Find Expression for an element now but you'll have to write the Find Expression yourself. Here's very simple coded step that finds an anchor based on the text it contains and clicks it:
The Text Content for the search is Data-bound.
This code should get you started if you need a solution now. Let me know if you're having trouble getting this to work!
Kind regards,
you can have a Data-Driven Find Expression for an element now but you'll have to write the Find Expression yourself. Here's very simple coded step that finds an anchor based on the text it contains and clicks it:
String dataBinded = (
string
)(System.Convert.ChangeType(Data[
"ExcelColumnName"
],
typeof
(
string
)));
HtmlAnchor myAnchor = Find.ByExpression<HtmlAnchor>(
"TextContent="
+dataBinded);
myAnchor.Click();
This code should get you started if you need a solution now. Let me know if you're having trouble getting this to work!
Kind regards,
Stoich
the Telerik team
Interested in Agile Testing?
Check out Telerik TV for a recording of
Automated Testing in the Agile Environment
0
Garry
Top achievements
Rank 1
answered on 26 Jan 2011, 07:02 PM
Thanks, this is what I was looking for.
0
Santosh
Top achievements
Rank 1
answered on 27 Mar 2012, 12:00 PM
Hi Stoich,
I am trying to achieve Data-Driven Find Expression for an element that finds an anchor based on the text it contains and clicks it:
The Text Content is Data-bounded which i am picking from an Excel sheet. Below is the code which i am using.
This code works fine for the first data bound pick from the excel sheet and performs the click event, but when it moves to the second iteration and picks the Text Content from the excel sheet it pops a "NullReferenceException".
Please refer the http://www.telerik.com/automated-testing-tools/community/forums/test-studio/general-discussions/databind---click-event-issue.aspx where i have posted in detail regarding my issue.
Regards,
Santosh
I am trying to achieve Data-Driven Find Expression for an element that finds an anchor based on the text it contains and clicks it:
The Text Content is Data-bounded which i am picking from an Excel sheet. Below is the code which i am using.
string
SelectedCategory = ((
string
)System.Convert.ChangeType(Data[
"A"
],
typeof
(
string
)));
Pages.WUATV1123WS010.CphMainContentWrapReadyToUseReports1DrpCategoriesDiv.SelectItemByText(SelectedCategory);
HtmlDiv e =
new
HtmlDiv();
e = Find.ById<HtmlDiv>(
"ReadyToUse_Reports"
);
string
SelectedReport = (
string
)(System.Convert.ChangeType(Data[
"B"
],
typeof
(
string
)));
HtmlAnchor a = e.Find.ByExpression<HtmlAnchor>(
"tagname=a"
,
"TextContent="
+ SelectedReport);
a.Click();
This code works fine for the first data bound pick from the excel sheet and performs the click event, but when it moves to the second iteration and picks the Text Content from the excel sheet it pops a "NullReferenceException".
HtmlAnchor a = e.Find.ByExpression<HtmlAnchor>(
"tagname=a"
,
"TextContent="
+ SelectedReport);
Please refer the http://www.telerik.com/automated-testing-tools/community/forums/test-studio/general-discussions/databind---click-event-issue.aspx where i have posted in detail regarding my issue.
Regards,
Santosh
0
Hello Santosh,
if the following code:
is throwing the exception, then most likely the element e was not located. This means that the following statement:
actually returned Null on the second iteration. Unfortunately I can't offer much more assistance without additional information.
All the best,
Stoich
the Telerik team
if the following code:
HtmlAnchor a = e.Find.ByExpression<HtmlAnchor>(
"tagname=a"
,
"TextContent="
+ SelectedReport);
e = Find.ById<HtmlDiv>(
"ReadyToUse_Reports"
);
All the best,
Stoich
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Test Studio Trainings