Check the presence of a website

Class library usage, coding and language questions.
rneuber1
Posts: 5
Joined: Wed Jul 04, 2012 2:14 pm

Check the presence of a website

Post by rneuber1 » Wed Jul 04, 2012 4:37 pm

Hi all,

I would simply like to check the presence of a given website (or more precisely, the presence of a file on the website) from within a UserCodeModule written in C#.

So my SUT is the remote Webserver, all I want to do is (pseudo code).

Code: Select all

string URL="http://myserver.com/jquery.js";
WebPage.Navigate(URL);
Validate.isFalse(WebPage.Valid);
Any idea how to achieve this?

\Ralf

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Check the presence of a website

Post by Support Team » Thu Jul 05, 2012 4:09 pm

Hi,

There are more possibilities how you can check if a page is successfully loaded.
You can use the following code to wait until the page is loaded:
WebPage.WaitForDocumentLoaded();

or you can search for an element inside this page. Choose an element which is for sure there if the site is completely loaded. You can check it with one of the Validate.Exists(...) methods.
For a detailed description please take a look at the following link: Validate Class.
Please take also a look at: Waiting for UI Elements - Repository Timeouts

Regards,
Markus
Ranorex Support Team
.
Image

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: Check the presence of a website

Post by sdaly » Fri Jul 06, 2012 8:22 am

Another way -

This will pass.

Code: Select all

Validate.IsTrue(FileExistsOnServer("http://www.ranorex.com/forum", "check-the-presence-of-a-website-t3554.html"), "Validating file exists on server");
This will fail.

Code: Select all

Validate.IsTrue(FileExistsOnServer("http://www.ranorex.com/forum", "doesnotexist.html"), "Validating file exists on server");

Code: Select all

Using System.Net;

public static bool FileExistsOnServer(string baseUrl, string filename){
			try {
				using(WebClient wc = new WebClient()){
                                        //save file to current working directory in case you want to do anything with it!
					wc.DownloadFile(baseUrl + "/" + filename, filename);
				}
			} catch (WebException wex) {
				HttpWebResponse httpResponse = (HttpWebResponse)wex.Response;
				if(httpResponse.StatusCode == HttpStatusCode.NotFound){
					return false; //file not found
				}
				//somethig else went wrong
				throw;
			}
			return true;
		}