Updated: Capybara: Check that a page element is hidden via CSS
- The step we used in the past (
Then "foo" should not be visibile
) doesn't reliably work in Selenium features. - I overhauled the entire step so it uses Javascript to detect visibility in Selenium.
- The step has support for jQuery and Prototype projects, so it should be a drop-in replacement for all your projects.
- For Rack::Test the step no longer uses XPath so you should be able to understand it when you are not a cyborg :)
- There were some other cards detailing alternative steps to detect visibility. I deleted all these other cards s...
RubyMine: Using pinned tabs will increase your productivity
I highly recommend that you make use of RubyMine's feature to pin tabs.
When you pin all "important" files, you can follow method definitions, wildly open files from search results and have a ton of open tabs -- without the problem of finding the stuff you were working on before.
Guide
- Pin the tabs of files that are currently in the focus of your work (important models, specs, etc):
- Right-click a tab and select "Pin tab"
- Or use a shortcut (see below)
- Work as usual.
- Once you opened other tabs because you searched ...
Click on a piece of text in Cucumber / Capyabra
The step definition below lets you write:
When I click on "Foo"
This is useful in Selenium features where the element you click on is not necessarily a link or button, but could be any HTML element with a Javascript event binding.
The easiest way to get this step is to use Spreewald. If you would like to add it manually, here is the step definition:
When /^I click on "([^\"]+)"$/ do |text|
matcher = ['*', { :text => text }]
element = page.find(:css, *matcher)
while be...
Speed up large Cucumber test suites
Test suites usually grow over time as more and more development time is spent on a projects. Overall run-time and performance of Cucumber suites in turn increases, too.
You can use the very same way Henning suggested for speeding up RSpec some time ago.
Put the following into features/support/deferred_garbage_collection.rb
Before do
DeferredGarbageCollection.start
end
After do
DeferredGarbageCollection.reconsider
end
We...
New cards feature: Personal RSS feed that includes public and private cards
Your account profile now links to a personal RSS feed. This RSS feed contains the newest public and private cards for all your decks.
New cards feature: Explicit language declaration for syntax highlighting
Makandra cards will auto-detect the language used for syntax highlighting.
This auto-detection sometimes fails for short code snippets. In such cases you can explicitly declare the language for Github-style code blocks:
```css
body {
font-size: 12px
}
```
Will turn into this:
body {
font-size: 12px
}
To disable syntax highlighting entirely use the text
language:
```text
I am nothing without pretend
...
Cucumber: Calling multiple steps from a step definition
When refactoring a sequence of steps to a new, more descriptive step, you can use the steps
method and Ruby's %-notation like this:
Given 'I have an article in my cart' do
steps %(
When I go the article list
And I open the first article
And I press "Add to cart"
)
end
This way you can simply copy the steps over without any changes.
Warning: Apparently, steps
processes its argument with the Gherkin parse...
How to: Specify size of Selenium browser window
Applications often show or hide elements based on viewport dimensions, or may have components that behave differently (like mobile vs desktop navigation menus).
Since you want your integration tests to behave consistently, you want to set a specific size for your tests' browser windows.
Using WebDriver options / Chrome device metrics
For Google Chrome, the preferred way is setting "device metrics". This allows you to configure dimensions larger than your display and enable/disable touch behavior.
Simply use register_driver
to set up...
rake spec + rails_admin = weirdly failing specs
If you use rails_admin, your specs pass with the rspec
binary, but not using rake spec
(or rake parallel:spec
etc), put this at the top of your spec_helper
:
ENV['SKIP_RAILS_ADMIN_INITIALIZER'] = 'false'
Don't ask.
This is probably also true for cucumber, your env.rb
would be the right place.
Use Memoizer instead of ActiveSupport::Memoizable
ActiveSupport::Memoizable
will be removed from Rails and has a lot of strange caveats that will ruin your day.
Use the Memoizer gem instead. It works in all past and future Railses and has none of the annoying "features" of ActiveSupport::Memoizable
. It just does memoization and does it well.
The syntax is similiar also:
class Foo
include M...
Do not use "find" on Capybara nodes from an array
In a nutshell: Capybara's find
will not work properly on nodes from a list. Don't find
on elements from a list.
Background
Consider this HTML:
<div class="message">
<h2>Hello World</h2>
Lorem ipsum...
</div>
<div class="message">
<h2>Hello Universe</h2>
Lorem ipsum...
</div>
Now let's say you obtain a list of all such message
containers as an array:
messages = page.all('.message')
And then you look at their titles like this:
messages[0].find('h2').text
=> "Hello W...
Rails 2's CookieStore produces invalid cookie data, causing tests to break
Note that this seems to affect only recent Rails 2 versions.
You will not encounter this until you are writing to the cookie more than once, but when doing so, integration tests (Cucumber) may break for you with this error:
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[] (NoMethodError)
Background
The regular/short cucumber backtrace is not of any help but looking at the full trace reveals that ActionPack's `actio...
Security fixes for Rails 2.3
Last week saw a security issue with rails 2.3 that required a fix. While an official patch was provided, the 2.3 branch is no longer maintained. So we forked it.
(I'm sure there are already 100 other forks doing absolutely the same, but they are not very easily discoverable.)
To use our fork, change the gem "rails"...
line in your Gemfile to this:
gem 'rails', :git => 'https://github.com/makandra/rails.git', :branch => '2-3-fixes'
The intent is to make as few changes to the f...
Fix: Capybara is very slow when filling out fields in large forms
In large forms (30+ controls) new Capybara version become [extremely slow] when filling out fields. It takes several seconds per input. The reason for this is that Capybara generates a huge slow XPath expression to find the field.
The attached code patches fill_in
with a much faster implementation. It's a dirty fix and probably does a lot less than Capybara's own fill_in
so don't use it unless you are having problems with test suites that are unusable because of this...
Rails asset pipeline: Why relative paths can work in development, but break in production
The problem
When using the asset pipeline your assets (images, javascripts, stylesheets, fonts) live in folders inside app
:
app/assets/fonts
app/assets/images
app/assets/javascripts
app/assets/stylesheets
With the asset pipeline, you can use the full power of Ruby to generate assets. E.g. you can have ERB tags in your Javascript. Or you can have an ERB template which generates Haml which generates HTML. You can chain as many preprocessors as you want.
When you deploy, Rails runs assets:precompile
...
Linux: How to add a task bar to VNC displays
If you are using VNC to run Selenium tests, it may be hard to see what's going on since by default there is no list of open windows and Alt
+Tab
won't work.
Solving that is easy:
-
Install a panel of your choice (like lxpanel) which offers task switching:
sudo apt-get install lxpanel
(You can't use
gnome-panel
because it won't start twice -- but lxpanel does a good job) -
To have that panel appear on VNC screens by default, edit
~/.vnc/xstartup
...
Visualized introduction how git works
Quick introduction to git internals for people who are not scared by words like Directed Acyclic Graph.
The linked page offers a simple yet concise explanation of how git is organized internally ('a directed acyclic graph with post-it notes'). Each feature is illustrated by a simple diagram, so you get a sound understanding of how each command affects git's structure.
Plotting graphs in Ruby with Gruff
Geoffrey Grosenbach has created Gruff for easily plotting graphs. It is written in pure Ruby and integrates with Rails applications.
It provides features as automatic sizing of dots and lines (the more values, the thinner the graph's elements), custom or predefined themes, different styles (bar, line, dot and many more) and multiple graphs in one chart.
Installation
In your Gemfile:
gem 'rmagick', :require => false
gem 'gruff'
Then run bundle install
(and don't forget to restart your development server.)
Usage
This i...
Use a special version of Chrome for selenium (and another for your everyday work)
Sometimes you need a special version of chrome because it has some features you need for testing, like in this card. You do not need to use that Version apart from tests, because you can tweek selenium to use a special version that you set in your environment:
# features/support/chrome.rb
require "selenium/webdriver"
Capybara.register_driver :chrome320x480 do |app|
if driver_path = ENV["CHROME_SELENIUM_BIN...
Set the accept-language of Chrome in selenium tests
You can set the resolution and user agent used in selenium tests with chrome with the method described in this card, but you can also set the accept-language and other profile settings if you do this:
# features/support/chrome.rb
require "selenium/webdriver"
Capybara.register_driver :chrome320x480 do |app|
args = []
args << "--window-...
Run Chrome in a specific resolution or user agent with Selenium
When you want to test how an web-application reacts in a specific resolution, you can set up a specific Selenium driver for some tests:
Before('@chrome320x480') do
Capybara.current_driver = :chrome320x480
end
After('@chrome320x480') do
Capybara.use_default_driver
end
You can use either chromium or chrome beta (as of 2012.05 the Version "19.0.1084.41 beta" works), or any other member of the family. It only needs to supports the "--window-size" command-line switch. [See this list](http://peter.sh...
Navigating through the browser history in a cucumber feature using selenium
In order to navigate through the browser history. you can manipulate the window.history object via javascript like follows:
When /^I go back in the browser history$/ do
page.evaluate_script('window.history.back()')
end
For further functions of the window and history objects check out this link.
An improved version of this step is now part of our gem spreewald on Github.
Browser support for box-shadow
Basic box shadow support is available in all browsers today, but you need to check to which extend they are supported. Implementations differ:
- Are multiple box shadows (separated by comma) possible?
- Are inset shadows possible?
- Can the spread radius be defined?
- Can you use it without vendor prefix?
A good compatibility chart can be found here. The gist is that, except for IE and some Safaris, you can use all features ...