locator 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +10 -2
- data/TODO +4 -0
- data/lib/locator/boolean.rb +1 -1
- data/lib/locator/dom/nokogiri/element.rb +14 -5
- data/lib/locator/dom/nokogiri/page.rb +7 -3
- data/lib/locator/dom/nokogiri.rb +1 -1
- data/lib/locator/dom.rb +1 -1
- data/lib/locator/element/area.rb +2 -2
- data/lib/locator/element/button.rb +3 -3
- data/lib/locator/element/check_box.rb +9 -0
- data/lib/locator/element/elements_list.rb +6 -6
- data/lib/locator/element/field.rb +2 -3
- data/lib/locator/element/file.rb +9 -0
- data/lib/locator/element/form.rb +2 -2
- data/lib/locator/element/form_element.rb +9 -0
- data/lib/locator/element/hidden_field.rb +9 -0
- data/lib/locator/element/input.rb +11 -0
- data/lib/locator/element/label.rb +2 -2
- data/lib/locator/element/labeled_element.rb +8 -10
- data/lib/locator/element/link.rb +2 -2
- data/lib/locator/element/radio_button.rb +9 -0
- data/lib/locator/element/select.rb +3 -3
- data/lib/locator/element/select_option.rb +2 -2
- data/lib/locator/element/text_area.rb +3 -3
- data/lib/locator/element.rb +31 -24
- data/lib/locator/result.rb +46 -0
- data/lib/locator/version.rb +2 -2
- data/lib/locator/xpath.rb +33 -14
- data/lib/locator.rb +43 -21
- data/test/all.rb +1 -1
- data/test/locator/element/button_test.rb +10 -5
- data/test/locator/element/field_test.rb +24 -13
- data/test/locator/element/form_test.rb +12 -6
- data/test/locator/element/label_test.rb +10 -6
- data/test/locator/element/link_test.rb +16 -14
- data/test/locator/element/select_option_test.rb +14 -7
- data/test/locator/element/select_test.rb +10 -6
- data/test/locator/element/text_area_test.rb +10 -5
- data/test/locator/element_test.rb +79 -21
- data/test/locator/xpath_test.rb +0 -5
- data/test/locator_test.rb +63 -13
- data/test/test_helper.rb +0 -14
- metadata +11 -2
@@ -5,22 +5,27 @@ class ElementButtonTest < Test::Unit::TestCase
|
|
5
5
|
Button = Locator::Element::Button
|
6
6
|
|
7
7
|
test "finds a button" do
|
8
|
-
|
8
|
+
html = '<button></button>'
|
9
|
+
assert_equal 'button', Button.new.locate(html).tag_name
|
9
10
|
end
|
10
11
|
|
11
12
|
test "finds a submit input" do
|
12
|
-
|
13
|
+
html = '<input type="submit">'
|
14
|
+
assert_equal 'input', Button.new.locate(html).tag_name
|
13
15
|
end
|
14
16
|
|
15
17
|
test "finds a button input" do
|
16
|
-
|
18
|
+
html = '<input type="button">'
|
19
|
+
assert_equal 'input', Button.new.locate(html).tag_name
|
17
20
|
end
|
18
21
|
|
19
22
|
test "finds an image input" do
|
20
|
-
|
23
|
+
html = '<input type="image">'
|
24
|
+
assert_equal 'input', Button.new.locate(html).tag_name
|
21
25
|
end
|
22
26
|
|
23
27
|
test "does not find a checkbox input" do
|
24
|
-
|
28
|
+
html = '<input type="checkbox">'
|
29
|
+
assert_nil Button.new.locate(html)
|
25
30
|
end
|
26
31
|
end
|
@@ -5,56 +5,67 @@ class ElementFieldTest < Test::Unit::TestCase
|
|
5
5
|
Field = Locator::Element::Field
|
6
6
|
|
7
7
|
test "finds a textarea" do
|
8
|
-
|
8
|
+
html = '<textarea></textarea>'
|
9
|
+
assert_equal 'textarea', Field.new.locate(html).tag_name
|
9
10
|
end
|
10
11
|
|
11
12
|
test "finds a text input" do
|
12
|
-
|
13
|
+
html = '<input type="text">'
|
14
|
+
assert_equal 'input', Field.new.locate(html).tag_name
|
13
15
|
end
|
14
16
|
|
15
17
|
test "finds a password input" do
|
16
|
-
|
18
|
+
html = '<input type="password">'
|
19
|
+
assert_equal 'input', Field.new.locate(html).tag_name
|
17
20
|
end
|
18
21
|
|
19
22
|
test "finds an email input" do
|
20
|
-
|
23
|
+
html = '<input type="email">'
|
24
|
+
assert_equal 'input', Field.new.locate(html).tag_name
|
21
25
|
end
|
22
26
|
|
23
27
|
test "finds a url input" do
|
24
|
-
|
28
|
+
html = '<input type="url">'
|
29
|
+
assert_equal 'input', Field.new.locate(html).tag_name
|
25
30
|
end
|
26
31
|
|
27
32
|
test "finds a search input" do
|
28
|
-
|
33
|
+
html = '<input type="search">'
|
34
|
+
assert_equal 'input', Field.new.locate(html).tag_name
|
29
35
|
end
|
30
36
|
|
31
37
|
test "finds a tel input" do
|
32
|
-
|
38
|
+
html = '<input type="tel">'
|
39
|
+
assert_equal 'input', Field.new.locate(html).tag_name
|
33
40
|
end
|
34
41
|
|
35
42
|
test "finds a color input" do
|
36
|
-
|
43
|
+
html = '<input type="color">'
|
44
|
+
assert_equal 'input', Field.new.locate(html).tag_name
|
37
45
|
end
|
38
46
|
|
39
47
|
test "finds a text input by name" do
|
40
|
-
|
48
|
+
html = '<input type="text" name="foo">'
|
49
|
+
assert_equal 'input', Field.new.locate(html, 'foo').tag_name
|
41
50
|
end
|
42
51
|
|
43
52
|
test "finds a text input by class attribute" do
|
44
|
-
|
53
|
+
html = '<input type="text" class="foo">'
|
54
|
+
assert_equal 'input', Field.new.locate(html, :class => 'foo').tag_name
|
45
55
|
end
|
46
56
|
|
47
57
|
test "finds an input by label content" do
|
48
58
|
html = '<label for="bar">foo</label><input type="text" id="bar">'
|
49
|
-
|
59
|
+
assert_equal 'input', Field.new.locate(html, 'foo').tag_name
|
50
60
|
end
|
51
61
|
|
52
62
|
test "finds an input by label content and input class" do
|
53
63
|
html = '<label for="bar">foo</label><input type="text" id="bar" class="baz">'
|
54
|
-
|
64
|
+
assert_equal 'input', Field.new.locate(html, 'foo', :class => 'baz').tag_name
|
55
65
|
end
|
56
66
|
|
57
67
|
test "does not find a checkbox input" do
|
58
|
-
|
68
|
+
html = '<input type="checkbox">'
|
69
|
+
assert_nil Field.new.locate(html)
|
59
70
|
end
|
60
71
|
end
|
@@ -5,26 +5,32 @@ class ElementFormTest < Test::Unit::TestCase
|
|
5
5
|
Form = Locator::Element::Form
|
6
6
|
|
7
7
|
test "finds a form" do
|
8
|
-
|
8
|
+
html = '<form></form>'
|
9
|
+
assert_equal 'form', Form.new.locate(html).tag_name
|
9
10
|
end
|
10
11
|
|
11
12
|
test "finds a form by id" do
|
12
|
-
|
13
|
+
html = '<form id="foo"></form>'
|
14
|
+
assert_equal 'form', Form.new.locate(html, 'foo').tag_name
|
13
15
|
end
|
14
16
|
|
15
17
|
test "finds a form by name" do
|
16
|
-
|
18
|
+
html = '<form name="foo"></form>'
|
19
|
+
assert_equal 'form', Form.new.locate(html, 'foo').tag_name
|
17
20
|
end
|
18
21
|
|
19
22
|
test "finds a form by class" do
|
20
|
-
|
23
|
+
html = '<form class="foo"></form>'
|
24
|
+
assert_equal 'form', Form.new.locate(html, :class => 'foo').tag_name
|
21
25
|
end
|
22
26
|
|
23
27
|
test "does not find a form when id does not match" do
|
24
|
-
|
28
|
+
html = '<form id="bar"></form>'
|
29
|
+
assert_nil Form.new.locate(html, :class => 'foo')
|
25
30
|
end
|
26
31
|
|
27
32
|
test "does not find a form when class does not match" do
|
28
|
-
|
33
|
+
html = '<form class="bar"></form>'
|
34
|
+
assert_nil Form.new.locate(html, :class => 'foo')
|
29
35
|
end
|
30
36
|
end
|
@@ -1,26 +1,30 @@
|
|
1
1
|
require File.expand_path('../../../test_helper', __FILE__)
|
2
|
-
require 'locator/element'
|
3
2
|
|
4
3
|
class ElementLabelTest < Test::Unit::TestCase
|
5
4
|
Label = Locator::Element::Label
|
6
5
|
|
7
6
|
test "finds a label" do
|
8
|
-
|
7
|
+
html = '<label></label>'
|
8
|
+
assert_equal 'label', Label.new.locate(html).tag_name
|
9
9
|
end
|
10
10
|
|
11
11
|
test "finds a label by id" do
|
12
|
-
|
12
|
+
html = '<label id="foo"></label>'
|
13
|
+
assert_equal 'label', Label.new.locate(html, 'foo').tag_name
|
13
14
|
end
|
14
15
|
|
15
16
|
test "finds a label by class" do
|
16
|
-
|
17
|
+
html = '<label class="foo"></label>'
|
18
|
+
assert_equal 'label', Label.new.locate(html, :class => 'foo').tag_name
|
17
19
|
end
|
18
20
|
|
19
21
|
test "does not find a label when id does not match" do
|
20
|
-
|
22
|
+
html = '<label id="bar"></label>'
|
23
|
+
assert_nil Label.new.locate(html, :class => 'foo')
|
21
24
|
end
|
22
25
|
|
23
26
|
test "does not find a label when class does not match" do
|
24
|
-
|
27
|
+
html = '<label class="bar"></label>'
|
28
|
+
assert_nil Label.new.locate(html, :class => 'foo')
|
25
29
|
end
|
26
30
|
end
|
@@ -1,34 +1,36 @@
|
|
1
1
|
require File.expand_path('../../../test_helper', __FILE__)
|
2
|
-
require 'locator/element'
|
3
2
|
|
4
|
-
class
|
3
|
+
class LocatorElementLinkTest < Test::Unit::TestCase
|
5
4
|
Link = Locator::Element::Link
|
6
5
|
|
7
6
|
test "finds a link" do
|
8
|
-
|
7
|
+
html = '<a class="foo"></a><a href="" class="bar"></a>'
|
8
|
+
assert_equal 'bar', Link.new.locate(html).attribute('class')
|
9
9
|
end
|
10
10
|
|
11
11
|
test "finds a link by id" do
|
12
|
-
|
12
|
+
html = '<a href="" id="foo"></a><a href="" id="bar"></a>'
|
13
|
+
assert_equal 'bar', Link.new.locate(html, :id => 'bar').attribute('id')
|
13
14
|
end
|
14
15
|
|
15
16
|
test "finds a link by class" do
|
16
|
-
|
17
|
+
html = '<a href="" class="foo"></a><a href="" class="bar"></a>'
|
18
|
+
assert_equal 'bar', Link.new.locate(html, :class => 'bar').attribute('class')
|
17
19
|
end
|
18
20
|
|
19
|
-
test "finds
|
20
|
-
|
21
|
+
test "finds a link by content" do
|
22
|
+
html = '<a href="">foo</a><a href="">bar</a>'
|
23
|
+
assert_equal 'bar', Link.new.locate(html, 'bar').content
|
21
24
|
end
|
22
25
|
|
23
26
|
test "does not find an anchor" do
|
24
|
-
|
27
|
+
html =
|
28
|
+
assert_nil Link.new.locate('<a name="">foo</a>')
|
25
29
|
end
|
26
30
|
|
27
|
-
test "
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
test "does not find a link when class does not match" do
|
32
|
-
assert_no_locate '<a href="" class="bar"></a>', Link.new.xpath(:class => 'foo')
|
31
|
+
test "finds a link with a href starting with a slash" do
|
32
|
+
html = '<a href="/foo/bar" class="foo"></a><a href="" class="bar"></a>'
|
33
|
+
assert_equal '/foo/bar', Link.new.locate(html, :href => '/foo/bar').attribute('href')
|
33
34
|
end
|
34
35
|
end
|
36
|
+
|
@@ -5,30 +5,37 @@ class ElementSelectOptionOptionTest < Test::Unit::TestCase
|
|
5
5
|
SelectOption = Locator::Element::SelectOption
|
6
6
|
|
7
7
|
test "finds an option" do
|
8
|
-
|
8
|
+
html = '<option></option>'
|
9
|
+
assert_equal 'option', SelectOption.new.locate(html).tag_name
|
9
10
|
end
|
10
11
|
|
11
12
|
test "finds an option by id" do
|
12
|
-
|
13
|
+
html = '<option id="foo"></option>'
|
14
|
+
assert_equal 'option', SelectOption.new.locate(html, 'foo').tag_name
|
13
15
|
end
|
14
16
|
|
15
17
|
test "finds an option by value" do
|
16
|
-
|
18
|
+
html = '<option value="foo"></option>'
|
19
|
+
assert_equal 'option', SelectOption.new.locate(html, 'foo').tag_name
|
17
20
|
end
|
18
21
|
|
19
22
|
test "finds an option by content" do
|
20
|
-
|
23
|
+
html = '<option>foo</option>'
|
24
|
+
assert_equal 'option', SelectOption.new.locate(html, 'foo').tag_name
|
21
25
|
end
|
22
26
|
|
23
27
|
test "finds an option by class attribute" do
|
24
|
-
|
28
|
+
html = '<option class="foo"></option>'
|
29
|
+
assert_equal 'option', SelectOption.new.locate(html, :class => 'foo').tag_name
|
25
30
|
end
|
26
31
|
|
27
32
|
test "does not find an option when id does not match" do
|
28
|
-
|
33
|
+
html = '<option id="bar"></option>'
|
34
|
+
assert_nil SelectOption.new.locate(html, :class => 'foo')
|
29
35
|
end
|
30
36
|
|
31
37
|
test "does not find an option when class does not match" do
|
32
|
-
|
38
|
+
html = '<option class="bar"></option>'
|
39
|
+
assert_nil SelectOption.new.locate(html, :class => 'foo')
|
33
40
|
end
|
34
41
|
end
|
@@ -1,26 +1,30 @@
|
|
1
1
|
require File.expand_path('../../../test_helper', __FILE__)
|
2
|
-
require 'locator/element'
|
3
2
|
|
4
3
|
class ElementSelectTest < Test::Unit::TestCase
|
5
4
|
Select = Locator::Element::Select
|
6
5
|
|
7
6
|
test "finds a select" do
|
8
|
-
|
7
|
+
html = '<select id="foo" class="foo"></select><select id="bar" class="bar"></select>'
|
8
|
+
assert_equal 'select', Select.new.locate(html).tag_name
|
9
9
|
end
|
10
10
|
|
11
11
|
test "finds a select by id" do
|
12
|
-
|
12
|
+
html = '<select id="foo"></select>'
|
13
|
+
assert_equal 'select', Select.new.locate(html, 'foo').tag_name
|
13
14
|
end
|
14
15
|
|
15
16
|
test "finds a select by class" do
|
16
|
-
|
17
|
+
html = '<select class="foo"></select>'
|
18
|
+
assert_equal 'select', Select.new.locate(html, :class => 'foo').tag_name
|
17
19
|
end
|
18
20
|
|
19
21
|
test "does not find a select when id does not match" do
|
20
|
-
|
22
|
+
html = '<select id="bar"></select>'
|
23
|
+
assert_nil Select.new.locate(html, :class => 'foo')
|
21
24
|
end
|
22
25
|
|
23
26
|
test "does not find a select when class does not match" do
|
24
|
-
|
27
|
+
html = '<select class="bar"></select>'
|
28
|
+
assert_nil Select.new.locate(html, :class => 'foo')
|
25
29
|
end
|
26
30
|
end
|
@@ -5,22 +5,27 @@ class ElementTextAreaTest < Test::Unit::TestCase
|
|
5
5
|
TextArea = Locator::Element::TextArea
|
6
6
|
|
7
7
|
test "finds a textarea" do
|
8
|
-
|
8
|
+
html = '<textarea></textarea>'
|
9
|
+
assert_equal 'textarea', TextArea.new.locate(html).tag_name
|
9
10
|
end
|
10
11
|
|
11
12
|
test "finds a textarea by id" do
|
12
|
-
|
13
|
+
html = '<textarea id="foo"></textarea>'
|
14
|
+
assert_equal 'textarea', TextArea.new.locate(html, 'foo').tag_name
|
13
15
|
end
|
14
16
|
|
15
17
|
test "finds a textarea by class" do
|
16
|
-
|
18
|
+
html = '<textarea class="foo"></textarea>'
|
19
|
+
assert_equal 'textarea', TextArea.new.locate(html, :class => 'foo').tag_name
|
17
20
|
end
|
18
21
|
|
19
22
|
test "does not find a textarea when id does not match" do
|
20
|
-
|
23
|
+
html = '<textarea id="bar"></textarea>'
|
24
|
+
assert_nil TextArea.new.locate(html, :class => 'foo')
|
21
25
|
end
|
22
26
|
|
23
27
|
test "does not find a textarea when class does not match" do
|
24
|
-
|
28
|
+
html = '<textarea class="bar"></textarea>'
|
29
|
+
assert_nil TextArea.new.locate(html, :class => 'foo')
|
25
30
|
end
|
26
31
|
end
|
@@ -1,40 +1,98 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
require 'locator/element'
|
3
2
|
|
4
|
-
class
|
5
|
-
|
6
|
-
|
3
|
+
class LocatorElementTest < Test::Unit::TestCase
|
4
|
+
include Locator
|
5
|
+
|
6
|
+
# xpath
|
7
|
+
|
8
|
+
test "xpath without further arguments given" do
|
9
|
+
assert_equal './/*', Element.new.xpath
|
7
10
|
end
|
8
|
-
|
9
|
-
test "
|
10
|
-
|
11
|
+
|
12
|
+
test "xpath with a node name" do
|
13
|
+
xpath = Element.new(:div).xpath
|
14
|
+
assert_equal './/div', xpath
|
15
|
+
end
|
16
|
+
|
17
|
+
test "xpath with attributes" do
|
18
|
+
xpath = Element.new(nil, nil, :type => 'type', :class => 'class').xpath
|
19
|
+
assert_equal ".//*[@type=\"type\"][contains(concat(' ', @class, ' '), concat(' ', \"class\", ' '))]", xpath
|
20
|
+
end
|
21
|
+
|
22
|
+
test "xpath with node name and attributes" do
|
23
|
+
xpath = Element.new(:div, nil, :type => 'type', :class => 'class').xpath
|
24
|
+
assert_equal ".//div[@type=\"type\"][contains(concat(' ', @class, ' '), concat(' ', \"class\", ' '))]", xpath
|
25
|
+
end
|
26
|
+
|
27
|
+
# all
|
28
|
+
|
29
|
+
test "all selects all elements when given no attributes" do
|
30
|
+
html = '<a class="foo"></a><p class="bar"></p>'
|
31
|
+
elements = Element.new.all(html)
|
32
|
+
assert_equal %w(html body a p), elements.map { |element| element.tag_name }
|
11
33
|
end
|
12
34
|
|
13
|
-
test "
|
14
|
-
|
35
|
+
test "all selects all nodes with given node name" do
|
36
|
+
html = '<a class="foo"></a><p class="bar"></p>'
|
37
|
+
elements = Element.new('a').all(html)
|
38
|
+
assert_equal %w(a), elements.map { |element| element.tag_name }
|
39
|
+
end
|
40
|
+
|
41
|
+
test "all selects all nodes with attribute given to initialize" do
|
42
|
+
html = '<a class="foo"></a><p class="bar"></p>'
|
43
|
+
elements = Element.new(nil, nil, :class => 'foo').all(html)
|
44
|
+
assert_equal %w(a), elements.map { |element| element.tag_name }
|
45
|
+
end
|
46
|
+
|
47
|
+
test "all selects all nodes with attribute given to all" do
|
48
|
+
html = '<a class="foo"></a><p class="bar"></p>'
|
49
|
+
elements = Element.new.all(html, :class => 'foo')
|
50
|
+
assert_equal %w(a), elements.map { |element| element.tag_name }
|
51
|
+
end
|
52
|
+
|
53
|
+
# locate
|
54
|
+
|
55
|
+
test "locate selects an element based on the length of the matching value" do
|
56
|
+
html = %(
|
57
|
+
<a href="#">the link with extra text</a>
|
58
|
+
<a href="http://www.some-very-long-url.com">the link ...</a>
|
59
|
+
)
|
60
|
+
element = Element.new.locate(html, 'the link')
|
61
|
+
assert_equal 'the link ...', element.content
|
15
62
|
end
|
16
63
|
|
17
|
-
test "
|
18
|
-
|
64
|
+
test "locate selects an element containing whitespace" do
|
65
|
+
html = %(
|
66
|
+
<a href="#">
|
67
|
+
the
|
68
|
+
link
|
69
|
+
</a>
|
70
|
+
)
|
71
|
+
assert Element.new.locate(html, 'the link')
|
19
72
|
end
|
20
73
|
|
21
|
-
test "
|
22
|
-
|
74
|
+
test "locate selects an element containing extra tags" do
|
75
|
+
html = '<a href="#">the <span>link</span></a>'
|
76
|
+
assert Element.new.locate(html, 'the link')
|
23
77
|
end
|
24
78
|
|
25
|
-
test "
|
26
|
-
|
79
|
+
test "locate selects an element containing extra text (1)" do
|
80
|
+
html = '<a href="#">the link »</label>'
|
81
|
+
assert Element.new.locate(html, 'the link')
|
27
82
|
end
|
28
83
|
|
29
|
-
test "
|
30
|
-
|
84
|
+
test "locate selects an element containing extra text (2)" do
|
85
|
+
html = '<a href="#">(the link)</label>'
|
86
|
+
assert Element.new.locate(html, 'the link')
|
31
87
|
end
|
32
88
|
|
33
|
-
test "
|
34
|
-
|
89
|
+
test "does not find a link when id does not match" do
|
90
|
+
html = '<a href="" id="bar"></a>'
|
91
|
+
assert_nil Element.new.locate(html, :id => 'foo')
|
35
92
|
end
|
36
93
|
|
37
|
-
test "
|
38
|
-
|
94
|
+
test "does not find a link when class does not match" do
|
95
|
+
html = '<a href="" class="bar"></a>'
|
96
|
+
assert_nil Element.new.locate(html, :class => 'foo')
|
39
97
|
end
|
40
98
|
end
|
data/test/locator/xpath_test.rb
CHANGED
@@ -27,11 +27,6 @@ class XpathTest < Test::Unit::TestCase
|
|
27
27
|
path = Xpath.new(['input', 'button']).to_s
|
28
28
|
assert_equal ".//input | .//button", path
|
29
29
|
end
|
30
|
-
|
31
|
-
test "xpath or xpath" do
|
32
|
-
path = Xpath.new('input', :type => 'text').or!(Xpath.new('textarea')).to_s
|
33
|
-
assert_equal %(.//input[@type="text"] | .//textarea), path
|
34
|
-
end
|
35
30
|
|
36
31
|
test "attribute equals to one of multiple values" do
|
37
32
|
path = Xpath.new('input', :type => [:text, :password]).to_s
|
data/test/locator_test.rb
CHANGED
@@ -1,31 +1,81 @@
|
|
1
1
|
require File.expand_path('../test_helper', __FILE__)
|
2
|
-
require 'locator/element'
|
3
2
|
|
4
3
|
class LocatorTest < Test::Unit::TestCase
|
4
|
+
include Locator
|
5
|
+
|
6
|
+
test "returns the element if given an element" do
|
7
|
+
html = '<p></p>'
|
8
|
+
element = Locator.locate(html, :p)
|
9
|
+
assert_equal element, Locator.locate(html, element)
|
10
|
+
end
|
11
|
+
|
5
12
|
test "looks up a locator/element class by type" do
|
6
13
|
assert_equal Locator::Element::Field, Locator[:field]
|
7
14
|
end
|
8
15
|
|
9
16
|
test "generates an xpath for type and given args" do
|
10
|
-
|
11
|
-
assert_equal './/form[@
|
17
|
+
xpath = Locator.xpath(:form, :id => 'bar')
|
18
|
+
assert_equal './/form[@id="bar"]', xpath
|
12
19
|
end
|
13
20
|
|
14
21
|
test "locates an element from html" do
|
15
|
-
html = '<html><body><
|
16
|
-
element = Locator.
|
17
|
-
assert_equal '
|
22
|
+
html = '<html><body><p></p><form class="bar"></form></body></html>'
|
23
|
+
element = Locator.locate(html, :form)
|
24
|
+
assert_equal 'form', element.tag_name
|
25
|
+
end
|
26
|
+
|
27
|
+
# within
|
28
|
+
|
29
|
+
test "within scopes to a locator" do
|
30
|
+
html = '<form></form><div id="bar"><form id="foo"></form></div>'
|
31
|
+
element = Locator.within(:div, :id => 'bar') { |scope| scope.locate(html, :form) }
|
32
|
+
assert_equal 'foo', element.attribute('id')
|
33
|
+
end
|
34
|
+
|
35
|
+
test "within scopes to a css selector" do
|
36
|
+
html = '<form></form><div id="bar"><form id="foo"></form></div>'
|
37
|
+
element = Locator.within('#bar') { |scope| scope.locate(html, :form) }
|
38
|
+
assert_equal 'foo', element.attribute('id')
|
18
39
|
end
|
19
40
|
|
20
41
|
test "within scopes to an xpath" do
|
21
|
-
html = '<form
|
22
|
-
element = Locator.
|
23
|
-
assert_equal '
|
42
|
+
html = '<form></form><div id="bar"><form id="foo"></form></div>'
|
43
|
+
element = Locator.within('//div[@id="bar"]') { |scope| scope.locate(html, :form) }
|
44
|
+
assert_equal 'foo', element.attribute('id')
|
45
|
+
end
|
46
|
+
|
47
|
+
test "nested within blocks" do
|
48
|
+
html = '<form></form><div><form><p id="foo"><p></form></div>'
|
49
|
+
element = Locator.within(:div) { |scope| scope.within(:form) { |scope| scope.locate(html, :p) } }
|
50
|
+
assert_equal 'foo', element.attribute('id')
|
51
|
+
end
|
52
|
+
|
53
|
+
test "locates scopes to :within option (css selector)" do
|
54
|
+
html = '<form></form><div><form id="foo"></form></div>'
|
55
|
+
element = Locator.locate(html, :form, :within => 'div')
|
56
|
+
assert_equal 'foo', element.attribute('id')
|
57
|
+
end
|
58
|
+
|
59
|
+
test "locates scopes to :within option (xpath)" do
|
60
|
+
html = '<form></form><div><form id="foo"></form></div>'
|
61
|
+
element = Locator.locate(html, :form, :within => '//div')
|
62
|
+
assert_equal 'foo', element.attribute('id')
|
63
|
+
end
|
64
|
+
|
65
|
+
test "within/locate with module included" do
|
66
|
+
html = '<form></form><div><form id="foo"></form></div>'
|
67
|
+
element = within(:div) { locate(html, :form) }
|
68
|
+
assert_equal 'foo', element.attribute('id')
|
69
|
+
end
|
70
|
+
|
71
|
+
test "locate when given a block scopes the block to the located element" do
|
72
|
+
html = '<form></form><div><form><p id="foo"><p></form></div>'
|
73
|
+
element = locate(html, :div) { locate(html, :form) { locate(html, :p) } }
|
74
|
+
assert_equal 'foo', element.attribute('id')
|
24
75
|
end
|
25
76
|
|
26
|
-
test "
|
27
|
-
html = '<form
|
28
|
-
|
29
|
-
assert_equal '<form></form>', element.to_s
|
77
|
+
test "locate does not yield the block when no element was found (would otherwise locate in global scope)" do
|
78
|
+
html = '<form></form><div><form><p id="foo"><p></form></div>'
|
79
|
+
assert_nil locate(html, :div) { locate(html, :form, :class => 'bar') { locate(html, :p) } }
|
30
80
|
end
|
31
81
|
end
|
data/test/test_helper.rb
CHANGED
@@ -29,18 +29,4 @@ end
|
|
29
29
|
|
30
30
|
class Test::Unit::TestCase
|
31
31
|
include TestMethod
|
32
|
-
|
33
|
-
def dom(html)
|
34
|
-
Locator::Dom.page(html)
|
35
|
-
end
|
36
|
-
|
37
|
-
def assert_locate(html, path, elements = nil)
|
38
|
-
expected = Array(elements || html)
|
39
|
-
result = dom(html).elements_by_xpath(path).map(&:to_s)
|
40
|
-
assert_equal expected, result
|
41
|
-
end
|
42
|
-
|
43
|
-
def assert_no_locate(html, path)
|
44
|
-
assert_locate(html, path, [])
|
45
|
-
end
|
46
32
|
end
|