如何使用 Minitest、Capybara、Selenium 执行 javascript 测试?
有很多关于如何使用 Capybara/Selenium/Rspec 执行 javascript 测试的示例,您可以在其中编写如下测试:
it "does something", :js => true do
...
end
但是使用 minitest 您无法通过第二个参数指示 selenium 执行测试。
有人对如何做到这一点有任何想法吗?
There are a lot of examples on how to perform javascript tests with Capybara/Selenium/Rspec in which you can write a test like so:
it "does something", :js => true do
...
end
However with minitest you can't pass a second parameter to instruct selenium to perform the test.
Does anyone have any ideas on how this can be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
:js
标志的作用非常简单。它将当前驱动程序从默认(rack-test)切换到另一个支持 javascript 执行的驱动程序(selenium、webkit)。你可以在 minitest 中做同样的事情:当然,为了方便你可以将其抽象成一个模块:
What
:js
flag is doing is very simple. It switches the current driver from default (rack-test) to another one that supports javascript execution (selenium, webkit). You can do the same thing in minitest:Of course you can abstract this into a module for convenience:
嗯,我注意到文档中的几行似乎说上面的操作只能在 Rspec 中完成,
但是,如果您使用 RSpec 或 Cucumber,您可能会想考虑保留更快的
:rack_test
作为default_driver
,并仅标记那些需要支持 JavaScript 的驱动程序的测试使用 :js =>;分别为 true
或@javascript
。Hmm I noticed a couple lines in the docs that seem to say that the above can only be done in Rspec
However, if you are using RSpec or Cucumber, you may instead want to consider leaving the faster
:rack_test
as thedefault_driver
, and marking only those tests that require a JavaScript-capable driverusing :js => true
or@javascript
, respectively.https://github.com/wojtekmach/minitest-metadata 似乎提供了一个解决方案这。
您可以执行以下操作:
https://github.com/wojtekmach/minitest-metadata seems to have provided a solution to exactly this.
You can do the following:
只是为了更新一些信息,有一个更强大的 selenium-webdriver 和 Ruby Bindings现在。
要使用 Rails 4 和 selenium-webdriver 从终端快速测试 Firefox 上的 JavaScript,您需要执行以下 4 个步骤:
将 gem 添加到 Gemfile
gem 'selenium-webdriver'
gem 'minitest-rails'
安装 gem
bundle install
生成测试用例(参考:官方指南< /a>)
bin/rails生成integration_test your_case_name
开始在你的测试用例中编写测试代码。
其实不用Capybara用ruby写也可以,用Capybara写case可以参考Github的Capybara< /p>
Minitest示例:
目前似乎缺乏关于如何顺利使用 selenium-webdriver、CI 服务器(Jenkins)和 Rails minitest 的资源,我创建了一个简单的项目,希望它可以帮助任何人快速入门 容易地:
Rails Selenium 工作案例 评论让我可以做出更好的答案。
Just to update some information, there is a more powerful selenium-webdriver with Ruby Bindings now.
To test JavaScript on firefox from terminal quickly with Rails 4 and selenium-webdriver, you need to do the 4 steps below:
Add gem to your Gemfile
gem 'selenium-webdriver'
gem 'minitest-rails'
Install gem
bundle install
Generate test case (Ref: Official Guide)
bin/rails generate integration_test your_case_name
Start to write test code in your test case.
Actually you can write it in ruby without Capybara, to write case with Capybara you can refer to Capybara at Github
Minitest sample:
Currently it seems lack of resources with respect to how to work with selenium-webdriver, CI server (Jenkins) and Rails minitest smoothly, I have created a simple project and hope it can help any one getting started quickly and easily:
Rails Selenium Working Case
Also appreciate the comments that let me can make better answer.