如何使用 Minitest、Capybara、Selenium 执行 javascript 测试?

发布于 2024-11-01 07:18:33 字数 259 浏览 6 评论 0原文

有很多关于如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不念旧人 2024-11-08 07:18:33

:js 标志的作用非常简单。它将当前驱动程序从默认(rack-test)切换到另一个支持 javascript 执行的驱动程序(selenium、webkit)。你可以在 minitest 中做同样的事情:

require "minitest/autorun"

class WebsiteTest < MiniTest::Unit::TestCase
  def teardown
    super
    Capybara.use_default_driver
  end

  def test_with_javascript
    Capybara.current_driver = :selenium
    visit "/"
    click_link "Hide"
    assert has_no_link?("Hide")
  end

  def test_without_javascript
    visit "/"
    click_link "Hide"
    assert has_link?("Hide")
  end
end

当然,为了方便你可以将其抽象成一个模块:

require "minitest/autorun"

module PossibleJSDriver
  def require_js
    Capybara.current_driver = :selenium
  end

  def teardown
    super
    Capybara.use_default_driver
  end
end

class MiniTest::Unit::TestCase
  include PossibleJSDriver
end

class WebsiteTest < MiniTest::Unit::TestCase
  def test_with_javascript
    require_js
    visit "/"
    click_link "Hide"
    assert has_no_link?("Hide")
  end

  def test_without_javascript
    visit "/"
    click_link "Hide"
    assert has_link?("Hide")
  end
end

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:

require "minitest/autorun"

class WebsiteTest < MiniTest::Unit::TestCase
  def teardown
    super
    Capybara.use_default_driver
  end

  def test_with_javascript
    Capybara.current_driver = :selenium
    visit "/"
    click_link "Hide"
    assert has_no_link?("Hide")
  end

  def test_without_javascript
    visit "/"
    click_link "Hide"
    assert has_link?("Hide")
  end
end

Of course you can abstract this into a module for convenience:

require "minitest/autorun"

module PossibleJSDriver
  def require_js
    Capybara.current_driver = :selenium
  end

  def teardown
    super
    Capybara.use_default_driver
  end
end

class MiniTest::Unit::TestCase
  include PossibleJSDriver
end

class WebsiteTest < MiniTest::Unit::TestCase
  def test_with_javascript
    require_js
    visit "/"
    click_link "Hide"
    assert has_no_link?("Hide")
  end

  def test_without_javascript
    visit "/"
    click_link "Hide"
    assert has_link?("Hide")
  end
end
一片旧的回忆 2024-11-08 07:18:33

嗯,我注意到文档中的几行似乎说上面的操作只能在 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 the default_driver, and marking only those tests that require a JavaScript-capable driver using :js => true or @javascript, respectively.

青芜 2024-11-08 07:18:33

https://github.com/wojtekmach/minitest-metadata 似乎提供了一个解决方案这。

您可以执行以下操作:

describe "something under test" do
  it "does not use selenium for this test" do
    visit "/"
    assert body.has_content?("Hello world")
  end

  it "does use selenium for this test", :js => true do
    visit "/"
    click_link "Hide" # a link that uses a javascript click event, for example
    assert body.has_no_link?("Hide")
  end
end

https://github.com/wojtekmach/minitest-metadata seems to have provided a solution to exactly this.

You can do the following:

describe "something under test" do
  it "does not use selenium for this test" do
    visit "/"
    assert body.has_content?("Hello world")
  end

  it "does use selenium for this test", :js => true do
    visit "/"
    click_link "Hide" # a link that uses a javascript click event, for example
    assert body.has_no_link?("Hide")
  end
end
第七度阳光i 2024-11-08 07:18:33

只是为了更新一些信息,有一个更强大的 selenium-webdriver 和 Ruby Bindings现在。

要使用 Rails 4 和 selenium-webdriver 从终端快速测试 Firefox 上的 JavaScript,您需要执行以下 4 个步骤:

  1. 将 gem 添加到 Gemfile

    gem 'selenium-webdriver'
    gem 'minitest-rails'

  2. 安装 gem

    bundle install

  3. 生成测试用例(参考:官方指南< /a>)

    bin/rails生成integration_test your_case_name

  4. 开始在你的测试用例中编写测试代码。
    其实不用Capybara用ruby写也可以,用Capybara写case可以参考Github的Capybara< /p>

Minitest示例:

# create a driver
driver = Selenium::WebDriver.for :firefox
# navigate to a page
driver.navigate.to 'url_to_page'
# get element with class box
box = driver.find_element(:css, '.box')

# check width
# get width by execute JavaScript
assert_equal 100, driver.execute_script('return $(arguments[0]).width()', box)

目前似乎缺乏关于如何顺利使用 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:

  1. Add gem to your Gemfile

    gem 'selenium-webdriver'
    gem 'minitest-rails'

  2. Install gem

    bundle install

  3. Generate test case (Ref: Official Guide)

    bin/rails generate integration_test your_case_name

  4. 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:

# create a driver
driver = Selenium::WebDriver.for :firefox
# navigate to a page
driver.navigate.to 'url_to_page'
# get element with class box
box = driver.find_element(:css, '.box')

# check width
# get width by execute JavaScript
assert_equal 100, driver.execute_script('return $(arguments[0]).width()', box)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文