1

im new to Selenium and c# so I've hit a dead end. I need to know how to check weather an images src file exists or not. When I mean exists, is it showing on the page (not the red x box you get when no image is present).

I have tried file.exists(@c://imagename); and System.File.Exists.
I don't know if this is correct or not.

Any help would be great!! My heads fried with this

Thanks

Kindle Q
  • 944
  • 2
  • 19
  • 28
Lou
  • 21
  • 1
  • 2
  • If the file exists on your machine or local area network, I'm not sure why `System.File.Exists` wouldn't work. How did you try and use it? – Chris Walsh Jun 02 '11 at 15:00
  • i had a string which got the src attribute and then called that to see if the file existed. – Lou Jun 02 '11 at 15:52

3 Answers3

1

My solutions was to check length of the file. you can modify this solution to your need:

    /// <summary>
    /// Gets the file lenght from location.
    /// </summary>
    /// <param name="location">The location where file location sould be located.</param>
    /// <returns>Lenght of the content</returns>
    public int GetFileLenghtFromUrlLocation(string location)
    {
        int len = 0;
        int timeoutInSeconds = 5;

        // paranoid check for null value
        if (string.IsNullOrEmpty(location)) return 0;

        // Create a web request to the URL
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(location);
        myRequest.Timeout = timeoutInSeconds * 1000;
        myRequest.AddRange(1024);
        try
        {
            // Get the web response
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

            // Make sure the response is valid
            if (HttpStatusCode.OK == myResponse.StatusCode)
            {
                // Open the response stream
                using (Stream myResponseStream = myResponse.GetResponseStream())
                {
                    if (myResponseStream == null) return 0;

                    using (StreamReader rdr = new StreamReader(myResponseStream))
                    {
                        len = rdr.ReadToEnd().Length;
                    }
                }
            }
        }
        catch (Exception err)
        {
            throw new Exception("Error saving file from URL:" + err.Message, err);
        }

        return len;
    }
cpoDesign
  • 8,953
  • 13
  • 62
  • 106
1

Assuming that the path to the image is relative in the src attribute you would need to work out the URL then run a test similar to the one outlined in this answer:

Test to see if an image exists in C#

If you really need to check if the image exists and has been deployed (I would question if this is a qorthwhile test to be honest) you could use something like the code below:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://full-path-to-your-image.png");
request.Method = "HEAD";

bool exists;
try
{
    request.GetResponse();
    exists = true;
}
catch
{
   exists = false;
}

It basically checks the URL (of the image in your case), to see if the images exists.

If you need a hand with it turn round and ask ;)

Community
  • 1
  • 1
Burt
  • 7,680
  • 18
  • 71
  • 127
  • Thanks. I find the image path by selenium.GetAttribute("//descendant::*[@class='name'][1]/a/img/@src"); this returns the result /StyleLib/test.jpg which is the image path. So I know that the image exists i just don't know how to test if it is present on the page. Hope that makes sense – Lou Jun 02 '11 at 15:21
  • Lou - Just to be clear, your test doesn't actually know that the image exists. The SRC attribute will be set regardless of whether the browser was able to load the image or not. For example, `` is legal, and extracting it's SRC attribute will produce `http://no.such.domain/no.such.jpg`, even though the browser obviously didn't get download the non-existent image. – Ross Patterson Jun 02 '11 at 15:33
  • ok i understand... so to just verify that the image is present, i just use the standard iselementpresent?? but not on the scr? – Lou Jun 02 '11 at 15:42
0

You can't do it, at least not solely with Selenium. Depending on which browser you're testing with, you might be able to look into the on-disk browser cache and find the file, but not all browsers cache everything to disk, and figuring out the filename may be very difficult.

Without Selenium, of course, you can use curl, wget, et al. to download the image file, or you could possibly screen-shot the browser and search for the "red X box" yourself. But neither is really a nice answer.

Ross Patterson
  • 9,527
  • 33
  • 48