我正在尝试编写能够执行以下操作的测试: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。
我接近这个吗?如果没有,我可以做些什么不同的事情?
如果要检查图像是否存在于某个部分下,则必须为该部分创建一个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。代码将完美运行。