File: check_spec.rb

package info (click to toggle)
ruby-capybara 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 948 kB
  • ctags: 516
  • sloc: ruby: 7,998; makefile: 4
file content (113 lines) | stat: -rw-r--r-- 3,724 bytes parent folder | download | duplicates (2)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
Capybara::SpecHelper.spec "#check" do
  before do
    @session.visit('/form')
  end

  describe "'checked' attribute" do
    it "should be true if checked" do
      @session.check("Terms of Use")
      @session.find(:xpath, "//input[@id='form_terms_of_use']")['checked'].should be_true
    end

    it "should be false if unchecked" do
      @session.find(:xpath, "//input[@id='form_terms_of_use']")['checked'].should be_false
    end
  end

  it "should trigger associated events", :requires => [:js] do
    @session.visit('/with_js')
    @session.check('checkbox_with_event')
    @session.should have_css('#checkbox_event_triggered');
  end

  describe "checking" do
    it "should not change an already checked checkbox" do
      @session.find(:xpath, "//input[@id='form_pets_dog']")['checked'].should be_true
      @session.check('form_pets_dog')
      @session.find(:xpath, "//input[@id='form_pets_dog']")['checked'].should be_true
    end

    it "should check an unchecked checkbox" do
      @session.find(:xpath, "//input[@id='form_pets_cat']")['checked'].should be_false
      @session.check('form_pets_cat')
      @session.find(:xpath, "//input[@id='form_pets_cat']")['checked'].should be_true
    end
  end

  describe "unchecking" do
    it "should not change an already unchecked checkbox" do
      @session.find(:xpath, "//input[@id='form_pets_cat']")['checked'].should be_false
      @session.uncheck('form_pets_cat')
      @session.find(:xpath, "//input[@id='form_pets_cat']")['checked'].should be_false
    end

    it "should uncheck a checked checkbox" do
      @session.find(:xpath, "//input[@id='form_pets_dog']")['checked'].should be_true
      @session.uncheck('form_pets_dog')
      @session.find(:xpath, "//input[@id='form_pets_dog']")['checked'].should be_false
    end
  end

  it "should check a checkbox by id" do
    @session.check("form_pets_cat")
    @session.click_button('awesome')
    extract_results(@session)['pets'].should include('dog', 'cat', 'hamster')
  end

  it "should check a checkbox by label" do
    @session.check("Cat")
    @session.click_button('awesome')
    extract_results(@session)['pets'].should include('dog', 'cat', 'hamster')
  end

  it "casts to string" do
    @session.check(:"form_pets_cat")
    @session.click_button('awesome')
    extract_results(@session)['pets'].should include('dog', 'cat', 'hamster')
  end

  context "with a locator that doesn't exist" do
    it "should raise an error" do
      msg = "Unable to find checkbox \"does not exist\""
      expect do
        @session.check('does not exist')
      end.to raise_error(Capybara::ElementNotFound, msg)
    end
  end

  context "with a disabled checkbox" do
    it "should raise an error" do
      expect do
        @session.check('Disabled Checkbox')
      end.to raise_error(Capybara::ElementNotFound)
    end
  end

  context "with :exact option" do
    it "should accept partial matches when false" do
      @session.check('Ham', :exact => false)
      @session.click_button('awesome')
      extract_results(@session)['pets'].should include('hamster')
    end

    it "should not accept partial matches when true" do
      expect do
        @session.check('Ham', :exact => true)
      end.to raise_error(Capybara::ElementNotFound)
    end
  end

  context "with `option` option" do
    it "can check boxes by their value" do
      @session.check('form[pets][]', :option => "cat")
      @session.click_button('awesome')
      extract_results(@session)['pets'].should include('cat')
    end

    it "should raise an error if option not found" do
      expect do
        @session.check('form[pets][]', :option => "elephant")
      end.to raise_error(Capybara::ElementNotFound)
    end
  end
end