I m doing my first webtest using VS i m trying to get all urls from the page and them later use those urls.but i tried different things to get urls for example when i place extrace rule for html tag.start tag = a and attribute = href then it returns only first this is main issue.
2nd how i can get my my context variable value .following is the code.
Thanks
awais
WebTestRequest request1 = new WebTestRequest("http://www.worldgolf.com/courses/usa/alabama/");
ExtractAttributeValue extractionRule1 = new ExtractAttributeValue();
extractionRule1.TagName = "a";
extractionRule1.AttributeName = "href";
extractionRule1.MatchAttributeName = "";
extractionRule1.MatchAttributeValue = "";
extractionRule1.HtmlDecode = true;
extractionRule1.Required = true;
extractionRule1.Index = 0;
extractionRule1.ContextParameterName = "";
extractionRule1.ContextParameterName = "values";
request1.ExtractValues += new EventHandler
//string a = this.Context["address"].ToString();
yield return request1;
aq
-------------------------------------------------------------------------------------
Hi Awais,
Here is a complete coded webtest that shows retreiving all URL's from anchor tags that contain hrefs. The urls are all stored in a List
1 //------------------------------------------------------------------------------
2 //
3 // This code was generated by a tool.
4 // Runtime Version:2.0.50727.3521
5 //
6 // Changes to this file may cause incorrect behavior and will be lost if
7 // the code is regenerated.
8 //
9 //------------------------------------------------------------------------------
10
11 namespace TestExperimentMar09
12 {
13 using System;
14 using System.Collections.Generic;
15 using System.Text;
16 using Microsoft.VisualStudio.TestTools.WebTesting;
17
18
19 public class WebTest3Coded : WebTest
20 {
21 List
22
23 public WebTest3Coded()
24 {
25 this.PreAuthenticate = true;
26
27 }
28
29 public override IEnumerator
30 {
31
32 this.PostRequest += new EventHandler
33 WebTestRequest request1 = new WebTestRequest("http://msdn.microsoft.com/");
34 request1.ExpectedResponseUrl = "http://msdn.microsoft.com/en-us/default.aspx";
35 yield return request1;
36 request1 = null;
37
38
39 }
40
41 void WebTest3Coded_PostRequest(object sender, PostRequestEventArgs e)
42 {
43 //lets make sure that we have a valid HTML response before we start to work on the Response.HtmlDocument
44 if (e.Response.IsHtml)
45 {
46 //check a filtered list of html tags that are anchor tags
47 foreach (HtmlTag htmlTag in e.Response.HtmlDocument.GetFilteredHtmlTags(new string[] {"a"}))
48 {
49 //makes sure our htmlTag has some attributes, otherwise no need to continue
50 if (htmlTag.Attributes != null)
51 {
52 //loop through all the htmlTag.attributes to see if what we need is there
53 foreach (HtmlAttribute attributeTag in htmlTag.Attributes)
54 {
55 //lets make sure we dont have a bad htmlTag
56 if ( !string.IsNullOrEmpty(attributeTag.Name) )
57 {
58 //we are only concerned with href attributes , other checks could be done here
59 if (attributeTag.Name.Equals("href", StringComparison.InvariantCultureIgnoreCase))
60 {
61 //if the list has the url we dont need to add it again
62 if (! this.urlList.Contains(attributeTag.Value))
63 {
64 //add the url to our master list
65 this.urlList.Add(htmlTag.GetAttributeValueAsString("href"));
66 }
67
68 }
69 }
70 }
71 }
72 }
73 }
74 }
75 }
76 }
77
------------------------------------------------------------------------------
Thanks a lot :)
awais
-------
aq
No comments:
Post a Comment