【问题标题】:Selenium: How to assert that image is displayed under a certain section of a webpageSelenium:如何断言图像显示在网页的某个部分下
【发布时间】:2019-07-17 21:45:38
【问题描述】:

我正在尝试编写一个能够执行以下操作的测试: 1. 导航到一个网站。 2. 导航到菜单下的页面。 3. 进入该页面后,断言我想要的图像显示在标有“SECTION”的部分下。

这是我的代码(方法 1):

public void test1() throws Exception {
   WebElement compare_image = driver.findElement(By.linkText("URL link where the image is located"));

   driver.get("website URL");

   WebElement image = driver.findElement(By.cssSelector("cssSelector for image from FireFox -> inspect element -> copy CSS selector"));

   assertEquals(image, compare_image); }

我对 Selenium 和 QA 自动化非常陌生,因此我将不胜感激任何详细的帮助,因为到目前为止我的谷歌搜索还不够。它给了我一个元素不存在的 findElement 调用异常,但我不知道为什么当我尝试了所有可以从检查元素获得的 Bys 时。

我的处理方法正确吗?如果没有,我能做些什么不同的事情?

【问题讨论】:

  • 所以你想验证一个元素指向图像还是你想验证图像本身?例如IMG src 与两张图片的逐位比较是否正确?

标签: selenium selenium-webdriver selenium-chromedriver selenium-ide build-automation


【解决方案1】:

如果您想检查某个部分下是否存在图像,则必须为该部分创建一个网络元素。

WebElement section= driver.findElement(By.xpath("//img(@class=‘Section')"));

现在在 section 元素下创建一个图像元素。

WebElement image= section.findElement(By.xpath("//img(@class=‘Test Image')"));

现在检查图像是否存在。

boolean imagePresent = image.isDisplayed();

现在断言布尔结果。

assertTrue(imagePresent, “No image is exist”);

注意:请注意部分和图片的定位器,因为您没有为其提供 Html。代码将完美运行。

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 2012-07-26
    • 2021-02-21
    • 1970-01-01
    相关资源
    最近更新 更多