如何保护Capybara测试不受任意文本更改的影响? [英] How can I protect my Capybara tests against arbitrary text changes?

查看:274
本文介绍了如何保护Capybara测试不受任意文本更改的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总的来说,我喜欢水豚的工作方式。我只是很难相信,在测试中整理用户可见的文本确实是个好主意;例如

In general I like how Capybara works. I just find it hard to believe that it's really a good idea to codify user-visible text in the tests; e.g.

click_link('Delete', match: :first)

好,所以我用这样的测试编写应用程序,然后我的设计师通过,或者品牌/营销人员看到删除并且适合,因为不要删除东西,我们删除它们(或者,点是任意的)。这样他们就可以对我的应用程序中按钮和链接的内容进行任意更改,即使我的应用程序运行正常,现在我所有的测试也都失败了。他们并不是真正的编码人员,所以了解如何处理我的测试并不是真正的责任,而且实际上,我认为我的测试不需要关心他们选择更改的文本。他们也不在乎我是否也需要将整个shebang翻译成其他语言。

OK, so I write my app with tests like this, and then my designer comes through, or the brand/marketing person sees "Delete" and has a fit because we do not "delete" things, we "remove" them (or whatever - point is, it's arbitrary). So they go through and make arbitrary changes to the content of the buttons and links in my app, and now all my tests are broken even though the app works fine. They're not really coders, so it's not really their responsibility to know what to do with my tests, and really, I don't think my tests should care what text they choose to change. They shouldn't really care if I need to translate the whole shebang to some other language either.

解决这个问题的明智方法是什么?我当时想我可以将CSS类添加到每个可测试的元素中并进行查找,但这似乎与设计者不符。我是否可以添加没有其他意义的元数据以进行测试?还是我看错了方向?

What's a sane way to address this? I was thinking I could just add a CSS class to each testable element and look for that, but that seems likely to run afoul of the designer. Can I just add otherwise-meaningless metadata for my tests to cue on? Or am I looking at this the wrong way?

推荐答案

1)我认为最简单的方法是使用元素ID为id的find方法或使用xpath和click()函数:

1) The easiest way I think to use find method with id of element or with xpath and click() function:

以下是示例:

feature 'Shall check devise authorization' do

  scenario "shall authorize User with correct email and password" do
    user = User.find(1)
    visit new_user_session_path
    fill_in "user[email]", with: user.email
    fill_in "user[password]", with: user.password
      # click_button "Sign in"
    find('input#sign_in_button').click()
      # find("//input[@id='sign_in_button']").click()
    has_selector? 'input#created_at_first'
  end

end

此处click_button 登录与

Here click_button "Sign in" the same as

find('input#sign_in_button').click() 
[id of element] 

,也与

find("//input[@id='sign_in_button']").click()
[xpath]

选择所需的内容。

如果您想按类别和ID选择元素,请在此处解决方案是:

If you would like to choose element by its class and id, here is the solution:

find('input#sign_in_button.btn.btn-primary.btn-success').click()
find("//input[@class='btn btn-primary btn-success'][@id='sign_in_button']").click()

另外,检查页面 capybara#finding 。有很多有用的信息。

Also it can be helpful to inspect page capybara#finding. There are a lot of useful information.

2)另一个选择是使用 capybara#scripting

2) Another option is to use capybara#scripting.

但这有点困难,因为您需要配置Capybara驱动程序以测试JS。

But it is a little harder, because you need to configure your Capybara driver for testing JS.

要配置Capybara驱动程序,应在Gemfile中添加gem'capybara-webkit'(您需要拥有libqtwebkit-dev)。

To configure your Capybara driver you should add gem 'capybara-webkit' in your Gemfile (you need to have libqtwebkit-dev).

sudo apt-get install libqtwebkit-dev
bundle install 

然后在您的spec_helper.rb文件中放置以下内容:

Then place in your spec_helper.rb something like this:

Capybara.current_driver = :webkit
Capybara.run_server = true #Whether start server when testing
#Capybara.server_port = 3000
#Capybara.default_selector = :css #:xpath #default selector , you can change to :css
Capybara.default_wait_time = 5 #When we testing AJAX, we can set a default wait time
Capybara.ignore_hidden_elements = false #Ignore hidden elements when testing, make helpful when you hide or show elements using javascript
Capybara.javascript_driver = :webkit

显示元素,以下规范将适用正确:

and the following spec will work correctly:

# -*- encoding : utf-8 -*-
require_relative '../spec_helper'

feature 'Shall check devise authorization' do

  scenario "shall authorize User with correct email and password", js: true do
    user = User.find(1)
    visit new_user_session_path
    fill_in "user[email]", with: user.email
    fill_in "user[password]", with: user.password

    page.execute_script("$('#sign_in_button').click()")

    has_selector? 'input#created_at_first'
  end

end

水豚-webkit驱动程序用于真正的无头测试。它使用QtWebKit启动渲染引擎进程。它也可以执行JavaScript。由于它不会加载整个浏览器,因此它比Selenium之类的驱动程序要快得多

The capybara-webkit driver is for true headless testing. It uses QtWebKit to start a rendering engine process. It can execute JavaScript as well. It is significantly faster than drivers like Selenium since it does not load an entire browser

有关更多详细信息,请访问测试-javascript-with-capybara-webkit

For more details please visit test-javascript-with-capybara-webkit

更新:

也可以浏览来自水豚源的此规范,可以帮助您了解如何查找元素或添加选择器:

Also you can browse this spec from capybara source, which can help you to understand how you can find elements or add selectors:

这篇关于如何保护Capybara测试不受任意文本更改的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆