Rspec和watir网络驱动程序;断言元素不存在

Rspec和watir网络驱动程序;断言元素不存在,rspec,watir-webdriver,assertions,Rspec,Watir Webdriver,Assertions,我使用的是Rspec和watir webdriver,我正在努力编写一个通过测试,以便在屏幕上删除某个项目时,该项目不存在——因此,通过这里就是我要做的: # this method gets the specific item I want to interact with def get_item(title) foo = @browser.div(:text, title).parent.parent return foo end # This method looks

我使用的是Rspec和watir webdriver,我正在努力编写一个通过测试,以便在屏幕上删除某个项目时,该项目不存在——因此,通过这里就是我要做的:

# this method gets the specific item I want to interact with
def get_item(title)
    foo = @browser.div(:text, title).parent.parent
    return foo
end

# This method looks at an element grid that contains all the items
def item_grid_contents
    grid = @browser.div(:class, 'items-grid')
    return grid
end
当我删除该项时,以下是我尝试编写断言的几种方法:

expect(get_item(title)).not_to be_present # => fails, unable to locate element
expect(item_grid_contents).not_to include(get_item(title)) # => fails, same error
在本例中,我希望确保元素无法定位,并且应该通过。 有没有其他方法来编写它以将其视为一个过程?

这是因为
#parent
方法使用javascript查找Selenium元素,并从中创建Watir元素。对于大多数元素,Watir创建了一个xpath定位器,但lazy会加载它,以便在与之交互之前它不必存在。通过此javascript调用,它会立即被评估,因此它会立即失败,而无法响应
#exists?
#present?
方法

这是一个很好的例子,说明了为什么我不喜欢当前的parent实现。我已经花了一段时间在修改代码上,因为我还没有一个令人信服的理由去推动它。这是一个令人信服的理由

就目前而言,我认为这将对您有效:

@browser.div(:text, title).element(xpath: './ancestor::*[2]')

为什么不检查是否存在
div(:text,title)
?毕竟,如果外部容器(父容器)不见了,那么内容(子容器)也不见了

或者(不确定此处是否有expect语法..)

小音符是红宝石。。通过不使用中间变量,可以使这些方法更简洁。事实上,您甚至可以消除隐含的“return”(但我们中的一些人喜欢它的可读性),而且大多数编写Ruby代码的人都使用两个空格缩进。因此,对于上述方法,您可以使用

def get_item(title)
  return @browser.div(:text, title).parent.parent
end
甚至

def item_grid_contents
  @browser.div(:class, 'items-grid')
end

有没有不使用xpath的方法可以做到这一点?除非我们Watir团队更改父方法的实现。我的愿望是用xpath实现它。xpath唯一的缺点是可读性,至少这一点非常明显。谢谢——这是对我来说最简单的解决方案。
def get_item(title)
  return @browser.div(:text, title).parent.parent
end
def item_grid_contents
  @browser.div(:class, 'items-grid')
end