【问题标题】:Selenium c# - how do i test that an image src file exists (verifying if the image shows on the page)Selenium c# - 我如何测试图像 src 文件是否存在(验证图像是否显示在页面上)
【发布时间】:2017-02-10 18:39:34
【问题描述】:

我是 Selenium 和 c# 的新手,所以我走到了死胡同。我需要知道如何检查图像 src 文件是否存在。当我的意思是存在时,它是否显示在页面上(不是没有图像时出现的红色 x 框)。

我试过file.exists(@c://imagename);和System.File.Exists
我不知道这是否正确。

任何帮助都会很棒!我的头被这个炸了

谢谢

【问题讨论】:

  • 如果文件存在于您的机器或局域网上,我不确定为什么System.File.Exists 不起作用。您是如何尝试使用它的?
  • 我有一个字符串,它获得了 src 属性,然后调用它来查看文件是否存在。

标签: c# selenium


【解决方案1】:

假设图像的路径在 src 属性中是相对的,您需要计算出 URL,然后运行类似于此答案中概述的测试:

Test to see if an image exists in C#

如果您确实需要检查图像是否存在并且是否已部署(老实说,我会质疑这是否是一个 qorthwhile 测试),您可以使用类似于以下代码的内容:

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

它基本上检查 URL(在您的情况下是图像的),以查看图像是否存在。

如果您需要帮助,请转身询问;)

【讨论】:

  • 谢谢。我通过 selenium.GetAttribute("//descendant::*[@class='name'][1]/a/img/@src"); 找到图像路径这将返回结果 /StyleLib/test.jpg,它是图像路径。所以我知道图像存在我只是不知道如何测试它是否存在于页面上。希望这是有道理的
  • Lou - 为了清楚起见,您的测试实际上并不知道图像存在。无论浏览器是否能够加载图像,都会设置 SRC 属性。例如<IMG SRC="http://no.such.domain/no.such.jpg"/>是合法的,提取它的SRC属性会产生http://no.such.domain/no.such.jpg,即使浏览器显然没有下载到不存在的图片。
  • 好的,我明白了......所以为了验证图像是否存在,我只使用标准 iselementpresent??但不在 scr 上?
【解决方案2】:

我的解决方案是检查文件的长度。 您可以根据需要修改此解决方案:

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

【讨论】:

    【解决方案3】:

    你不能这样做,至少不能单独使用 Selenium。根据您正在测试的浏览器,您可能能够查看磁盘浏览器缓存并找到文件,但并非所有浏览器都将所有内容缓存到磁盘,并且找出文件名可能非常困难。

    如果没有 Selenium,当然可以使用 curl、wget 等。下载图像文件,或者您可以截屏浏览器并自己搜索“红色 X 框”。但两者都不是真正的答案。

    【讨论】:

      猜你喜欢
      • 2022-12-16
      • 2021-06-26
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多