简体   繁体   English

无法使用黄瓜step_definition定位元素,但能够通过水豚独立定位

[英]Unable to locate element with cucumber step_definition, but able to locate it with capybara stand-alone

I am testing a website that has both a vertical login form on the right, and a horizontal form on the bottom. 我正在测试一个网站,该网站的右侧既有垂直登录表单,又有底部有水平登录表单。 Both have identically named "email" and "password" fields. 两者都有相同名称的“电子邮件”和“密码”字段。 So I am employing capybara's within to scope for the fields I'm interested in. The interesting bits of the web form look like this: 所以我采用水豚的within ,以余地我感兴趣的领域的Web表单看起来像这样的有趣的位:

在此处输入图片说明

I have two separate projects, in which I am experimenting with sauce labs for automation. 我有两个单独的项目,其中我正在尝试调味酱实验室的自动化。 The first project is the capybara-only example, modified to test the page shown above. 第一个项目是仅水豚的示例,已对其进行了修改以测试上面显示的页面。 The second project is a cucumber implementation of the exact same tests. 第二个项目是完全相同的测试的黄瓜实现。 These are very simple, one-time hard-coded tests, just to get a proof of concept of the two techniques. 这些是非常简单的一次性硬编码测试,只是为了证明这两种技术的概念。

Here is the interesting bit of the capybara-only example: 这是仅水豚示例的有趣之处:

within(:css, ".right-container.login-form") do
    fill_in 'email', :with => "greg.gauthier+#{generate_email_suffix}@website.com"
    fill_in 'password', :with => 'l33tP@$$w0rd'
    click_button 'Submit'
end

Here is the interesting bit of the cucumber step_definition: 这是黄瓜step_definition的有趣部分:

When(/^The user enters his information$/) do
    within(:css, ".right-container.login-form") do #the page has duplicate forms
        fill_in 'email', :with => "greg.gauthier+#{generate_email_suffix}@website.com"
        fill_in 'password', :with => 'l33tP@$$w0rd'
        click_button 'Submit'
    end
end

When I run the capybara-only version, everything works great. 当我运行仅限水豚的版本时,一切工作正常。 The form gets filled in, and the email confirmation gets sent. 填写表格,并发送电子邮件确认。

However, when I run the cucumber version, I get this error: 但是,当我运行Cucumber版本时,出现此错误:

Unable to find css ".right-container.login-form" (Capybara::ElementNotFound)

How can this be? 怎么会这样? It's the exact same page, the exact same capybara method ( within , using the :css selector), and the exact same test code. 这是完全相同的页面,完全相同的capybara方法( within ,使用:css选择器),以及完全相同的测试代码。 What am I not getting (aside from the fact that I'm probably cuking it wrong )? 我没有得到什么(除了我可能认为这是错误的事实之外)?

Oh, here's what the require list looks like in the sauce_helper: 哦,这是sauce_helper中的需求列表:

Capybara-only version: 仅限水豚的版本:

require "sauce"
require "sauce/capybara"
require 'capybara/rails'
require 'capybara/rspec'

Cucumber version: 黄瓜版本:

require "sauce"
require "sauce/capybara"
require "sauce/cucumber"

Do I maybe need to include the extra capybara gems in the cucumber version? 我是否可能需要在黄瓜版本中包含额外的水豚宝石?

Ok, I'm embarrassed to admit this, but the reason for this question was ignorance of webdriver behavior, at least as it works with Capybara/Cucumber. 好的,我很尴尬地承认这一点,但是这个问题的原因是对Webdriver行为的无知,至少在与Capybara / Cucumber兼容的情况下如此。

The test in question is the second scenario in a set of scenarios. 有问题的测试是一组方案中的第二个方案。 Apparently, selenium resets the browser to blank, between each scenario. 显然,在每种情况下,硒都会将浏览器重置为空白。 So, test one works perfectly, but test two, three, and so forth fail on ElementNotFound because, of course... the page in question was not even loaded. 因此,测试一个完美地工作了,但是测试2,三个等等在ElementNotFound上失败了,因为……当然,所讨论的页面甚至没有被加载。

I added a Before hook to a hooks.rb file in my support path, and now it's working. 我在支持路径中的hooks.rb文件中添加了一个Before钩子,现在可以使用了。

Apologies for cluttering the question stream... 道歉让问题流混乱...

Try adding require 'capybara' prior to require 'sauce' in your "Cumcumber Version" 尝试在“黄瓜版本”中添加require 'capybara' require 'sauce'之前添加require 'capybara'

From sauce GitHub 从酱GitHub

## In your test or spec helper
require "capybara"
require "sauce/capybara"

Reasoning within calls find . 推理within来电find Sauce checks to see if a find method is defined an if so then it creates it's own alias called base_find if not it uses it's own find method which is not the same as capybara's. Sauce会检查查找方法是否已定义,如果是,则创建它自己的别名,称为base_find如果base_find ,则使用与水豚不同的自己的find方法。 I think they may have something to do with your issue since it is using sauce 's find method. 我认为他们可能与您的问题有关,因为它使用的是saucefind方法。

Not positive this is the answer but you can always give it a shot. 不是肯定的,这就是答案,但是您可以随时尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM