watir_robot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # Functionality related to HTML buttons (whether +<input>+ type submit or actual +<button>+ tags)
5
+ #
6
+ module Button
7
+
8
+ ### Actions ###
9
+
10
+ #
11
+ # Click a button
12
+ #
13
+ # @param [String] loc attribute/value pairs that match an HTML element
14
+ #
15
+ def click_button(loc)
16
+ @browser.button(parse_location(loc)).click
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,53 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # Functionality related to checkbox form elements
5
+ #
6
+ module CheckBox
7
+
8
+ ### Actions ###
9
+
10
+ #
11
+ # Check a checkbox
12
+ #
13
+ # @param [String] loc attribute/value pairs that match an HTML element
14
+ #
15
+ def select_checkbox(loc)
16
+ @browser.checkbox(parse_location(loc)).set
17
+ end
18
+
19
+ #
20
+ # Uncheck a checkbox
21
+ #
22
+ # @param [String] loc attribute/value pairs that match an HTML element
23
+ #
24
+ def unselect_checkbox(loc)
25
+ @browser.checkbox(parse_location(loc)).set(false)
26
+ end
27
+
28
+
29
+ ### Conditions ###
30
+
31
+ #
32
+ # Verify that checkbox is checked
33
+ #
34
+ # @param [String] loc attribute/value pairs that match an HTML element
35
+ #
36
+ def checkbox_should_be_selected(loc)
37
+ raise(Exception::CheckboxSelectionError, "The checkbox located at #{loc} is not checked off.") unless
38
+ @browser.checkbox(parse_location(loc)).set?
39
+ end
40
+
41
+ #
42
+ # Verify that checkbox is not checked
43
+ #
44
+ # @param [String] loc attribute/value pairs that match an HTML element
45
+ #
46
+ def checkbox_should_not_be_selected(loc)
47
+ raise(Exception::CheckboxSelectionError, "The checkbox located at #{loc} is checked off.") if
48
+ @browser.checkbox(parse_location(loc)).set?
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,95 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # Functionality related to HTML elements in a generic, global context
5
+ #
6
+ module Element
7
+
8
+ ### Actions ###
9
+
10
+ #
11
+ # Get the value of an attribute for an element
12
+ #
13
+ # @param [String] loc attribute/value pairs that match an HTML element
14
+ # @param [String] attr the attribute for which to return a value
15
+ # @return [String] the value of the attribute for the given HTML element
16
+ #
17
+ def get_element_attribute(loc, attr)
18
+ @browser.element(parse_location(loc)).attribute_value(attr)
19
+ end
20
+
21
+ #
22
+ # Get an element's text
23
+ #
24
+ # @param [String] loc attribute/value pairs that match an HTML element
25
+ # @return [String] the text of a given HTML *without* any HTML markup
26
+ #
27
+ def get_element_text(loc)
28
+ @browser.element(parse_location(loc)).text
29
+ end
30
+
31
+ #
32
+ # Click any HTML element
33
+ #
34
+ # @param [String] loc attribute/value pairs that match an HTML element
35
+ #
36
+ def click_element(loc)
37
+ @browser.element(parse_location(loc)).click
38
+ end
39
+
40
+ #
41
+ # Set focus to element
42
+ #
43
+ # @param [String] loc attribute/value pairs that match an HTML element
44
+ #
45
+ def focus(loc)
46
+ @browser.element(parse_location(loc)).focus
47
+ end
48
+
49
+ ### Conditions ###
50
+
51
+ #
52
+ # Verify that an element is visible
53
+ #
54
+ # @param [String] loc attribute/value pairs that match an HTML element
55
+ #
56
+ def element_should_be_visible(loc)
57
+ raise(Exception::ElementVisibilityError, "The element described by #{parse_location(loc)} is not visible") unless
58
+ @browser.element(parse_location(loc)).visible?
59
+ end
60
+
61
+ #
62
+ # Verify that an element is not visible
63
+ #
64
+ # @param [String] loc attribute/value pairs that match an HTML element
65
+ #
66
+ def element_should_not_be_visible(loc)
67
+ raise(Exception::ElementVisibilityError, "The element described by #{parse_location(loc)} is not visible") if
68
+ @browser.element(parse_location(loc)).visible?
69
+ end
70
+
71
+ #
72
+ # Verify that an element's text contains a specific value
73
+ #
74
+ # @param [String] loc attribute/value pairs that match an HTML element
75
+ # @param [String] text the text to compare against
76
+ #
77
+ def element_text_should_contain(loc, text)
78
+ raise(Exception::ElementMatchError, "The element's text #{@browser.element(parse_location(loc)).text} does not contain the text #{text}") unless
79
+ @browser.element(parse_location(loc)).text.include? text
80
+ end
81
+
82
+ #
83
+ # Verify that an element's text matches a specific value
84
+ # @param [String] loc attribute/value pairs that match an HTML element
85
+ # @param [String] text the text to compare against
86
+ #
87
+ def element_text_should_be(loc, text)
88
+ raise(Exception::ElementMatchError, "The element's text #{@browser.element(parse_location(loc)).text} does not equal #{text}") unless
89
+ @browser.element(parse_location(loc)).text == text
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+
@@ -0,0 +1,30 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # Functionality for HTML file-upload fields
5
+ #
6
+ module FileField
7
+
8
+ ### Actions ###
9
+
10
+ #
11
+ # Insert path of a file into a file-upload field
12
+ #
13
+ # @param [String] loc attribute/value pairs that match an HTML element
14
+ # @param [String] path the path to the file you wish to upload from your computer
15
+ #
16
+ def choose_file(loc, path)
17
+ @browser.file_field(parse_location(loc)).set path
18
+ end
19
+
20
+ #
21
+ # Get the file path present in a file-upload field
22
+ #
23
+ # @param [String] loc attribute/value pairs that match an HTML element
24
+ # @param [String] the path of the file in a file-upload field
25
+ #
26
+ def get_filefield_path(loc)
27
+ @browser.file_field(parse_location(loc)).value
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,143 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # Functionality related to HTML forms as a whole
5
+ #
6
+ # @todo Add keywords here
7
+ module Form
8
+
9
+ #
10
+ # Verify that a form contains a specific button
11
+ #
12
+ # @param [String] form_loc attribute/value pairs that match an HTML element
13
+ # @param [String] field_loc attribute/value pairs that match an HTML element
14
+ #
15
+ def form_should_contain_button(form_loc, field_loc)
16
+ raise(Exception::ElementMatchError, "The button described by #{field_loc} was not located in the form described by #{form_loc}") unless
17
+ @browser.form(parse_location(form_loc)).button(parse_location(field_loc)).exists?
18
+ end
19
+
20
+ #
21
+ # Verify that a form does not contain a specific button
22
+ #
23
+ # @param [String] form_loc attribute/value pairs that match an HTML element
24
+ # @param [String] field_loc attribute/value pairs that match an HTML element
25
+ #
26
+ def form_should_not_contain_button(form_loc, field_loc)
27
+ raise(Exception::ElementMatchError, "The button described by #{field_loc} was not located in the form described by #{form_loc}") if
28
+ @browser.form(parse_location(form_loc)).button(parse_location(field_loc)).exists?
29
+ end
30
+
31
+ #
32
+ # Verify that a form contains a specific checkbox
33
+ #
34
+ # @param [String] form_loc attribute/value pairs that match an HTML element
35
+ # @param [String] field_loc attribute/value pairs that match an HTML element
36
+ #
37
+ def form_should_contain_checkbox(form_loc, field_loc)
38
+ raise(Exception::ElementMatchError, "The checkbox described by #{field_loc} was not located in the form described by #{form_loc}") unless
39
+ @browser.form(parse_location(form_loc)).checkbox(parse_location(field_loc)).exists?
40
+ end
41
+
42
+ #
43
+ # Verify that a form does not contain a specific checkbox
44
+ #
45
+ # @param [String] form_loc attribute/value pairs that match an HTML element
46
+ # @param [String] field_loc attribute/value pairs that match an HTML element
47
+ #
48
+ def form_should_not_contain_checkbox(form_loc, field_loc)
49
+ raise(Exception::ElementMatchError, "The checkbox described by #{field_loc} was not located in the form described by #{form_loc}") if
50
+ @browser.form(parse_location(form_loc)).checkbox(parse_location(field_loc)).exists?
51
+ end
52
+
53
+ #
54
+ # Verify that a form contains a specific file-field
55
+ #
56
+ # @param [String] form_loc attribute/value pairs that match an HTML element
57
+ # @param [String] field_loc attribute/value pairs that match an HTML element
58
+ #
59
+ def form_should_contain_filefield(form_loc, field_loc)
60
+ raise(Exception::ElementMatchError, "The file-field described by #{field_loc} was not located in the form described by #{form_loc}") unless
61
+ @browser.form(parse_location(form_loc)).file_field(parse_location(field_loc)).exists?
62
+ end
63
+
64
+ #
65
+ # Verify that a form does not contain a specific file-field
66
+ #
67
+ # @param [String] form_loc attribute/value pairs that match an HTML element
68
+ # @param [String] field_loc attribute/value pairs that match an HTML element
69
+ #
70
+ def form_should_not_contain_filefield(form_loc, field_loc)
71
+ raise(Exception::ElementMatchError, "The file-field described by #{field_loc} was not located in the form described by #{form_loc}") if
72
+ @browser.form(parse_location(form_loc)).file_field(parse_location(field_loc)).exists?
73
+ end
74
+
75
+ #
76
+ # Verify that a form contains a specific radio button
77
+ #
78
+ # @param [String] form_loc attribute/value pairs that match an HTML element
79
+ # @param [String] field_loc attribute/value pairs that match an HTML element
80
+ #
81
+ def form_should_contain_radio_button(form_loc, field_loc)
82
+ raise(Exception::ElementMatchError, "The radio button described by #{field_loc} was not located in the form described by #{form_loc}") unless
83
+ @browser.form(parse_location(form_loc)).radio(parse_location(field_loc)).exists?
84
+ end
85
+
86
+ #
87
+ # Verify that a form does not contain a specific radio button
88
+ #
89
+ # @param [String] form_loc attribute/value pairs that match an HTML element
90
+ # @param [String] field_loc attribute/value pairs that match an HTML element
91
+ #
92
+ def form_should_not_contain_radio_button(form_loc, field_loc)
93
+ raise(Exception::ElementMatchError, "The radio button described by #{field_loc} was not located in the form described by #{form_loc}") if
94
+ @browser.form(parse_location(form_loc)).radio(parse_location(field_loc)).exists?
95
+ end
96
+
97
+ #
98
+ # Verify that a form contains a specific select list
99
+ #
100
+ # @param [String] form_loc attribute/value pairs that match an HTML element
101
+ # @param [String] field_loc attribute/value pairs that match an HTML element
102
+ #
103
+ def form_should_contain_select_list(form_loc, field_loc)
104
+ raise(Exception::ElementMatchError, "The select list described by #{field_loc} was not located in the form described by #{form_loc}") unless
105
+ @browser.form(parse_location(form_loc)).select(parse_location(field_loc)).exists?
106
+ end
107
+
108
+ #
109
+ # Verify that a form does not contain a specific select list
110
+ #
111
+ # @param [String] form_loc attribute/value pairs that match an HTML element
112
+ # @param [String] field_loc attribute/value pairs that match an HTML element
113
+ #
114
+ def form_should_not_contain_select_list(form_loc, field_loc)
115
+ raise(Exception::ElementMatchError, "The select list described by #{field_loc} was not located in the form described by #{form_loc}") if
116
+ @browser.form(parse_location(form_loc)).select(parse_location(field_loc)).exists?
117
+ end
118
+
119
+ #
120
+ # Verify that a form contains a specific text field
121
+ #
122
+ # @param [String] form_loc attribute/value pairs that match an HTML element
123
+ # @param [String] field_loc attribute/value pairs that match an HTML element
124
+ #
125
+ def form_should_contain_textfield(form_loc, field_loc)
126
+ raise(Exception::ElementMatchError, "The textfield described by #{field_loc} was not located in the form described by #{form_loc}") unless
127
+ @browser.form(parse_location(form_loc)).text_field(parse_location(field_loc)).exists?
128
+ end
129
+
130
+ #
131
+ # Verify that a form does not contain a specific text field
132
+ #
133
+ # @param [String] form_loc attribute/value pairs that match an HTML element
134
+ # @param [String] field_loc attribute/value pairs that match an HTML element
135
+ #
136
+ def form_should_not_contain_textfield(form_loc, field_loc)
137
+ raise(Exception::ElementMatchError, "The textfield described by #{field_loc} was not located in the form described by #{form_loc}") if
138
+ @browser.form(parse_location(form_loc)).text_field(parse_location(field_loc)).exists?
139
+ end
140
+
141
+ end
142
+
143
+ end
@@ -0,0 +1,18 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # Functionality related to images in an HTML document
5
+ module Image
6
+
7
+ #
8
+ # Click an image
9
+ #
10
+ # @param [String] loc attribute/value pairs that match an HTML element
11
+ #
12
+ def click_image(loc)
13
+ @browser.image(parse_location(loc)).click
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,21 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # Functionality related to links in an HTML document
5
+ #
6
+ module Link
7
+
8
+ ### Actions ###
9
+
10
+ #
11
+ # Click a link
12
+ #
13
+ # @param [String] loc attribute/value pairs that match an HTML element
14
+ #
15
+ def click_link(loc)
16
+ @browser.link(parse_location(loc)).click
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,42 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # Functionality related to ordered and unordered HTML lists
5
+ #
6
+ module List
7
+
8
+ ### Actions ###
9
+
10
+ #
11
+ # Get text from the items of a list
12
+ #
13
+ # The second argument can be "true" or "false"; true for ordered list, false (default) for unordered.
14
+ #
15
+ # @param [String] loc attribute/value pairs that match an HTML element
16
+ # @param [String] ordered true is ol, false is ul; strings for the booleans because the arguments come from Robot Framework
17
+ # @param [String] sep the separator that should be used to separate the list items
18
+ #
19
+ def get_list_items(loc, ordered = "false", sep = ";;")
20
+ ordered = ordered.downcase unless ordered.nil?
21
+ if ordered.nil?
22
+ # TODO: Warn user about this ambiguity
23
+ list = @browser.ul(parse_location(loc))
24
+ elsif ordered == 'true'
25
+ list = @browser.ol(parse_location(loc))
26
+ elsif ordered == 'false'
27
+ list = @browser.ul(parse_location(loc))
28
+ else
29
+ raise(ArgumentError, "If you specify ordered vs. unordered lists, the only valid values are 'true' or 'false' (case-insensitive)")
30
+ end
31
+
32
+ items = []
33
+ list.lis.each do |item|
34
+ items << item.text
35
+ end
36
+
37
+ items.join(sep)
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -0,0 +1,148 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # Native functionality for manipulating the mouse and keyboard via Java's java.awt.Robot class
5
+ #
6
+ module Native
7
+ require 'java'
8
+ java_import 'java.awt.Robot'
9
+ java_import 'java.awt.event.InputEvent'
10
+ java_import 'java.awt.event.KeyEvent'
11
+ java_import 'java.awt.Toolkit'
12
+ java_import 'java.awt.Dimension'
13
+ java_import 'java.awt.Rectangle'
14
+ java_import 'java.awt.image.BufferedImage'
15
+ java_import 'javax.imageio.ImageIO'
16
+
17
+ #
18
+ # Move mouse to specific coordinates
19
+ #
20
+ # @param [String] x x-axis coordinate (as string because arg comes from Robot Framework)
21
+ # @param [String] y y-axis coordinate (as string because arg comes from Robot Framework)
22
+ #
23
+ def move_mouse_to_position(x, y)
24
+ @robot ||= Robot.new
25
+ @robot.mouseMove(x.to_i, y.to_i)
26
+ end
27
+
28
+ #
29
+ # Left mouse press
30
+ #
31
+ def press_left_mouse_button
32
+ @robot ||= Robot.new
33
+ @robot.mousePress(InputEvent::BUTTON1_MASK)
34
+ end
35
+
36
+ #
37
+ # Right mouse press
38
+ #
39
+ def press_right_mouse_button
40
+ @robot ||= Robot.new
41
+ @robot.mousePress(InputEvent::BUTTON2_MASK)
42
+ end
43
+
44
+ #
45
+ # Left mouse release
46
+ #
47
+ def release_left_mouse_button
48
+ @robot ||= Robot.new
49
+ @robot.mouseRelease(InputEvent::BUTTON1_MASK)
50
+ end
51
+
52
+ #
53
+ # Right mouse release
54
+ #
55
+ def release_right_mouse_button
56
+ @robot ||= Robot.new
57
+ @robot.mouseRelease(InputEvent::BUTTON2_MASK)
58
+ end
59
+
60
+ #
61
+ # Left mouse click
62
+ #
63
+ def click_left_mouse_button
64
+ @robot ||= Robot.new
65
+ press_left_mouse_button
66
+ release_left_mouse_button
67
+ end
68
+
69
+ #
70
+ # Right mouse click
71
+ #
72
+ def click_right_mouse_button
73
+ @robot ||= Robot.new
74
+ press_right_mouse_button
75
+ release_right_mouse_button
76
+ end
77
+
78
+ #
79
+ # Press a key on a given HTML element
80
+ #
81
+ # @param [String] loc id, css or xpath query to identify an HTML element
82
+ # @param [String] the letter to be pressed, or an ASCII code in the format "ascii=65" (the letter 'a')
83
+ #
84
+ #def press_keys_at_location(loc, keys)
85
+ # @browser.element(parse_location(loc)).focus
86
+ #
87
+ # @robot ||= Robot.new
88
+ # key_codes = parse_key_code(key)
89
+ # @robot.keyPress(key_code)
90
+ # @robot.keyRelease(key_code)
91
+ #end
92
+
93
+ #
94
+ # Press a key (literally press and release).
95
+ #
96
+ # If you want to press a key on a given HTML element, you should use the
97
+ # Press Key keyword, or use the Focus keyword before Press Key Native.
98
+ #
99
+ # @param [String] the letter to be pressed, or an ASCII code in the format "ascii=65" (the letter 'a')
100
+ #
101
+ #def press_keys(keys)
102
+ # @robot ||= Robot.new
103
+ # key_code = parse_key_code(key)
104
+ # @robot.keyPress(key_code)
105
+ # @robot.keyRelease(key_code)
106
+ #end
107
+
108
+ #
109
+ # Drag and drop with the mouse
110
+ #
111
+ # This is at the operating system level. Consider using WebDriver's
112
+ # facilities for drag and drop if you need drag-and-drop within a web page.
113
+ #
114
+ # @param [String] start_x x-axis coordinate of initial mouse press (as string because arg comes from Robot Framework)
115
+ # @param [String] start_y y-axis coordinate of iniital mouse press (as string because arg comes from Robot Framework)
116
+ # @param [String] finish_x x-axis coordinate of final mouse release (as string because arg comes from Robot Framework)
117
+ # @param [String] finish_y y-axis coordinate of final mouse release (as string because arg comes from Robot Framework)
118
+ #
119
+ def drag_and_drop(start_x, start_y, finish_x, finish_y)
120
+ @robot ||= Robot.new
121
+ move_mouse_to_position(start_x.to_i, start_y.to_i)
122
+ press_left_mouse_button
123
+ move_mouse_to_position(finish_x.to_i, finish_y.to_i)
124
+ release_left_mouse_button
125
+ end
126
+
127
+ #
128
+ # Take a screenshot of the entire screen
129
+ #
130
+ # @param [String] path the path (including file name) to which to save the screenshot image
131
+ #
132
+ def capture_screenshot(path = nil)
133
+ t = Toolkit.getDefaultToolkit
134
+ s = t.getScreenSize
135
+ rect = Rectangle.new(0, 0, s.width, s.height);
136
+
137
+ @robot ||= Robot.new
138
+ image = @robot.createScreenCapture(rect)
139
+ if path.nil?
140
+ path = Time.now.to_i.to_s + '_screenshot.png'
141
+ end
142
+ file = java.io.File.new(path)
143
+ ImageIO.write(image, 'png', file)
144
+ end
145
+
146
+ end
147
+
148
+ end