开发者

Selenium c# - how do i test that an image src file exists (verifying if the image shows on the page)

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


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 ;)


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;
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜