tags:

views:

596

answers:

5

Hi

I'm working on WatiN automation tool. I'm having problem in regular expression. I've situation where i have to enter some text and click on a button in the popup window. I'm using AttachToIE method and URL attribute("http://192.168.25.10:215/admin/SelectUsers.aspx?Type=FeedbackID=ef5ad7ef5490-4656-9669-32464aeba7cd") of the popup to attach to the popup.

The problem is each time the popup appears the ID value in the URL changes. So i'm not able to access the popup. can anyone plz help with this by giving me Regular Expression for the changing value of ID in the below URL ("http://192.168.25.10:215/admin/SelectUsers.aspx?Type=FeedbackID=ef5ad7ef5490-4656-9669-32464aeba7cd")

thanking you

A: 

Hi,

I'm not familiar with WatiN but it looks like it's runs on .Net so perhaps this might help?

var desiredId = "000000000000-0000-0000-000000000000";
var url = "http://192.168.25.10:215/admin/SelectUsers.aspx?Type=FeedbackID=ef5ad7ef5490-4656-9669-32464aeba7cd&someMoreStuff";
var pattern = @"(?i)(?<=FeedBackId=)[-a-z0-9]+";
var result = Regex.Replace(url, pattern, desiredId);
Console.WriteLine(result);

//Output:  http://192.168.25.10:215/admin/SelectUsers.aspx?Type=FeedbackID=000000000000-0000-0000-000000000000&amp;someMoreStuff

The following pattern should have the same affect but is more defensive. It should only match stuff in the query string, it requires the id to be 35 characters and won't match similar parameter names like "PreviousFeedBackId".

var pattern = @"(?i)(?<=\?.*\bFeedBackId=)[-a-z0-9]{35,35}\b";

If you just want to extract the id:

var id = Regex.Match(url, pattern).Value;
Console.WriteLine(id);
//output: ef5ad7ef5490-4656-9669-32464aeba7cd
it depends
A: 

I don want to replace the value of ID. coz each time the popup appears the value of the ID changes by itself so i need a regular expression so that i don need to mention the value of ID, instead i want to put a regular expression for ID value so that i can accept any value..

@vinaykumar - this should be a comment to the answer above by @itdepends.
Jose Basilio
Hi vinay, Sorry, I'm not sure I understand the requirement yet. With a Regex one usually matches or replaces part of a string. If the input is the url in your original question which part do you want to match/replace? (I guessed it was the id value). And what do you want to do with it: just match/find it or replace it? (if you want to match/find the id value then you can use the same pattern but with the Match method instead Replace.)
it depends
+1  A: 
string baseUrl ="http://192.168.25.10:215/admin/SelectUsers.aspx?Type=FeedbackID="
Regex urlIE= new Regex(baseUrl + "[\\wd]+", RegexOptions.IgnoreCase);
IE ie = IE.AttachToIE(Find.ByUrl(urlIE);
andreja
+1  A: 

It appears that you have a URL with 2 query string parameters Type and ID and your pattern is:

"http://192.168.25.10:215/admin/SelectUsers.aspx?Type=Feedback&amp;ID={some id}"

You can use the Find.ByUrl() attribute constraint method and pass it to AttachToIE() as shown below with the regex for matching that pattern.

string url = "http://192.168.25.10:215/admin/SelectUsers.aspx?Type=Feedback&amp;ID="
Regex regex = new Regex(url + "[a-z0-9]+", RegexOptions.IgnoreCase);
IE ie = IE.AttachToIE(Find.ByUrl(regex));
Jose Basilio
A: 

WatiN has a feature where in we can use the url by neglecting the query string. Below is the code which is working fine for me.

string baseUrl = "http://192.168.25.10:215/admin/SelectUsers.aspx"; IE ie = IE.AttachToIE(Find.ByUrl(baseUrl,true));