2013-08-22 164 views

回答

12

您可以定义下before(:all)

describe "Test" do 
    before(:all) do 
    ... 
    ... 
    page.driver.browser.manage.window.resize_to(x,y) #Mention it here 
    end 

    it "should find everything" do 
    ... 
    end 

    after(:all) do 
    ... 
    end 
end 
+0

然后我需要在每个spec文件中这样做吗?我如何为*一切*做? – CDub

+1

您可以使用RSpec.configure在您的示例中包含或扩展模块,然后将您的before:all /:each放入要包含/扩展的模块中:https://www.relishapp.com/rspec/rspec-core/docs/helper-methods/define-helper-methods-in-a-module –

+0

@CDub查看我的答案。 –

34

一个适当的方式做到这一点对所有的JS测试是增加内部spec_helper.rbRSpec.configure

config.before(:each, js: true) do 
    Capybara.page.driver.browser.manage.window.maximize 
end 

最大化窗口下面。更改为resize_to(x,y)以设置任何窗口大小。

编辑:如果你碰巧使用骚灵正确的方法来做到这一点是

config.before(:each, js: true) do 
    Capybara.page.driver.browser.resize(x,y) 
end 
+1

这似乎不适用于水豚和Poltergeist:'未定义的方法'管理'为#<水豚:: Poltergeist ::浏览器:0x007fd4e76ec828>' – tirdadc

+0

嘿@tirdadc什么版本是你在用吗? –

+0

好的,我可以看到Poltergeist没有管理,但有一种方法来调整大小,更新答案! –

8

或许是由于水豚最近的变化,有什么工作对我来说是:

before do 
    Capybara.page.current_window.resize_to(x, y) 
end 
+0

这是目前唯一至少做某事的人,但是我得到了Capybara :: NotSupportedByDriverError错误。我尝试了poltergeist和webkit。我正在运行Ubuntu 16.04,任何人都知道什么是错的? – Aurimas

+0

@Aurimas你可以尝试改变你的驱动,试着在你的规范中使用'js:true' – ilgam

0

@tirdadc如果您使用的是Poltergeist,您可以在您的rails_helper.rb文件中添加如下内容:

Capybara.register_driver :poltergeist do |app| 
    options = { 
    # js_errors: true, 
    # cookies: true, 
    window_size: [320, 568] # iphone 5 
    } 
    Capybara::Poltergeist::Driver.new(app, options) 
end 
2

对于水豚2.2.4版本测试运行时,你可以通过做

before do 
    handle = Capybara.page.driver.current_window_handle 
    Capybara.page.driver.resize_window_to(handle, height, width) 
end 

或者

before do 
    Capybara.page.current_window.resize_to(height, width) 
end 

如果你得到水豚:: NotSupportedByDriverError实现这一目标:水豚::驱动程序:: Base#current_window_handle您必须更改您的驱动程序以供示例使用JAVAZCRIPT DRIVER

before do 
    Capybara.page.current_window.resize_to(height, width) 
end 

scenario js: true do 
    # your test here 
end