wrapybara 0.1.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.
@@ -0,0 +1,21 @@
1
+ module Wrapybara
2
+ class Label
3
+ include Element
4
+
5
+ def initialize(identifier, scope = default_scope, how = default_how)
6
+ @identifier = identifier
7
+ @how = how
8
+ @scope = scope
9
+ xpath = "//label[text()='#{identifier}' or @id='#{identifier}' or @for='#{identifier}']"
10
+ @element = get_element(xpath, scope)
11
+ end
12
+
13
+ def should_exist
14
+ super "Expected a label #{self.element_identifier} to exist"
15
+ end
16
+
17
+ def should_not_exist
18
+ super "Did not expect a label #{self.element_identifier}' to exist"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ module Wrapybara
2
+ class Link
3
+ include Element
4
+
5
+ attr_reader :element, :identifier, :how, :scope
6
+
7
+ def initialize(identifier, scope = default_scope, how = default_how)
8
+ @identifier = identifier
9
+ @how = how
10
+ @scope = scope
11
+ xpath = "//a[@href='#{identifier}' or @id='#{identifier}' or @title='#{identifier}' or contains(normalize-space(), '#{identifier}')]"
12
+ @element = get_element(xpath, scope)
13
+ end
14
+
15
+ def should_exist
16
+ super "Expected a link #{self.element_identifier} to exist"
17
+ end
18
+
19
+ def should_not_exist
20
+ super "Did not expect a link #{self.element_identifier}' to exist"
21
+ end
22
+
23
+ def click
24
+ self.should_exist
25
+ # Capybara method
26
+ @element.click
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,48 @@
1
+ module Wrapybara
2
+ class Option
3
+ include Element
4
+
5
+ attr_accessor :parent, :element
6
+
7
+ def initialize(select, option)
8
+ @option = option
9
+ @parent = select
10
+ xpath = option.blank? ? ".//option[normalize-space()='']" : XPath::HTML.option(option)
11
+ @element = select.element.find(xpath) rescue nil
12
+ end
13
+
14
+ def should_exist
15
+ super "Expected select #{self.parent_identifier} to have option '#{@option}'"
16
+ end
17
+
18
+ def should_not_exist
19
+ super "Did not expect select #{self.parent_identifier} to have option '#{@option}'"
20
+ end
21
+
22
+ def select
23
+ self.should_exist
24
+ # Capybara method
25
+ @element.select_option
26
+ end
27
+
28
+ def deselect
29
+ self.should_exist
30
+ # Capybara method
31
+ @element.unselect_option
32
+ end
33
+
34
+ def selected?
35
+ self.should_exist
36
+ # Capybara method
37
+ @element.selected? || @parent.element.value == @element.value
38
+ end
39
+
40
+ def should_be_selected
41
+ raise UnmetExpectation, "Expected select #{self.parent_identifier} to have option '#{@option}' selected" unless self.selected?
42
+ end
43
+
44
+ def should_not_be_selected
45
+ raise UnmetExpectation, "Did not expect select #{self.parent_identifier} to have option '#{@option}' selected" if self.selected?
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,22 @@
1
+ module Wrapybara
2
+ class PasswordField
3
+ include Element
4
+ include FillableField
5
+
6
+ def initialize(identifier, scope = default_scope, how = default_how)
7
+ @identifier = identifier
8
+ @how = how
9
+ @scope = scope
10
+ xpath = XPath::HTML.send(:locate_field, XPath.descendant(:input)[XPath.attr(:type).equals('password')], identifier)
11
+ @element = get_element(xpath, scope)
12
+ end
13
+
14
+ def should_exist
15
+ super "Expected a password field #{self.element_identifier} to exist"
16
+ end
17
+
18
+ def should_not_exist
19
+ super "Did not expect a password field #{self.element_identifier}' to exist"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,49 @@
1
+ module Wrapybara
2
+ class RadioButton
3
+ include Element
4
+
5
+ def initialize(identifier, scope = default_scope, how = default_how)
6
+ @identifier = identifier
7
+ @how = how
8
+ @scope = scope
9
+ xpath = XPath::HTML.radio_button(identifier)
10
+ @element = get_element(xpath, scope)
11
+ end
12
+
13
+ def should_exist
14
+ super "Expected a radio button #{self.element_identifier} to exist"
15
+ end
16
+
17
+ def should_not_exist
18
+ super "Did not expect a radio button #{self.element_identifier}' to exist"
19
+ end
20
+
21
+ def set(state)
22
+ self.should_exist
23
+ # Capybara method
24
+ @element.set(state)
25
+ end
26
+
27
+ def check
28
+ self.set(true)
29
+ end
30
+
31
+ def uncheck
32
+ self.set(false)
33
+ end
34
+
35
+ def checked?
36
+ self.should_exist
37
+ # Capybara method
38
+ @element.checked?
39
+ end
40
+
41
+ def should_be_checked
42
+ raise UnmetExpectation, "Expected a radio button #{self.element_identifier} to be checked" unless self.checked?
43
+ end
44
+
45
+ def should_not_be_checked
46
+ raise UnmetExpectation, "Did not expect a radio button #{self.element_identifier} to be checked" if self.checked?
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,67 @@
1
+ module Wrapybara
2
+ class Select
3
+ include Element
4
+
5
+ attr_reader :element, :identifier, :how, :scope
6
+
7
+ def initialize(identifier, scope = default_scope, how = default_how)
8
+ @identifier = identifier
9
+ @how = how
10
+ @scope = scope
11
+ @element = get_element(XPath::HTML.select(identifier), scope)
12
+ end
13
+
14
+ def should_exist
15
+ super "Expected a select #{self.element_identifier} to exist"
16
+ end
17
+
18
+ def should_not_exist
19
+ super "Did not expect a select #{self.element_identifier}' to exist"
20
+ end
21
+
22
+ def option(option)
23
+ Option.new(self, option)
24
+ end
25
+
26
+ def has_option?(option)
27
+ self.should_exist
28
+ self.option(option).exists?
29
+ end
30
+
31
+ def should_have_options(options)
32
+ self.should_exist
33
+ options.each do |option|
34
+ self.option(option).should_exist
35
+ end
36
+ end
37
+
38
+ def should_not_have_options(options)
39
+ self.should_exist
40
+ options.each do |option|
41
+ self.option(option).should_not_exist
42
+ end
43
+ end
44
+
45
+ def select(option)
46
+ self.should_have_options([option])
47
+ self.option(option).select
48
+ end
49
+
50
+ alias_method :choose, :select
51
+
52
+ def deselect(option)
53
+ self.should_have_options([option])
54
+ self.option(option).deselect
55
+ end
56
+
57
+ def should_be_selected(option)
58
+ self.should_have_options([option])
59
+ self.option(option).should_be_selected
60
+ end
61
+
62
+ def should_not_be_selected(option)
63
+ self.should_have_options([option])
64
+ self.option(option).should_not_be_selected
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,51 @@
1
+ module Wrapybara
2
+ class Table
3
+ include Element
4
+
5
+ attr_reader :element, :identifier, :how, :scope, :head, :body
6
+
7
+ def initialize(identifier, scope = default_scope, how = default_how)
8
+ @identifier = identifier
9
+ @how = how
10
+ @scope = scope
11
+ xpath = XPath::HTML.table(identifier)
12
+ @element = get_element(xpath, scope)
13
+ @head = TableHead.new(self)
14
+ @body = TableBody.new(self)
15
+ end
16
+
17
+ def should_exist
18
+ super "Expected a table #{self.element_identifier}' to exist"
19
+ end
20
+
21
+ def should_not_exist
22
+ super "Did not expect a table #{self.element_identifier} to exist"
23
+ end
24
+
25
+ def should_have_columns(columns)
26
+ self.head.should_exist
27
+ columns.each do |column|
28
+ raise UnmetExpectation, "Expected table #{self.element_identifier} to have column '#{column}'" unless self.has_column?(column)
29
+ end
30
+ end
31
+
32
+ def should_not_have_columns(columns)
33
+ self.head.should_exist
34
+ columns.each do |column|
35
+ raise UnmetExpectation, "Did not expect table #{self.element_identifier} to have column '#{column}'" if self.has_column?(column)
36
+ end
37
+ end
38
+
39
+ def click_column(column)
40
+ self.should_exist
41
+ self.should_have_columns([column])
42
+ cell = self.head.cell(column)
43
+ link = cell.element.find('a[1]')
44
+ link.click
45
+ end
46
+
47
+ def has_column?(label)
48
+ @head.has_column?(label)
49
+ end
50
+ end # class Table
51
+ end # module Wrapybara
@@ -0,0 +1,40 @@
1
+ module Wrapybara
2
+ class TableBody
3
+ include Element
4
+
5
+ attr_reader :parent, :element
6
+
7
+ def initialize(table)
8
+ @parent = table
9
+ @element = table.element.find('tbody') rescue nil
10
+ end
11
+
12
+ def should_exist
13
+ super "Expected table #{@parent.how} '#{self.parent_identifier} to have a body"
14
+ end
15
+
16
+ def should_not_exist
17
+ super "Did not expect table #{@parent.how} '#{self.parent_identifier} to have a body"
18
+ end
19
+
20
+ def cell(*args)
21
+ TableCell.new(self, args)
22
+ end
23
+
24
+ def should_have_cells(labels)
25
+ labels.each do |label|
26
+ self.cell(label).should_exist
27
+ end
28
+ end
29
+
30
+ def should_not_have_cells(labels)
31
+ labels.each do |label|
32
+ self.cell(label).should_not_exist
33
+ end
34
+ end
35
+
36
+ def exists?
37
+ !@element.nil?
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,87 @@
1
+ module Wrapybara
2
+ class TableCell
3
+ include Element
4
+
5
+ attr_reader :parent, :element
6
+
7
+ def initialize(parent, *args)
8
+ args = args.first if args.first.is_a?(Array)
9
+
10
+ @parent = parent
11
+ t_what = parent.is_a?(TableHead) ? 'th' : 'td'
12
+ xpath =
13
+ if args.first.to_i == 0
14
+ @content = args.first
15
+ "*/#{t_what}[contains(normalize-space(), '#{@content}')]"
16
+ else
17
+ @row = args.first.to_i
18
+ @column = args.last.to_i
19
+ "tr[#{@row}]/#{t_what}[#{@column}]"
20
+ end
21
+
22
+ @element = parent.element.find(xpath) rescue nil
23
+ end
24
+
25
+ def should_exist
26
+ super "Expected #{self.exists_message}"
27
+ end
28
+
29
+ def should_not_exist
30
+ super "Did not expect #{self.exists_message}"
31
+ end
32
+
33
+ def should_have_content(content)
34
+ self.should_exist
35
+ raise UnmetExpectation, "Expected #{self.has_content_message(content)}" unless self.has_content?(content)
36
+ end
37
+
38
+ def should_not_content(content)
39
+ self.should_exist
40
+ raise UnmetExpectation, "Did not expect #{self.has_content_message(content)}" if self.has_content?(content)
41
+ end
42
+
43
+ def should_contain(element, identifier, how)
44
+ self.should_exist
45
+ raise UnmetExpectation, "Expected #{self.contains_message(element, identifier, how)}" unless self.contains?(element, identifier, how)
46
+ end
47
+
48
+ def should_not_contain(element, identifier, how)
49
+ self.should_exist
50
+ raise UnmetExpectation, "Did not expect #{self.contains_message(element, identifier, how)}" if self.contains?(element, identifier, how)
51
+ end
52
+
53
+ def contains?(element, identifier, how)
54
+ child = "Wrapybara::#{element.camelize}".constantize.new(identifier, self.element.path, how)
55
+ child.exists?
56
+ end
57
+
58
+ def exists?
59
+ !@element.nil?
60
+ end
61
+
62
+ def has_content?(content)
63
+ @element.text =~ /#{content}/
64
+ end
65
+
66
+ def exists_message
67
+ parent = @parent.is_a?(TableHead) ? 'head' : 'body'
68
+ message = "#{parent} of table #{@parent.parent.how} '#{@parent.parent.identifier}'#{within(@parent.parent.scope)} to have a cell "
69
+ message += @content ? "with content '#{@content}'" : "at row #{@row} column #{@column}"
70
+ message
71
+ end
72
+
73
+ def has_content_message(content)
74
+ parent = @parent.is_a?(TableHead) ? 'head' : 'body'
75
+ message = "#{parent} cell at row #{@row} column #{@column} of table #{@parent.parent.how} '#{@parent.parent.identifier}'#{within(@parent.parent.scope)}"
76
+ message += " to have content '#{content}'"
77
+ message
78
+ end
79
+
80
+ def contains_message(element, identifier, how)
81
+ parent = @parent.is_a?(TableHead) ? 'head' : 'body'
82
+ message = "#{parent} cell at row #{@row} column #{@column} of table #{@parent.parent.how} '#{@parent.parent.identifier}'#{within(@parent.parent.scope)}"
83
+ message += " to contain a[n] #{element} #{how} '#{identifier}'"
84
+ message
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,40 @@
1
+ module Wrapybara
2
+ class TableHead
3
+ include Element
4
+
5
+ attr_reader :parent, :element
6
+
7
+ def initialize(table)
8
+ @parent = table
9
+ @element = table.element.find('thead') rescue nil
10
+ end
11
+
12
+ def should_exist
13
+ super "Expected table #{self.parent_identifier} to have a head"
14
+ end
15
+
16
+ def should_not_exist
17
+ super "Did not expect table #{self.parent_identifier} to have a head"
18
+ end
19
+
20
+ def cell(*args)
21
+ TableCell.new(self, args)
22
+ end
23
+
24
+ def has_column?(label)
25
+ self.cell(label).exists?
26
+ end
27
+
28
+ def should_have_cells(labels)
29
+ labels.each do |label|
30
+ self.cell(label).should_exist
31
+ end
32
+ end
33
+
34
+ def should_not_have_cells(labels)
35
+ labels.each do |label|
36
+ self.cell(label).should_not_exist
37
+ end
38
+ end
39
+ end
40
+ end