用水豚启动浏览器 [英] launching browser with capybara

查看:70
本文介绍了用水豚启动浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Capybara的新手,希望有人可以帮助我解决我遇到的问题。

I am new to Capybara and was hoping someone could help me with a problem I am having.

作为测试的一部分,我有许多不同的测试规格文件。当前,在每个规格文件的顶部,我使用以下内容创建一个新的Capybara会话:
session = Capybara :: Session.new(:selenium)。

As part of my testing I have a number of different test spec files. Currently at the top of each spec file I create a new Capybara session with the following: session = Capybara::Session.new(:selenium).

当只有1个规格文件要运行时,这很好用,但是我发现当我尝试同时运行多个不同的规格文件测试(包含在一个文件夹中)时,它会为每个实例创建一个新实例,最终导致负载为不同的浏览器窗口。有没有办法我可以只在一个地方而不是在每个测试规格文件中使用它?

This works fine when there is just 1 spec file to run, but I find when I attempt to run a number of different spec file tests together (contained in a folder) it creates a new instance for each first and I end up with loads of different browser windows. Is there a way I can just have this in 1 place and not in each test spec file?

我以前有黄瓜和w草的经验,可以做的就是在env文件中创建浏览器实例。您也可以在这里拆解。 rspec / capybara中是否有等效项?我曾尝试将'session = Capybara :: Session.new(:selenium)'行放入我的规范帮助文件中,但是当我尝试运行测试时,即使我需要'我也得到'未定义的局部变量或方法'session'。

I have previous experience with cucumber and watir and what you could do with that was create your browser instance in your env file. And you could also have your teardown here too. Is there an equivalent in rspec/capybara? I have tried putting 'session = Capybara::Session.new(:selenium)' line into my spec helper file, but when I try to run the tests I get 'undefined local variable or method `session' even though I have require 'spec_helper' in each test spec.

任何与此相关的帮助都将非常棒!!如果以上任何不清楚的地方,请告诉我,我会尽力澄清。

Any help with this would be great!! If any of the above is not clear please let me know and I will do my best to clarify.

推荐答案

斯科特·阿里斯特(Scott Alister)描述了为此,请在他的博客上针对watir和rspec 。您可以对水豚和rspec进行同样的调整。

Scott Alister described how to do this for watir and rspec on his blog. You could adapt the same for capybara and rspec.

尝试:

session = Capybara::Session.new(:selenium)

RSpec.configure do |config|
  config.before(:each) { @session = session }
end

请注意,您的测试将使用 @session 变量引用会话。

Note that your tests would refer to the session using the @session variable.

这篇关于用水豚启动浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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