2012-11-30 105 views
2

在我的數據庫Rails應用程序中我有一些查詢。如何在沒有測試的情況下使用水豚

# PostController 
@post.query # => 'blabla' 

,我希望把這個字符串在谷歌或雅虎

簡單的ruby文件我用

require ... all gems 
Capybara.app_host = 'http://www.google.com/' 

class Google 
    include Capybara::DSL 
    def search 
    visit('/') 
    fill_in "q", :with => "blah blah" 
    click_button("Google") 
    ... 

,但我怎麼可以把這個代碼控制器(或模型方法)?

回答

0
class PostsController < ApplicationController 

require 'capybara' 
require 'capybara/dsl' 

include Capybara::DSL 

Capybara.current_driver = :webkit 

    def index 
    @posts = Post.all 
    end 

    def show 
    @post = Post.find(params[:id]) 
    visit(@post.search_type) 
    fill_in "q", :with => @post.query 
    click_button("Google") 

    loop_variable = 0 
    num_of_page = 1 
    max_count_of_page = 4 
    flag_on_break = false 

    while(loop_variable == 0) do 
     has_on_page = page.has_content?(@post.search_question) 

     if has_on_page 
     loop_variable = 1 
     else 
     find('.b:last a').click 
     num_of_page += 1 
     end 
     if num_of_page > max_count_of_page 
      flag_on_break = true 
      break 
      end 
     end 

     if flag_on_break 
     @number = 'Nothing not found' 
     else 
     @number = num_of_page 
     end 
    end 

    def new 
    @post = Post.new 
    end 

    def create 
    @post = Post.new(params[:post]) 

    if @post.save 
     flash[:notice] = "Post created!" 
     redirect_to root_path 
    end 
    end 
end 

這是一塊我的代碼 - 但主要的想法,我認爲是可以理解的

0

水豚不打算用於此類用途。您可能需要嘗試mechanize

+0

我正在尋找如何使用谷歌與水豚搜索(當我有時間 - 我把答案) –

相關問題