亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Cucumber + Capybara - What we need for rails

系統 2031 0

What’s Capybara

Capybara is a webrat alternative which aims to support all browser simulators.

As you know, webrat can not run javascript on the webpage. In order to test javascript and AJAX based website we need install Selenium-client github .com/ph7/selenium-client and learn extra API and also some configuratons.

Capybara give your a full stack solution.
You can use similar API drive webrat and selenium. And don’t need worry about configuraton.

What’s more, Capybara integrated Celerity which is a JRuby wrapper around HtmlUnit - a headless Java browser with JavaScript support. As your expect it using the same API.

And the killer feature of Capybara is you can easily change driver it use for testing. Not only in config file but also in runtime.

Why we need Cucumber

Before I introduce what is Cucumber, you need know what is BDD .

BDD is an evolution thinking behind TestDrivenDevelopment and AcceptanceTestDrivenPlanning

You don’t need worry about what the hell BDD is. After you start using Cucumber you will know. I promise.

Cucumber is allow you execute plain-text which written by BDD like format as automated tests.

There are plenty of materials on the internet which you can learn Cucumber

First place your need go is Ryan Bates’ RailsCasts Beginning with Cucumber
Then official Wiki of Cucumber will very helpful!!, and there also list some blog posts written by community

I won’t involve more about it. In my later blog post I will give you some tricks about using Cucumber.

Using Cucumber and Capybara

Install

Follow the instruction on Capybara:

Install as a gem

      sudo gem install capybara
    

On OSX you may have to install libffi, you can install it via MacPorts with:

      sudo port install libffi
    

And you also need install Cucumber

      sudo gem install cucumber
    

Generate basic Cucumber folder structure and configuratons

Capybara is built to work nicely with Cucumber. You can easily generate Capybara style cucumber structure and configuraton.

      script/generate cucumber --capybara
    

And maybe you also need install another gem named launchy which is helper class for launching cross-platform applications in a fire and forget manner.

      sudo gem install launchy
    
      group :cucumber 
      
        do
      
      
        

  gem 
      
      
        '
      
      
        capybara
      
      
        '
      
      
        

  gem 
      
      
        '
      
      
        database_cleaner
      
      
        '
      
      
        

  gem 
      
      
        '
      
      
        cucumber-rails
      
      
        '
      
      
        

  gem 
      
      
        '
      
      
        cucumber
      
      
        '
      
      
        

  gem 
      
      
        '
      
      
        rspec-rails
      
      
        '
      
      
        

  gem 
      
      
        '
      
      
        spork
      
      
        '
      
      
        

  gem 
      
      
        '
      
      
        launchy
      
      
        '
      
          # So you can 
      
        do
      
      
         Then show me the page

end 
      
    

?

Configuration

Available Configuration

Actually after you run the generator, you don’t need much more configuraton. Here I’ll list some of configuraton you can set.

You can specify it in features/support/env.rb file

          Capybara.run_server = 
          
            true
          
          
             #Whether start server when testing

Capybara.default_selector 
          
          = :xpath #
          
            default
          
          
             selector , you can change to :css

Capybara.default_wait_time 
          
          = 
          
            2
          
           #When we testing AJAX, we can 
          
            set
          
           a 
          
            default
          
          
             wait time

Capybara.ignore_hidden_elements 
          
          = 
          
            false
          
           #Ignore hidden elements when testing, make helpful when you hide or show elements 
          
            using
          
          
             javascript

Capybara.javascript_driver 
          
          = :culerity #
          
            default
          
           driver when you 
          
            using
          
           @javascript tag
        

Load rails test features

You can put codes below in features/support/env.rb file

      Before 
      
        do
      
      
        

  Fixtures.reset_cache

  fixtures_folder 
      
      = File.join(RAILS_ROOT, 
      
        '
      
      
        test
      
      
        '
      
      , 
      
        '
      
      
        fixtures
      
      
        '
      
      
        )

  fixtures 
      
      = Dir[File.join(fixtures_folder, 
      
        '
      
      
        *.yml
      
      
        '
      
      )].map {|f| File.basename(f, 
      
        '
      
      
        .yml
      
      
        '
      
      
        ) }

  Fixtures.create_fixtures(fixtures_folder, fixtures)

end
      
    

And change

      Cucumber::Rails::World.use_transactional_fixtures = 
      
        false
      
      
        

DatabaseCleaner.strategy 
      
      = :truncation
    

OK. For now we already finish configuration. we can start writing cucumber test

How to run test under different testing driver

As you know cucumber support tags
Capybara using tag to specify different driver, it supports @javascript, @selenium, @celerity, @culerity and @rack_test tags
You can use it like:

      
          @javascript

  Scenario: 
      
      
        do
      
      
         something AJAXy

  When I click the AJAX link

  ...
      
    

About how to write cucumber, you can check out Cucumber Wiki

About Capybara API

What I want to show you is how’s Capybara API look like.

This is all support Webrat like API s in Capybara

      DSL_METHODS =
      
         [

      :all, :attach_file, :body, :check, :choose, :click, :click_button, :click_link, :current_url, :drag, :evaluate_script,

      :field_labeled, :fill_in, :find, :find_button, :find_by_id, :find_field, :find_link, :has_content
      
      ?, :has_css?
      
        ,

      :has_no_content
      
      ?, :has_no_css?, :has_no_xpath?, :has_xpath?, :locate, :save_and_open_page, :
      
        select
      
      
        , :source, :uncheck,

      :visit, :wait_until, :within, :within_fieldset, :within_table, :has_link
      
      ?, :has_no_link?, :has_button?, :has_no_button?
      
        ,

      :has_field
      
      ?, :has_no_field?, :has_checked_field?, :has_unchecked_field?, :has_no_table?, :has_table?
      
        , :unselect,

      :has_select
      
      ?, :has_no_select?
      
        

    ]
      
    

Here are some examples which are not mentioned in Capybara Wiki.

        # we can 
      
        get
      
       page 
      
        object
      
      
        from
      
       cucumber steps, page 
      
        is
      
      
         an instance of @Capybara::Session@

  page.has_css
      
      ? 
      
        "
      
      
        ul.error_messages li
      
      
        "
      
      , :count => 
      
        5
      
      , :text => 
      
        "
      
      
        error
      
      
        "
      
      
        

  page.has_xpath
      
      ? 
      
        "
      
      
        //ul[@class='error_messages']/li
      
      
        "
      
      , :count => 
      
        5
      
      , :text => 
      
        "
      
      
        error
      
      
        "
      
      
        



  #Equivalent

  page.find(:css,
      
      
        "
      
      
        ul.error_messages li
      
      
        "
      
      , :count => 
      
        5
      
      , :text => 
      
        "
      
      
        error
      
      
        "
      
      
        

  page.find(:xpath,
      
      
        "
      
      
        //ul[@class='error_messages']/li
      
      
        "
      
      , :count => 
      
        5
      
      , :text => 
      
        "
      
      
        error
      
      
        "
      
      
        



  #Iterate all elements you found

  all(:xpath,
      
      
        "
      
      
        //ul[@class='error_messages']/input
      
      
        "
      
      ).each 
      
        do
      
       |node|
      
        

    puts node.value

    puts node.[:attribute_name]

    puts node.click

    puts node.
      
      
        set
      
      (
      
        "
      
      
        aa
      
      
        "
      
      ) #
      
        set
      
      
         value

    puts node.text

  end
      
    

XPath

If you want to use XPath in Capybara, you need caution string escape.
Capybara give us a good example

      def s(
      
        string
      
      
        )

  
      
      
        if
      
      
        string
      
      .include?(
      
        "
      
      
        '
      
      
        "
      
      
        )

    
      
      
        string
      
       = 
      
        string
      
      .split(
      
        "
      
      
        '
      
      
        "
      
      , -
      
        1
      
      ).map 
      
        do
      
       |substr|

    
      
        "
      
      
        '#{substr}'
      
      
        "
      
      
        

    end.join(
      
      %q{,
      
        "
      
      
        '
      
      
        "
      
      
        ,})

    
      
      
        "
      
      
        concat(#{string})
      
      
        "
      
      
        else
      
      
        "
      
      
        '#{string}'
      
      
        "
      
      
        

  end

end
      
    

Summarize

Cucumber + Capybara will make your integration test easilier.
Finally we find a full stack integration test.
Have fun with it!

?

?

from : http://www.allenwei.cn/cucumber-capybara-what-we-need-for-rails-integration-test

Cucumber + Capybara - What we need for rails integration test


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 91中文字幕在线一区 | 国产中文字幕在线观看 | 国产v欧美v日韩在线观看 | 久久久国产精品网站 | 亚洲欧美日韩一区二区在线观看 | 午夜一级精品免费毛片 | 四虎视频网站 | 狠狠色综合久久丁香婷婷 | 国产在线观看精品香蕉v区 国产在线观看美女福利精 国产在线观看午夜不卡 | 天天翘夜夜洗澡天天做 | 免费特黄一级欧美大片 | 久久精品国产在热久久2019 | 亚洲人成网站色7799在线观看 | 国内自拍在线观看 | 久久精品国产影库免费看 | 干干操操| 男人的天堂欧美精品色偷偷 | 国产香蕉精品视频 | 看全大色黄大色黄大片一级爽 | 狠狠激情五月综合婷婷俺 | 青草社区视频 | 99视频精品全部在线播放 | 青青青在线视频人视频在线 | 色一情一乱一乱91av | 亚洲全黄 | 2021国产精品自产拍在线 | 四虎影院海外永久 | 日本一级高清片免费 | 拍拍拍精品视频在线观看 | 天天操夜夜操免费视频 | 亚洲一级毛片免费看 | 国产日韩精品一区在线不卡 | 亚洲 欧美 另类中文字幕 | 2021午夜国产精品福利 | 亚洲一区二区三区免费看 | 欧美成人精品高清在线观看 | 91人碰| 久草手机在线 | 97高清国语自产拍中国大陆 | 亚洲高清一区二区三区 | 国产一级毛片一区二区三区 |