Selenium:如何断言图像显示在网页的某个部分下

问题描述 投票:0回答:1

我正在尝试编写能够执行以下操作的测试: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调用提供了一个不存在异常的元素,但我不知道为什么我尝试了所有可以从inspect元素获取的Bys。

我接近这个吗?如果没有,我可以做些什么不同的事情?

selenium selenium-webdriver selenium-chromedriver selenium-ide build-automation
1个回答
0
投票

如果要检查图像是否存在于某个部分下,则必须为该部分创建一个webelement。

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

现在在section元素下创建一个image元素。

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

现在检查图像是否存在。

boolean imagePresent = image.isDisplayed();

现在断言布尔结果。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.