簡體   English   中英

我可以覆蓋 Capybara 的 page.body 值嗎

[英]Can I override Capybara's page.body value

我遇到了一個問題,即存根響應不起作用(使用帶有遠程瀏覽器的 docker),但需要針對外部請求進行測試

有沒有辦法通過 Capybara 對響應進行硬編碼?

# This is what we would normally do
WebMock.stub_request(:get, 'http://example.com').to_return(status: 200, body: IO.read('spec/factories/submission_stubs/mystub.html'))

# In theory this should return mystub.html, but that is not working in my case.
# But since we're using a remote webdriver via docker it has no idea about my 'mock'
visit 'http://example.com'

# So I'm looking to simply override page.body to be `mystub.html`
puts page.body

我也只是嘗試從這個答案中讀取直接文件: Capybara open an html file in my computer

但似乎該文件沒有正確安裝在我的遠程驅動程序系統上

編輯:我一生都無法按預期工作。 響應總是返回和空的 HTML 頁面。 我的解決方案是簡單地將我的存根響應加載到雲中並將其公開,以便我的遠程瀏覽器可以訪問它。

您所指的解決方案是正確的。 這里的技巧是我們加載路徑直到文件存在的目錄。 不要包含文件名本身

這是一個工作示例:

context 'testing' do

  before do
    # the trick here we load the path up till the directory where the file exist
    # don't include the file name itself
    Capybara.app = Rack::File.new('spec/factories/submission_stubs')
  end

  it 'loads the file' do
    visit 'mystub.html'

    puts page.body
  end
end

您不能存根瀏覽器從使用 WebMock 的測試中看到的響應,因為它只存根本地請求。 要存根/模擬對瀏覽器請求的響應,您應該查看可編程代理,例如https://github.com/oesmith/puffing-billy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM