博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cucumber + Capybara - What we need for rails integration test
阅读量:5347 次
发布时间:2019-06-15

本文共 5799 字,大约阅读时间需要 19 分钟。

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 .com/ph7/selenium-client and learn extra API and also some configuratons.

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, integrated 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 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.

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

Then will very helpful!!, and there also list some 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 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 pageend

 

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 testingCapybara.default_selector = :xpath #default selector , you can change to :cssCapybara.default_wait_time = 2 #When we testing AJAX, we can set a default wait timeCapybara.ignore_hidden_elements = false #Ignore hidden elements when testing, make helpful when you hide or show elements using javascriptCapybara.javascript_driver = :culerity #default driver when you using @javascript tag

Load 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 = falseDatabaseCleaner.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

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

About Capybara API

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

This is all support Webrat like APIs 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}'"  endend

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

转载于:https://www.cnblogs.com/feichan/archive/2012/12/17/2821703.html

你可能感兴趣的文章
2 Orchard汉化资源包的使用
查看>>
python3 property
查看>>
自定义控件注意点
查看>>
SSRS 报表 如何匿名查看
查看>>
JVM内存管理机制
查看>>
centos 安装Mysql
查看>>
简单通用Ajax函数
查看>>
【Android】ListView监听上下滑动(判断是否显示返回顶部按钮
查看>>
HBASE的MAPREDUCE任务运行异常解决办法,无需CYGWIN,纯WINDOWS环境
查看>>
禅道在docker上部署与迁移
查看>>
关于继承、封装、多态、抽象和接口
查看>>
c27---typedef
查看>>
android WebViewClient和WebChromeClient
查看>>
div+css清除浮动代码
查看>>
017Python路--解释器
查看>>
idea2019中utf-8乱码问题
查看>>
docker应用-3(搭建hadoop以及hbase集群)
查看>>
Luogu4191 [CTSC2010]性能优化【多项式,循环卷积】
查看>>
Oracle 相关查询
查看>>
求1到5阶乘之和
查看>>