ArticleDirectory
- Install
- Generate basic cucumber folder structure and configuratons
- Configuration
- How to Run test under different testing driver
- Xpath
What's capytasks
Capyms 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 andAjaxBased website we need install selenium-clientgithub.com/ph7/selenium-client and learn extraAPIAnd also some configuratons.
Capyspongive your a full stack solution.
You can use similarAPIDrive webrat and Selenium. And don't need worry about Configuraton.
What's more, capy+integrated celerity which is a jruby wrapper around htmlunit-a headless Java browser with JavaScript support. As your has CT it using the sameAPI.
And the killer feature of capyature 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 isBDD.
BDDIs an evolution thinking behind testdrivendevelopment and acceptancetestdrivenplanning
You don't need worry about what the hellBDDIs. After you start using cucumber you will know. I promise.
Cucumber is allow you execute plain-text which writtenBDDLike format as automatic 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 capybarainstall
Follow the instruction on capyction:
Install as a gem
Sudo gem install capy.pdf
OnOSXYou may have to install libffi, you can install it via macports:
Sudo port install libffi
And you also need install cucumber
Sudo gem install cucumber
Generate basic cucumber folder structure and configuratons
Capyber is built to work nicely with cucumber. You can easily generate capy?style cucumber structure and Configuraton.
Script/generate cucumber -- capyber
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 ' Capyworkflow ' Gem ' Database_cleaner ' Gem ' Cucumber-rails ' Gem ' Cucumber ' Gem ' Rspec-rails ' Gem ' Spork ' Gem ' Launchy ' # So you can Do Then show me the pageend
Configurationavailable Configuration
Actually after you run the generator, you don't need much more than Aton. Here I'll list some of your aton you can set.
You can specify it inFeatures/support/env. Rb
File
Capybara. run_server =True# Whether start server when testingcapytor. default_selector=: XPath #DefaultSelector, you can change to: csscapy.pdf. default_wait_time=2# When we testing Ajax, we canSetADefaultWait timecapypackages. ignore_hidden_elements=False# Ignore hidden elements when testing, make helpful when you hide or show elementsUsingJavascriptcapy.pdf. javascript_driver=: Culerity #DefaultDriver when youUsing@ JavaScript tag
Load rails test features
You can put codes below inFeatures/support/env. Rb
File
BeforeDoFixtures. 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 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:DoSomething ajaxy when I click the Ajax link...
About how to write cucumber, you can check out cucumber Wiki
About capytasks API
What I want to show you is how's capyschemeAPILook like.
This is all support webrat likeAPIS in capytasks
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 capy?wiki.
# We can Get Page Object From Cucumber steps, page Is An instance of @ capycss: 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 capyinjection, you need caution string escape.
Capyuncgive 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