summaryrefslogtreecommitdiff
path: root/_posts/2012-08-27-testing-website-with-capybara-webkit-and-testunit.md
blob: a360e39c3a191eefaaf2e66af20d321015d1e836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
---
layout: post
title: "Testing website with capybara-webkit and test/unit"
description: ""
category: Development
tags: [Headless browser, Testing]
---
{% include JB/setup %}
---

# Setup for headless browser tests with test/unit and capybara-webkit.

You need to install capybara and capybara-webkit as gem. Capybara-webkit requires two system libraries **qt4** and **xvfb**. To install execute:

  `apt-get install qt4-dev-tools`<br>
  `apt-get install xvfb`

as sudo. This works for Debian Squeeze. If **libqt4-dev** is not auto-installed you have to install it manually.
  `apt-get install libqt4-dev`

# Install the gems:

  `gem install capybara`<br>
  `gem install capybara-webkit`

To run the headless browser you have to execute:

  `Xvfb :1 -screen 0 1024x768x16 -nolisten inet6 &`<br>
  `export 'DISPLAY=localhost:1.0'`

you can ignore warnings like:

  *SELinux: Disabled on system, not enabling in X server<br>
  [dix] Could not init font path element /usr/share/fonts/X11/cyrillic, removing from list!<br>
  [dix] Could not init font path element /usr/share/fonts/X11/100dpi/:unscaled, removing from list!<br>
  [dix] Could not init font path element /usr/share/fonts/X11/75dpi/:unscaled, removing from list!<br>
  [dix] Could not init font path element /usr/share/fonts/X11/Type1, removing from list!<br>
  [dix] Could not init font path element /usr/share/fonts/X11/100dpi, removing from list!<br>
  [dix] Could not init font path element /usr/share/fonts/X11/75dpi, removing from list!<br>
  [dix] Could not init font path element /var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType, removing from list!<br>
  libudev: udev_monitor_enable_receiving: bind failed: Operation not permitted<br>
  config/udev: failed to bind the udev monitor<br>
  [config] failed to initialise udev<br>*


You can integrate this command to your test. For the following example `export 'DISPLAY=localhost:1.0'` is already in ~/.bashrc.

# Example test:

`

    require 'rubygems'
    require 'test/unit'
    require 'capybara/dsl'
    require 'capybara-webkit'
 
    Capybara.default_driver = :webkit
    Capybara.default_wait_time = 5
    Capybara.javascript_driver = :webkit
    Capybara.run_server = false

    class LazarWebTest < Test::Unit::TestCase
      include Capybara::DSL

      @@uri = "http://lazar.in-silico.ch"

      def test_00_start
        `Xvfb :1 -screen 0 1024x768x16 -nolisten inet6 &`
        sleep 2
      end
      
      def test_01_visit
        visit(@@uri)
        puts page.source
      end

      def test_99_kill
        `pidof Xvfb|xargs kill`
      end

    end
`