Page 1 of 1

Check the presence of a website

Posted: Wed Jul 04, 2012 4:37 pm
by rneuber1
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

Re: Check the presence of a website

Posted: Thu Jul 05, 2012 4:09 pm
by Support Team
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

Re: Check the presence of a website

Posted: Fri Jul 06, 2012 8:22 am
by sdaly
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;
		}