File: has_xpath_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 (127 lines) | stat: -rw-r--r-- 4,879 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
Capybara::SpecHelper.spec '#has_xpath?' do
  before do
    @session.visit('/with_html')
  end

  it "should be true if the given selector is on the page" do
    @session.should have_xpath("//p")
    @session.should have_xpath("//p//a[@id='foo']")
    @session.should have_xpath("//p[contains(.,'est')]")
  end

  it "should be false if the given selector is not on the page" do
    @session.should_not have_xpath("//abbr")
    @session.should_not have_xpath("//p//a[@id='doesnotexist']")
    @session.should_not have_xpath("//p[contains(.,'thisstringisnotonpage')]")
  end

  it "should use xpath even if default selector is CSS" do
    Capybara.default_selector = :css
    @session.should_not have_xpath("//p//a[@id='doesnotexist']")
  end

  it "should respect scopes" do
    @session.within "//p[@id='first']" do
      @session.should have_xpath(".//a[@id='foo']")
      @session.should_not have_xpath(".//a[@id='red']")
    end
  end

  it "should wait for content to appear", :requires => [:js] do
    @session.visit('/with_js')
    @session.click_link('Click me')
    @session.should have_xpath("//input[@type='submit' and @value='New Here']")
  end

  context "with count" do
    it "should be true if the content occurs the given number of times" do
      @session.should have_xpath("//p", :count => 3)
      @session.should have_xpath("//p//a[@id='foo']", :count => 1)
      @session.should have_xpath("//p[contains(.,'est')]", :count => 1)
      @session.should have_xpath("//p//a[@id='doesnotexist']", :count => 0)
    end

    it "should be false if the content occurs a different number of times than the given" do
      @session.should_not have_xpath("//p", :count => 6)
      @session.should_not have_xpath("//p//a[@id='foo']", :count => 2)
      @session.should_not have_xpath("//p[contains(.,'est')]", :count => 5)
      @session.should_not have_xpath("//p//a[@id='doesnotexist']", :count => 1)
    end
  end

  context "with text" do
    it "should discard all matches where the given string is not contained" do
      @session.should have_xpath("//p//a", :text => "Redirect", :count => 1)
      @session.should_not have_xpath("//p", :text => "Doesnotexist")
    end

    it "should discard all matches where the given regexp is not matched" do
      @session.should have_xpath("//p//a", :text => /re[dab]i/i, :count => 1)
      @session.should_not have_xpath("//p//a", :text => /Red$/)
    end
  end
end

Capybara::SpecHelper.spec '#has_no_xpath?' do
  before do
    @session.visit('/with_html')
  end

  it "should be false if the given selector is on the page" do
    @session.should_not have_no_xpath("//p")
    @session.should_not have_no_xpath("//p//a[@id='foo']")
    @session.should_not have_no_xpath("//p[contains(.,'est')]")
  end

  it "should be true if the given selector is not on the page" do
    @session.should have_no_xpath("//abbr")
    @session.should have_no_xpath("//p//a[@id='doesnotexist']")
    @session.should have_no_xpath("//p[contains(.,'thisstringisnotonpage')]")
  end

  it "should use xpath even if default selector is CSS" do
    Capybara.default_selector = :css
    @session.should have_no_xpath("//p//a[@id='doesnotexist']")
  end

  it "should respect scopes" do
    @session.within "//p[@id='first']" do
      @session.should_not have_no_xpath(".//a[@id='foo']")
      @session.should have_no_xpath(".//a[@id='red']")
    end
  end

  it "should wait for content to disappear", :requires => [:js] do
    @session.visit('/with_js')
    @session.click_link('Click me')
    @session.should have_no_xpath("//p[@id='change']")
  end

  context "with count" do
    it "should be false if the content occurs the given number of times" do
      @session.should_not have_no_xpath("//p", :count => 3)
      @session.should_not have_no_xpath("//p//a[@id='foo']", :count => 1)
      @session.should_not have_no_xpath("//p[contains(.,'est')]", :count => 1)
      @session.should_not have_no_xpath("//p//a[@id='doesnotexist']", :count => 0)
    end

    it "should be true if the content occurs a different number of times than the given" do
      @session.should have_no_xpath("//p", :count => 6)
      @session.should have_no_xpath("//p//a[@id='foo']", :count => 2)
      @session.should have_no_xpath("//p[contains(.,'est')]", :count => 5)
      @session.should have_no_xpath("//p//a[@id='doesnotexist']", :count => 1)
    end
  end

  context "with text" do
    it "should discard all matches where the given string is contained" do
      @session.should_not have_no_xpath("//p//a", :text => "Redirect", :count => 1)
      @session.should have_no_xpath("//p", :text => "Doesnotexist")
    end

    it "should discard all matches where the given regexp is matched" do
      @session.should_not have_no_xpath("//p//a", :text => /re[dab]i/i, :count => 1)
      @session.should have_no_xpath("//p//a", :text => /Red$/)
    end
  end
end