locator 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/README.textile +10 -2
  2. data/TODO +4 -0
  3. data/lib/locator/boolean.rb +1 -1
  4. data/lib/locator/dom/nokogiri/element.rb +14 -5
  5. data/lib/locator/dom/nokogiri/page.rb +7 -3
  6. data/lib/locator/dom/nokogiri.rb +1 -1
  7. data/lib/locator/dom.rb +1 -1
  8. data/lib/locator/element/area.rb +2 -2
  9. data/lib/locator/element/button.rb +3 -3
  10. data/lib/locator/element/check_box.rb +9 -0
  11. data/lib/locator/element/elements_list.rb +6 -6
  12. data/lib/locator/element/field.rb +2 -3
  13. data/lib/locator/element/file.rb +9 -0
  14. data/lib/locator/element/form.rb +2 -2
  15. data/lib/locator/element/form_element.rb +9 -0
  16. data/lib/locator/element/hidden_field.rb +9 -0
  17. data/lib/locator/element/input.rb +11 -0
  18. data/lib/locator/element/label.rb +2 -2
  19. data/lib/locator/element/labeled_element.rb +8 -10
  20. data/lib/locator/element/link.rb +2 -2
  21. data/lib/locator/element/radio_button.rb +9 -0
  22. data/lib/locator/element/select.rb +3 -3
  23. data/lib/locator/element/select_option.rb +2 -2
  24. data/lib/locator/element/text_area.rb +3 -3
  25. data/lib/locator/element.rb +31 -24
  26. data/lib/locator/result.rb +46 -0
  27. data/lib/locator/version.rb +2 -2
  28. data/lib/locator/xpath.rb +33 -14
  29. data/lib/locator.rb +43 -21
  30. data/test/all.rb +1 -1
  31. data/test/locator/element/button_test.rb +10 -5
  32. data/test/locator/element/field_test.rb +24 -13
  33. data/test/locator/element/form_test.rb +12 -6
  34. data/test/locator/element/label_test.rb +10 -6
  35. data/test/locator/element/link_test.rb +16 -14
  36. data/test/locator/element/select_option_test.rb +14 -7
  37. data/test/locator/element/select_test.rb +10 -6
  38. data/test/locator/element/text_area_test.rb +10 -5
  39. data/test/locator/element_test.rb +79 -21
  40. data/test/locator/xpath_test.rb +0 -5
  41. data/test/locator_test.rb +63 -13
  42. data/test/test_helper.rb +0 -14
  43. 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
- assert_locate '<button></button>', Button.new.xpath
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
- assert_locate '<input type="submit">', Button.new.xpath
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
- assert_locate '<input type="button">', Button.new.xpath
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
- assert_locate '<input type="image">', Button.new.xpath
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
- assert_no_locate '<input type="checkbox">', Button.new.xpath
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
- assert_locate '<textarea></textarea>', Field.new.xpath
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
- assert_locate '<input type="text">', Field.new.xpath
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
- assert_locate '<input type="password">', Field.new.xpath
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
- assert_locate '<input type="email">', Field.new.xpath
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
- assert_locate '<input type="url">', Field.new.xpath
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
- assert_locate '<input type="search">', Field.new.xpath
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
- assert_locate '<input type="tel">', Field.new.xpath
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
- assert_locate '<input type="color">', Field.new.xpath
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
- assert_locate '<input type="text" name="foo">', Field.new.xpath('foo')
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
- assert_locate '<input type="text" class="foo">', Field.new.xpath(:class => 'foo')
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
- assert_locate html, Field.new.xpath('foo'), '<input type="text" id="bar">'
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
- assert_locate html, Field.new.xpath('foo', :class => 'baz'), '<input type="text" id="bar" class="baz">'
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
- assert_no_locate '<input type="checkbox">', Field.new.xpath
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
- assert_locate '<form></form>', Form.new.xpath
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
- assert_locate '<form id="foo"></form>', Form.new.xpath('foo')
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
- assert_locate '<form name="foo"></form>', Form.new.xpath('foo')
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
- assert_locate '<form class="foo"></form>', Form.new.xpath(:class => 'foo')
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
- assert_no_locate '<form id="bar"></form>', Form.new.xpath(:class => 'foo')
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
- assert_no_locate '<form class="bar"></form>', Form.new.xpath(:class => 'foo')
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
- assert_locate '<label></label>', Label.new.xpath
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
- assert_locate '<label id="foo"></label>', Label.new.xpath('foo')
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
- assert_locate '<label class="foo"></label>', Label.new.xpath(:class => 'foo')
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
- assert_no_locate '<label id="bar"></label>', Label.new.xpath(:class => 'foo')
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
- assert_no_locate '<label class="bar"></label>', Label.new.xpath(:class => 'foo')
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 ElementLinkTest < Test::Unit::TestCase
3
+ class LocatorElementLinkTest < Test::Unit::TestCase
5
4
  Link = Locator::Element::Link
6
5
 
7
6
  test "finds a link" do
8
- assert_locate '<a href=""></a>', Link.new.xpath
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
- assert_locate '<a href="" id="foo"></a>', Link.new.xpath('foo')
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
- assert_locate '<a href="" class="foo"></a>', Link.new.xpath(:class => 'foo')
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 an link by content" do
20
- assert_locate '<a href="">foo</a>', Link.new.xpath('foo')
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
- assert_no_locate '<a name=""></a>', Link.new.xpath
27
+ html =
28
+ assert_nil Link.new.locate('<a name="">foo</a>')
25
29
  end
26
30
 
27
- test "does not find a link when id does not match" do
28
- assert_no_locate '<a href="" id="bar"></a>', Link.new.xpath(:class => 'foo')
29
- end
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
- assert_locate '<option></option>', SelectOption.new.xpath
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
- assert_locate '<option id="foo"></option>', SelectOption.new.xpath('foo')
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
- assert_locate '<option value="foo"></option>', SelectOption.new.xpath('foo')
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
- assert_locate '<option>foo</option>', SelectOption.new.xpath('foo')
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
- assert_locate '<option class="foo"></option>', SelectOption.new.xpath(:class => 'foo')
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
- assert_no_locate '<option id="bar"></option>', SelectOption.new.xpath(:class => 'foo')
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
- assert_no_locate '<option class="bar"></option>', SelectOption.new.xpath(:class => 'foo')
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
- assert_locate '<select></select>', Select.new.xpath
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
- assert_locate '<select id="foo"></select>', Select.new.xpath('foo')
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
- assert_locate '<select class="foo"></select>', Select.new.xpath(:class => 'foo')
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
- assert_no_locate '<select id="bar"></select>', Select.new.xpath(:class => 'foo')
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
- assert_no_locate '<select class="bar"></select>', Select.new.xpath(:class => 'foo')
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
- assert_locate '<textarea></textarea>', TextArea.new.xpath
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
- assert_locate '<textarea id="foo"></textarea>', TextArea.new.xpath('foo')
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
- assert_locate '<textarea class="foo"></textarea>', TextArea.new.xpath(:class => 'foo')
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
- assert_no_locate '<textarea id="bar"></textarea>', TextArea.new.xpath(:class => 'foo')
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
- assert_no_locate '<textarea class="bar"></textarea>', TextArea.new.xpath(:class => 'foo')
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 ElementTest < Test::Unit::TestCase
5
- def xpath(*args)
6
- Locator::Element.new('foo', [:id, :content]).xpath(*args)
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 "no selector given => finds all element" do
10
- assert_locate '<foo></foo><foo></foo>', xpath, ['<foo></foo>', '<foo></foo>']
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 "given selector equals content => finds the element" do
14
- assert_locate '<foo>foo</foo>', xpath('foo')
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 "given selector contained in content => finds the element" do
18
- assert_locate '<foo>foo!</foo>', xpath('foo')
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 "given selector equals id => finds the element" do
22
- assert_locate '<foo id="foo"></foo>', xpath('foo')
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 "given selector contained in id => does not find the element" do
26
- assert_no_locate '<foo id="foobar"></foo>', xpath('foo')
79
+ test "locate selects an element containing extra text (1)" do
80
+ html = '<a href="#">the link &raquo;</label>'
81
+ assert Element.new.locate(html, 'the link')
27
82
  end
28
83
 
29
- test "given attribute equals attribute => finds the element" do
30
- assert_locate '<foo class="foo">a</foo>', xpath(:class => 'foo')
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 "given attribute contained in attribute => does not find the element" do
34
- assert_no_locate '<foo class="foo-bar">a</foo>', xpath(:class => 'foo')
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 "selector + attributes => finds the element" do
38
- assert_locate '<foo class="foo" title="bar">bar</foo>', xpath('bar', :class => 'foo', :title => 'bar')
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
@@ -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
- path = Locator.xpath(:form, 'foo', :class => 'bar')
11
- assert_equal './/form[@class="bar"][@id="foo" or @name="foo"]', path
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><h1></h1><form class="bar"></form></body></html>'
16
- element = Locator.new(html).locate(:form, :class => 'bar')
17
- assert_equal '<form class="bar"></form>', element.to_s
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 id="foo"></form><div id="bar"><form></form></div>'
22
- element = Locator.new(html).within(:xpath => '//div[@id="bar"]') { locate(:form) }
23
- assert_equal '<form></form>', element.to_s
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 "locates scopes to :within option" do
27
- html = '<form id="foo"></form><div id="bar"><form></form></div>'
28
- element = Locator.new(html).locate(:form, :within => '//div[@id="bar"]')
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