capybara-page-object 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +83 -0
- data/VERSION +1 -1
- data/capybara-page-object.gemspec +17 -10
- data/lib/capybara-page-object.rb +4 -4
- data/lib/collections.rb +11 -5
- data/lib/delegators.rb +14 -0
- data/lib/element.rb +14 -11
- data/lib/element/TODO.md +11 -0
- data/lib/element/anchor.rb +1 -5
- data/lib/element/form.rb +6 -10
- data/lib/element/form_field.rb +4 -9
- data/lib/element/head.rb +15 -0
- data/lib/element/image.rb +6 -11
- data/lib/element/input.rb +8 -13
- data/lib/element/list.rb +1 -15
- data/lib/element/list_item.rb +7 -0
- data/lib/element/meta.rb +2 -7
- data/lib/element/select.rb +1 -13
- data/lib/element/table.rb +11 -4
- data/lib/element/table_header.rb +4 -0
- data/lib/element/table_row.rb +7 -0
- data/lib/element/text_based_input.rb +16 -0
- data/lib/element/textarea.rb +5 -11
- data/lib/html5_data.rb +3 -7
- data/lib/key_value.rb +1 -1
- data/lib/node.rb +9 -54
- data/lib/page.rb +40 -0
- data/spec/common_spec.rb +13 -21
- data/spec/element/anchor_spec.rb +2 -2
- data/spec/element/base_spec.rb +2 -2
- data/spec/element/form_field_spec.rb +4 -4
- data/spec/element/form_spec.rb +26 -25
- data/spec/element/head_spec.rb +24 -0
- data/spec/element/image_spec.rb +5 -5
- data/spec/element/input_spec.rb +52 -17
- data/spec/element/list_item_spec.rb +19 -0
- data/spec/element/list_spec.rb +3 -20
- data/spec/element/meta_spec.rb +3 -2
- data/spec/element/select_spec.rb +4 -29
- data/spec/element/table_row_spec.rb +19 -0
- data/spec/element/table_spec.rb +35 -5
- data/spec/element/textarea_spec.rb +14 -4
- data/spec/node_spec.rb +14 -37
- data/spec/page_spec.rb +37 -0
- metadata +31 -24
- data/README.rdoc +0 -18
- data/lib/element/listitem.rb +0 -7
- data/lib/extractor/common.rb +0 -42
- data/lib/extractor/page_level.rb +0 -15
- data/lib/navigation.rb +0 -16
- data/spec/element/listitem_spec.rb +0 -25
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe "Head" do
|
4
|
+
context "#title" do
|
5
|
+
it "returns the page title" do
|
6
|
+
node = CapybaraPageObject::Head.from_string '<head><title>Hello World</title></head>', 'head'
|
7
|
+
node.title.should == 'Hello World'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "#meta_description" do
|
12
|
+
it "returns the meta description" do
|
13
|
+
node = CapybaraPageObject::Head.from_string '<head><meta name="description" content="meta description"></head>', 'head'
|
14
|
+
node.meta_description.should == "meta description"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "#meta_keywords" do
|
19
|
+
it "returns the meta keywords as an array" do
|
20
|
+
node = CapybaraPageObject::Head.from_string '<head><meta name="keywords" content="keyword 1, keyword 2"></head>', 'head'
|
21
|
+
node.meta_keywords.should == ["keyword 1", "keyword 2"]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/spec/element/image_spec.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
describe "Image" do
|
4
4
|
context "#alt" do
|
5
5
|
it "returns the alt attribute" do
|
6
|
-
i = CapybaraPageObject::Image.from_string '<img alt="alt image">'
|
6
|
+
i = CapybaraPageObject::Image.from_string '<img alt="alt image">', 'img'
|
7
7
|
i.alt.should == 'alt image'
|
8
8
|
end
|
9
9
|
|
10
10
|
it "strips whitespace" do
|
11
|
-
i = CapybaraPageObject::Image.from_string '<img alt=" alt image ">'
|
11
|
+
i = CapybaraPageObject::Image.from_string '<img alt=" alt image ">', 'img'
|
12
12
|
i.alt.should == 'alt image'
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
context "#key" do
|
17
17
|
it "return the alt attribute" do
|
18
|
-
i = CapybaraPageObject::Image.from_string '<img alt="alt image">'
|
18
|
+
i = CapybaraPageObject::Image.from_string '<img alt="alt image">', 'img'
|
19
19
|
i.key.should == 'alt image'
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
context "#value" do
|
24
24
|
it "return the source image as a URI object" do
|
25
|
-
i = CapybaraPageObject::Image.from_string '<img src="image.jpg">'
|
25
|
+
i = CapybaraPageObject::Image.from_string '<img src="image.jpg">', 'img'
|
26
26
|
i.value.class.should == URI::Generic
|
27
27
|
i.value.path.should == 'image.jpg'
|
28
28
|
end
|
data/spec/element/input_spec.rb
CHANGED
@@ -1,49 +1,84 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
describe "Input" do
|
4
4
|
context "#key" do
|
5
5
|
it "return the name attribute" do
|
6
|
-
i = CapybaraPageObject::Input.from_string '<input name="foo"/>'
|
6
|
+
i = CapybaraPageObject::Input.from_string '<input name="foo"/>', 'input'
|
7
7
|
i.key.should == 'foo'
|
8
8
|
end
|
9
9
|
end
|
10
|
-
|
11
|
-
#
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
|
11
|
+
context "#value" do
|
12
|
+
it "returns the value attribute" do
|
13
|
+
input = CapybaraPageObject::Input.from_string '<input value="foo">', 'input'
|
14
|
+
input.value.should == 'foo'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "#clear!" do
|
19
|
+
it "clears the value" do
|
20
|
+
input = CapybaraPageObject::Input.from_string '<input name="foo" value="bar">', 'input'
|
21
|
+
input.source.should_receive(:fill_in).with("foo", {:with => ''})
|
22
|
+
input.clear!
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "#blank?" do
|
27
|
+
it "is true if the value is an empty string" do
|
28
|
+
input = CapybaraPageObject::Input.from_string '<input name="foo" value="">', 'input'
|
29
|
+
input.should be_blank
|
30
|
+
end
|
31
|
+
it "is true if the value is not present" do
|
32
|
+
input = CapybaraPageObject::Input.from_string '<input name="foo">', 'input'
|
33
|
+
input.should be_blank
|
34
|
+
end
|
35
|
+
it "is true if the value is only whitespace" do
|
36
|
+
input = CapybaraPageObject::Input.from_string '<input name="foo" value=" \n \t ">', 'input'
|
37
|
+
input.should_not be_blank
|
38
|
+
end
|
39
|
+
it "is false if the value is an non-empty string" do
|
40
|
+
input = CapybaraPageObject::Input.from_string '<input name="foo" value="bar">', 'input'
|
41
|
+
input.should_not be_blank
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "#value=(value)" do
|
46
|
+
it "it allows the value to be set" do
|
47
|
+
input = CapybaraPageObject::Input.from_string '<input name="foo" value="old_bar">', 'input'
|
48
|
+
new_value = 'new_bar'
|
49
|
+
input.source.should_receive(:fill_in).with("foo", {:with => new_value})
|
50
|
+
input.value = new_value
|
51
|
+
end
|
52
|
+
end
|
18
53
|
|
19
54
|
context "#untyped" do
|
20
55
|
it "is true if the input has no type attribute" do
|
21
|
-
input = CapybaraPageObject::Input.from_string '<input type="text">'
|
56
|
+
input = CapybaraPageObject::Input.from_string '<input type="text">', 'input'
|
22
57
|
input.should_not be_untyped
|
23
58
|
end
|
24
59
|
|
25
60
|
it "is true if the input has no type attribute" do
|
26
|
-
input = CapybaraPageObject::Input.from_string '<input>'
|
61
|
+
input = CapybaraPageObject::Input.from_string '<input>', 'input'
|
27
62
|
input.should be_untyped
|
28
63
|
end
|
29
64
|
end
|
30
|
-
|
65
|
+
|
31
66
|
context "#checkable?" do
|
32
67
|
it "is true for a checkbox" do
|
33
68
|
checkbox = '<input type="checkbox">'
|
34
|
-
CapybaraPageObject::Input.from_string(checkbox).should be_checkable
|
69
|
+
CapybaraPageObject::Input.from_string(checkbox, 'input').should be_checkable
|
35
70
|
end
|
36
71
|
it "is true for a radio button" do
|
37
72
|
radio = '<input type="radio">'
|
38
|
-
CapybaraPageObject::Input.from_string(radio).should be_checkable
|
73
|
+
CapybaraPageObject::Input.from_string(radio, 'input').should be_checkable
|
39
74
|
end
|
40
75
|
it "is false an input with a blank type" do
|
41
76
|
none = '<input type="">'
|
42
|
-
CapybaraPageObject::Input.from_string(none).should_not be_checkable
|
77
|
+
CapybaraPageObject::Input.from_string(none, 'input').should_not be_checkable
|
43
78
|
end
|
44
79
|
it "is false for other kinds of input" do
|
45
80
|
text = '<input type="text">'
|
46
|
-
CapybaraPageObject::Input.from_string(text).should_not be_checkable
|
81
|
+
CapybaraPageObject::Input.from_string(text, 'input').should_not be_checkable
|
47
82
|
end
|
48
83
|
end
|
49
84
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe "ListItem" do
|
4
|
+
before do
|
5
|
+
s = <<-EOF
|
6
|
+
<li id="foo">
|
7
|
+
<span class="description">iPhone</span>
|
8
|
+
<span class="price">$500</span>
|
9
|
+
</li>
|
10
|
+
EOF
|
11
|
+
@list_item = CapybaraPageObject::ListItem.from_string s, 'li'
|
12
|
+
end
|
13
|
+
|
14
|
+
context "#text" do
|
15
|
+
it "return the tag's innter HTML content stripped of whitespace" do
|
16
|
+
@list_item.text.should == "iPhone\n $500"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/element/list_spec.rb
CHANGED
@@ -1,16 +1,13 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
module CapybaraPageObject
|
4
4
|
class MyListItem < CapybaraPageObject::Node
|
5
|
-
def value
|
6
|
-
find('.description').text.strip
|
7
|
-
end
|
8
5
|
end
|
9
6
|
end
|
10
7
|
|
11
8
|
describe "List" do
|
12
9
|
before do
|
13
|
-
|
10
|
+
html = <<-EOF
|
14
11
|
<ul id="products">
|
15
12
|
<li>
|
16
13
|
<span class="description">iPhone</span>
|
@@ -22,26 +19,12 @@ describe "List" do
|
|
22
19
|
</li>
|
23
20
|
</ul>
|
24
21
|
EOF
|
22
|
+
@list = CapybaraPageObject::List.from_string html, 'ul'
|
25
23
|
end
|
26
24
|
|
27
25
|
context "#items" do
|
28
26
|
it "return the list items" do
|
29
27
|
@list.should have(2).items
|
30
28
|
end
|
31
|
-
|
32
|
-
it "accept a custom list item object" do
|
33
|
-
@list.items({:factory => CapybaraPageObject::MyListItem}).first.value.should == "iPhone"
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
context "#key" do
|
38
|
-
it "be the id" do
|
39
|
-
@list.key.should == 'products'
|
40
|
-
end
|
41
|
-
|
42
|
-
it "should handle ordered list (ol)" do
|
43
|
-
list = CapybaraPageObject::List.from_string '<ol id="ordered"></ol>'
|
44
|
-
list.key.should == 'ordered'
|
45
|
-
end
|
46
29
|
end
|
47
30
|
end
|
data/spec/element/meta_spec.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
describe "Meta" do
|
4
4
|
context "Meta" do
|
5
5
|
before do
|
6
|
-
|
6
|
+
s = <<-EOF
|
7
7
|
<meta name="description" content="my description">
|
8
8
|
EOF
|
9
|
+
@meta = CapybaraPageObject::Meta.from_string s, 'meta'
|
9
10
|
end
|
10
11
|
|
11
12
|
context "#key" do
|
data/spec/element/select_spec.rb
CHANGED
@@ -1,36 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
describe "Select" do
|
4
4
|
before do
|
5
|
-
@select = CapybaraPageObject::Select.from_string '<select name="foo"></
|
6
|
-
end
|
7
|
-
|
8
|
-
context "#key" do
|
9
|
-
it "returns the name attribute" do
|
10
|
-
@select.key.should == 'foo'
|
11
|
-
end
|
5
|
+
@select = CapybaraPageObject::Select.from_string '<select name="foo"></select>', 'select'
|
12
6
|
end
|
13
7
|
|
14
|
-
|
15
|
-
|
16
|
-
s = CapybaraPageObject::Select.from_string <<-EOM
|
17
|
-
<select name="colour">
|
18
|
-
<option>red</option>
|
19
|
-
<option selected>blue</option>
|
20
|
-
</select>
|
21
|
-
EOM
|
22
|
-
s.value.should == 'blue'
|
23
|
-
end
|
24
|
-
|
25
|
-
it "returns an array for amultiple select" do
|
26
|
-
s = CapybaraPageObject::Select.from_string <<-EOM
|
27
|
-
<select name="countries" multiple>
|
28
|
-
<option>france</option>
|
29
|
-
<option selected>spain</option>
|
30
|
-
<option selected>germany</option>
|
31
|
-
</select>
|
32
|
-
EOM
|
33
|
-
s.value.should == ['spain', 'germany']
|
34
|
-
end
|
8
|
+
it "can be instantiated" do
|
9
|
+
@select.should_not be_nil
|
35
10
|
end
|
36
11
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe "CapybaraPageObject::TableRow" do
|
4
|
+
context "#header?" do
|
5
|
+
it "return true if the row contains a header cell" do
|
6
|
+
html = "<tr><th>a</th><td>b</td></tr>"
|
7
|
+
row = CapybaraPageObject::TableRow.from_string html, 'tr'
|
8
|
+
row.header?.should be_true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context "#header?" do
|
13
|
+
it "returns false if the row contains no header cells" do
|
14
|
+
html = "<tr><td>a</td><td>b</td></tr>"
|
15
|
+
row = CapybaraPageObject::TableRow.from_string html, 'tr'
|
16
|
+
row.header?.should be_false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/element/table_spec.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
describe "CapybaraPageObject::Table" do
|
4
4
|
before do
|
5
|
-
|
5
|
+
s = <<-EOF
|
6
6
|
<table id="table_1" class="table_1_class">
|
7
7
|
<tr id="table_1_tr_1">
|
8
8
|
<th id="table_1_th_1">TH1</th>
|
@@ -14,17 +14,47 @@ describe "CapybaraPageObject::Table" do
|
|
14
14
|
</tr>
|
15
15
|
</table>
|
16
16
|
EOF
|
17
|
+
@table = CapybaraPageObject::Table.from_string s, 'table'
|
18
|
+
# TODO clean this up
|
19
|
+
s = <<-EOF
|
20
|
+
<table id="table_1" class="table_1_class">
|
21
|
+
<tr id="table_1_tr_1">
|
22
|
+
<td id="table_1_th_1">TH1</td>
|
23
|
+
<td id="table_1_th_2">TH2</td>
|
24
|
+
</tr>
|
25
|
+
<tr id="table_1_tr_2">
|
26
|
+
<td id="table_1_td_1">TD1</td>
|
27
|
+
<td id="table_1_td_2">TD2</td>
|
28
|
+
</tr>
|
29
|
+
</table>
|
30
|
+
EOF
|
31
|
+
@table_without_headers = CapybaraPageObject::Table.from_string s, 'table'
|
17
32
|
end
|
18
|
-
|
33
|
+
|
19
34
|
context "#rows" do
|
20
35
|
it "return the table's rows" do
|
21
|
-
@table.rows
|
36
|
+
@table.rows.keys.should =~ ['table_1_tr_2']
|
22
37
|
end
|
23
38
|
end
|
24
39
|
|
25
40
|
context "#headers" do
|
26
41
|
it "return the table's headers" do
|
27
|
-
@table.headers.
|
42
|
+
@table.headers.keys.should =~ ["table_1_th_1", "table_1_th_2"]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "#rows" do
|
47
|
+
context "for a table with a header row" do
|
48
|
+
it "returns all the table rows except header row" do
|
49
|
+
@table.should have(1).rows
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "for a table without a header row" do
|
54
|
+
it "returns all the table rows" do
|
55
|
+
@table_without_headers.should have(2).rows
|
56
|
+
end
|
28
57
|
end
|
29
58
|
end
|
59
|
+
|
30
60
|
end
|
@@ -1,8 +1,9 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
describe "Textarea" do
|
4
4
|
before do
|
5
|
-
@textarea = CapybaraPageObject::Textarea.from_string '<textarea name="foo">bar</textarea>'
|
5
|
+
@textarea = CapybaraPageObject::Textarea.from_string '<textarea name="foo">bar</textarea>', 'textarea'
|
6
|
+
@blank_textarea = CapybaraPageObject::Textarea.from_string '<textarea name="foo"></textarea>', 'textarea'
|
6
7
|
end
|
7
8
|
|
8
9
|
context "#key" do
|
@@ -10,10 +11,19 @@ describe "Textarea" do
|
|
10
11
|
@textarea.key.should == 'foo'
|
11
12
|
end
|
12
13
|
end
|
13
|
-
|
14
|
+
|
14
15
|
context "#value" do
|
15
|
-
it "
|
16
|
+
it "returns the content" do
|
16
17
|
@textarea.value.should == 'bar'
|
17
18
|
end
|
18
19
|
end
|
20
|
+
|
21
|
+
context "#blank?" do
|
22
|
+
it "is false is there is content" do
|
23
|
+
@textarea.should_not be_blank
|
24
|
+
end
|
25
|
+
it "is true if there is no content" do
|
26
|
+
@blank_textarea.should be_blank
|
27
|
+
end
|
28
|
+
end
|
19
29
|
end
|
data/spec/node_spec.rb
CHANGED
@@ -1,45 +1,22 @@
|
|
1
|
-
require
|
1
|
+
require 'helper'
|
2
2
|
|
3
3
|
describe "Page" do
|
4
|
-
context "
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
node.title.should == 'Hello World'
|
9
|
-
end
|
4
|
+
context "#classes" do
|
5
|
+
it "returns an array of class names" do
|
6
|
+
anchor = CapybaraPageObject::Anchor.from_string '<a class="foo bar">', 'a'
|
7
|
+
anchor.classes.should =~ ['foo', 'bar']
|
10
8
|
end
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
node = CapybaraPageObject::Node.from_string '<meta name="description" content="meta description">'
|
15
|
-
node.meta_description.should == "meta description"
|
16
|
-
end
|
9
|
+
it "handles extraneous spacing" do
|
10
|
+
anchor = CapybaraPageObject::Anchor.from_string '<a class=" foo bar ">', 'a'
|
11
|
+
anchor.classes.should =~ ['foo', 'bar']
|
17
12
|
end
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
node = CapybaraPageObject::Node.from_string '<meta name="keywords" content="keyword 1, keyword 2">'
|
22
|
-
node.meta_keywords.should == ["keyword 1", "keyword 2"]
|
23
|
-
end
|
13
|
+
it "returns an empty array classes is empty" do
|
14
|
+
anchor = CapybaraPageObject::Anchor.from_string '<a class=" ">', 'a'
|
15
|
+
anchor.classes.should be_empty
|
24
16
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
anchor = CapybaraPageObject::Anchor.from_string '<a class="foo bar">'
|
29
|
-
anchor.classes.should =~ ['foo', 'bar']
|
30
|
-
end
|
31
|
-
it "handles extraneous spacing" do
|
32
|
-
anchor = CapybaraPageObject::Anchor.from_string '<a class=" foo bar ">'
|
33
|
-
anchor.classes.should =~ ['foo', 'bar']
|
34
|
-
end
|
35
|
-
it "returns an empty array classes is empty" do
|
36
|
-
anchor = CapybaraPageObject::Anchor.from_string '<a class=" ">'
|
37
|
-
anchor.classes.should be_empty
|
38
|
-
end
|
39
|
-
it "returns an empty array when the classes attribute is not present" do
|
40
|
-
anchor = CapybaraPageObject::Anchor.from_string '<a/>'
|
41
|
-
anchor.classes.should be_empty
|
42
|
-
end
|
17
|
+
it "returns an empty array when the classes attribute is not present" do
|
18
|
+
anchor = CapybaraPageObject::Anchor.from_string '<a/>', 'a'
|
19
|
+
anchor.classes.should be_empty
|
43
20
|
end
|
44
21
|
end
|
45
22
|
end
|