【问题标题】:Validate Image on Web Page Using Geb and Spock使用 Geb 和 Spock 验证网页上的图像
【发布时间】:2017-07-19 17:24:34
【问题描述】:

我是使用 geb 和 spock 的新手,但是当我只有代码的查看源时,我正在尝试验证网页上显示的图像。任何建议表示赞赏!我根据我之前编写的链接测试对这段代码进行了建模,所以我确定我遗漏了一些东西。我的页面文件的一个例子是:

// code not included where I have defined the url/etc. Below is content

someImage { $("img", file: "image-logo.png") }

我的规范页面的一个例子是:

def "Valid image"() {
        given: "an image checker"
        to SomePage
        when:
        someImage.hover()
        then:
        verifyAt()

【问题讨论】:

  • 在这种情况下,“验证”是什么意思,链接/它的校验和/...?
  • 抱歉,我正在尝试验证图片是否存在于网页上。我想我可以通过检查源页面上是否存在文件名来做到这一点,但我不确定这是否是处理这个问题的正确方法。
  • 您应该通过 id/css 选择器找到图像,然后验证 src 属性。

标签: automated-tests spock specifications geb


【解决方案1】:

您可以验证图像样式是否设置为displayed,任何具有css 属性display: none 的内容都无法通过webdriver API 进行交互。

我在 Geb 中对导航器对象使用 isDisplayed 方法时遇到了一些问题,因此我将执行以下操作:

在你定义页面内容的地方添加一个检查内容可以交互的方法(因此设置为显示):

someImage { $("img", file: "image-logo.png") }

Boolean contentDisplayed(Navigator content) {
    content
}

那么您的测试将如下所示:

def "Valid image"() {
    given: "an image checker"
    to SomePage
    when:
    someImage.hover()
    then:
    contentDisplayed(someImage)

这里值得注意的是,只要在此处访问 .hover 方法,就可以验证图像是否为 displayed 就 css 而言。因此,您可以将测试简化为:

def "Valid image"() {
    when: "an image checker"
    to SomePage
    then:
    contentDisplayed(someImage)

【讨论】:

  • 谢谢,这很有帮助!
猜你喜欢
  • 2019-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
相关资源
最近更新 更多