Wednesday, 16 December 2009

How to get Source code of the target web page

Sometimes during the automation development I faced lots of problem when the source code functionality is disable of any target web page.

Then I thought do we have any alternative to solve this problem?
I searched/goggled somewhere here and there. At last I got some snip of code.

I customize the code in such a way that it helped me in 2 ways.
1. When source code functionally is disable of the target web page/web site.
2. When something failed (no control found) during running of automation code then just get the source code and save it in text file. Latter it would help me to find out why my automation code failed or Check for the controls id/value/text etc on that web page.

Below is the code for that

public void GetSourceCode()
//Target website/web page
string strUrlOfWebPage = "";
//Location to store source code in text file.
string strTextFileLocation = "c://SourceFile.txt";
System.Net.WebRequest request = System.Net.HttpWebRequest.Create(urlOfWebPage);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader readerSource = new System.IO.StreamReader
string strSourceForTextFile = readerSource.ReadToEnd();
System.IO.File.WriteAllText(strTextFileLocation, strSourceForTextFile);

catch(Exception ex)

Thanks ,
Md. jawed