watir_robot 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,100 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # Functionality related to HTMl tables, including headers, bodies, footers, and cells
5
+ #
6
+ module Table
7
+
8
+ ### Actions ###
9
+
10
+ #
11
+ # Get contents of a single table cell
12
+ #
13
+ # @param [String] loc attribute/value pairs that match an HTML element
14
+ # @param [String] row with the top-most row as 1, the number of the row in question
15
+ # @param [String] col with the left-most row as 1, the number of the column in question
16
+ # @return [String] the text contained within the targeted table cell
17
+ #
18
+ def get_table_cell(loc, row, col)
19
+ row = row.to_i - 1
20
+ col = col.to_i - 1
21
+ @browser.table(parse_location(loc))[row][col].text
22
+ end
23
+
24
+
25
+ ### Conditions ###
26
+
27
+ #
28
+ # Verify that table cell contains certain contents
29
+ #
30
+ # @param [String] loc attribute/value pairs that match an HTML element
31
+ # @param [String] row with the top-most row as 1, the number of the row in question
32
+ # @param [String] col with the left-most row as 1, the number of the column in question
33
+ # @param [String] text the text to compare against
34
+ #
35
+ def table_cell_should_contain(loc, row, col, text)
36
+ row = row.to_i - 1
37
+ col = col.to_i - 1
38
+ raise(ElementMatchError, "The table cell at row #{row} and column #{col}, located in the table at #{loc}, does not contain the text #{text}") unless
39
+ @browser.table(parse_location(loc))[row][col].text.include? text
40
+ end
41
+
42
+ #
43
+ # Verify that a table column contains certain contents
44
+ #
45
+ # @param [String] loc attribute/value pairs that match an HTML element
46
+ # @param [String] col with the left-most row as 1, the number of the column in question
47
+ # @param [String] text the text to compare against
48
+ #
49
+ def table_column_should_contain(loc, col, text)
50
+ # There's no "nice" way to do this, so we run through each row and snag
51
+ # the cell at offset y
52
+ col = col.to_i - 1
53
+ content = ''
54
+ @browser.table(parse_location(loc)).rows.each do |row|
55
+ content << ' '
56
+ content << row[col].text
57
+ end
58
+
59
+ raise(Exception::ElementMatchError, "The table column number #{c} in the table located at #{loc} does not contain the text #{text}") unless
60
+ content.include? text
61
+ end
62
+
63
+ #
64
+ # Verify that a table's header contains certain contents
65
+ #
66
+ # @param [String] loc attribute/value pairs that match an HTML element
67
+ # @param [String] text the text to compare against
68
+ #
69
+ def table_header_should_contain(loc, text)
70
+ raise(Exception::ElementMatchError, "The table header of the table located at #{loc} does not contain the text #{text}") unless
71
+ @browser.table(parse_location(loc)).thead.text.include? text
72
+ end
73
+
74
+ #
75
+ # Verify that a table row conatins certain contents
76
+ #
77
+ # @param [String] loc attribute/value pairs that match an HTML element
78
+ # @param [String] row with the top-most row as 1, the number of the row in question
79
+ # @param [String] text the text to compare against
80
+ #
81
+ def table_row_should_contain(loc, row, text)
82
+ row = row.to_i - 1
83
+ raise(Exception::ElementMatchError, "The table row number #{row} in the table located at #{loc} does not contain the text #{text}") unless
84
+ @browser.table(parse_location(loc))[row].text.include? text
85
+ end
86
+
87
+ #
88
+ # Verify that certain content is contained somewhere within an entire HTML table
89
+ #
90
+ # @param [String] loc attribute/value pairs that match an HTML element
91
+ # @param [String] text the text to compare against
92
+ #
93
+ def table_should_contain(loc, text)
94
+ raise(Exception::ElementMatchError, "The table located at #{loc} does not contain the text #{text}") unless
95
+ @browser.table(parse_location(loc)).text.include? text
96
+ end
97
+
98
+ end
99
+
100
+ end
@@ -0,0 +1,69 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # Functionality related to text fields (+<input>+ of type text and password)
5
+ #
6
+ module TextField
7
+
8
+ ### Actions ###
9
+
10
+ #
11
+ # Focus cursor inside a textfield
12
+ #
13
+ # @param [String] loc attribute/value pairs that match an HTML element
14
+ #
15
+ def focus_textfield(loc)
16
+ @browser.text_field(parse_location(loc)).focus
17
+ end
18
+
19
+ #
20
+ # Input text into a text field
21
+ #
22
+ # @param [String] loc attribute/value pairs that match an HTML element
23
+ # @param [String] text the text to type into the field
24
+ #
25
+ def input_text(loc, text)
26
+ @browser.text_field(parse_location(loc)).set text
27
+ end
28
+
29
+ #
30
+ # Input a password into a password field.
31
+ #
32
+ # @note This value will not be logged.
33
+ #
34
+ # @param [String] loc attribute/value pairs that match an HTML element
35
+ # @param [String] password the password to type into the field
36
+ #
37
+ def input_password(loc, password)
38
+ # This will differ from input_text when logging is implemented
39
+ @browser.text_field(parse_location(loc)).set password
40
+ end
41
+
42
+
43
+ ### Conditions ###
44
+
45
+ #
46
+ # Verify that textfield's text contains the value
47
+ #
48
+ # @param [String] loc attribute/value pairs that match an HTML element
49
+ # @param [String] text the text to type into the field
50
+ #
51
+ def textfield_should_contain(loc, text)
52
+ raise(Exception::ElementMatchError, "The textfield located at #{loc} does not contain the text #{text}") unless
53
+ @browser.text_field(parse_location(loc)).value.include? text
54
+ end
55
+
56
+ #
57
+ # Verify that textfield's text is the value
58
+ #
59
+ # @param [String] loc attribute/value pairs that match an HTML element
60
+ # @param [String] text the text to compare against
61
+ #
62
+ def textfield_should_be(loc, text)
63
+ raise(Exception::ElementMatchError, "The textfield located at #{loc} does not have text equal to #{text}") unless
64
+ @browser.text_field(parse_location(loc)).value == text
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,170 @@
1
+ module WatirRobot
2
+
3
+ #
4
+ # This module contains the functions necessary to convert arguments obtained
5
+ # from Robot Framework tests and make them meaningful to the underlying
6
+ # Watir-WebDriver code.
7
+ #
8
+ module Parser
9
+
10
+ #
11
+ # Mapping of Unicode values to java.awt.event.KeyEvent constants
12
+ #
13
+ KEY_CONSTANTS = {
14
+ 32 => ['VK_SPACE'],
15
+ 33 => ['VK_EXCLAMATION_MARK'],
16
+ 34 => ['VK_SHIFT', 'VK_QUOTE'],
17
+ 35 => ['VK_SHIFT', 'VK_3'],
18
+ 36 => ['VK_SHIFT', 'VK_4'],
19
+ 38 => ['VK_AMPERSAND'],
20
+ 39 => ['VK_QUOTE'],
21
+ 40 => ['VK_SHIFT', 'VK_9'],
22
+ 41 => ['VK_SHIFT', 'VK_0'],
23
+ 42 => ['VK_ASTERISK'],
24
+ 43 => ['VK_ADD'],
25
+ 45 => ['VK_SUBTRACT'],
26
+ 46 => ['VK_DECIMAL'],
27
+ 47 => ['VK_SLASH'],
28
+ 48 => ['VK_0'],
29
+ 49 => ['VK_1'],
30
+ 50 => ['VK_2'],
31
+ 51 => ['VK_3'],
32
+ 52 => ['VK_4'],
33
+ 53 => ['VK_5'],
34
+ 54 => ['VK_6'],
35
+ 55 => ['VK_7'],
36
+ 56 => ['VK_8'],
37
+ 57 => ['VK_9'],
38
+ 58 => ['VK_SHIFT', 'VK_SEMICOLON'],
39
+ 59 => ['VK_SEMICOLON'],
40
+ 60 => ['VK_SHIFT', 'VK_COMMA'],
41
+ 61 => ['VK_EQUALS'],
42
+ 62 => ['VK_SHIFT', 'VK_DECIMAL'],
43
+ 64 => ['VK_AT'],
44
+ 65 => ['VK_SHIFT', 'VK_A'],
45
+ 66 => ['VK_SHIFT', 'VK_B'],
46
+ 67 => ['VK_SHIFT', 'VK_C'],
47
+ 68 => ['VK_SHIFT', 'VK_D'],
48
+ 69 => ['VK_SHIFT', 'VK_E'],
49
+ 70 => ['VK_SHIFT', 'VK_F'],
50
+ 71 => ['VK_SHIFT', 'VK_G'],
51
+ 72 => ['VK_SHIFT', 'VK_H'],
52
+ 73 => ['VK_SHIFT', 'VK_I'],
53
+ 74 => ['VK_SHIFT', 'VK_J'],
54
+ 75 => ['VK_SHIFT', 'VK_K'],
55
+ 76 => ['VK_SHIFT', 'VK_L'],
56
+ 77 => ['VK_SHIFT', 'VK_M'],
57
+ 78 => ['VK_SHIFT', 'VK_N'],
58
+ 79 => ['VK_SHIFT', 'VK_O'],
59
+ 80 => ['VK_SHIFT', 'VK_P'],
60
+ 81 => ['VK_SHIFT', 'VK_Q'],
61
+ 82 => ['VK_SHIFT', 'VK_R'],
62
+ 83 => ['VK_SHIFT', 'VK_S'],
63
+ 84 => ['VK_SHIFT', 'VK_T'],
64
+ 85 => ['VK_SHIFT', 'VK_U'],
65
+ 86 => ['VK_SHIFT', 'VK_V'],
66
+ 87 => ['VK_SHIFT', 'VK_W'],
67
+ 88 => ['VK_SHIFT', 'VK_X'],
68
+ 89 => ['VK_SHIFT', 'VK_Y'],
69
+ 90 => ['VK_SHIFT', 'VK_Z'],
70
+ 91 => ['VK_OPEN_BRACKET'],
71
+ 92 => ['VK_BACK_SLASH'],
72
+ 93 => ['VK_CLOSE_BRACKET'],
73
+ 94 => ['VK_SHIFT', 'VK_6'],
74
+ 95 => ['VK_SHIFT', 'VK_SUBTRACT'],
75
+ 96 => ['VK_BACK_QUOTE'],
76
+ 97 => ['VK_A'],
77
+ 98 => ['VK_B'],
78
+ 99 => ['VK_C'],
79
+ 100 => ['VK_D'],
80
+ 101 => ['VK_E'],
81
+ 102 => ['VK_F'],
82
+ 103 => ['VK_G'],
83
+ 104 => ['VK_H'],
84
+ 105 => ['VK_I'],
85
+ 106 => ['VK_J'],
86
+ 107 => ['VK_K'],
87
+ 108 => ['VK_L'],
88
+ 109 => ['VK_M'],
89
+ 110 => ['VK_N'],
90
+ 111 => ['VK_O'],
91
+ 112 => ['VK_P'],
92
+ 113 => ['VK_Q'],
93
+ 114 => ['VK_R'],
94
+ 115 => ['VK_S'],
95
+ 116 => ['VK_T'],
96
+ 117 => ['VK_U'],
97
+ 118 => ['VK_V'],
98
+ 119 => ['VK_W'],
99
+ 120 => ['VK_X'],
100
+ 121 => ['VK_Y'],
101
+ 122 => ['VK_Z'],
102
+ 123 => ['VK_SHIFT', 'VK_OPEN_BRACKET'],
103
+ 125 => ['VK_SHIFT', 'VK_CLOSE_BRACKET'],
104
+ }
105
+
106
+ #
107
+ # Parses the "location" string provided as argument for locating HTML elements.
108
+ #
109
+ # Watir-WebDriver expects a hash of parameters by which to search for HTML
110
+ # elements. This function take arguments from Robot Framework tests in the
111
+ # form of <tt>attribute=value</tt> and constructs a hash which
112
+ # Watir-WebDriver can use to uniquely identify HTML elements on the page.
113
+ #
114
+ # @param [String] loc argument from Robot Framework to locate an HTML element
115
+ # @return [Hash] a hash which Watir-WebDriver can use to locate an HTML element
116
+ #
117
+ def parse_location(loc)
118
+ loc = self.trim_sides(loc)
119
+
120
+ if loc[0..3].downcase == 'css='
121
+ return {:css => loc[4..loc.length]}
122
+ elsif loc[0..5].downcase == 'xpath='
123
+ return {:xpath => loc[6..loc.length]}
124
+ else
125
+ # Comma-separated attributes
126
+ attr_list = loc.split(',')
127
+ attrs = {}
128
+ attr_list.each do |a|
129
+ attr_kv = a.split('=')
130
+ attrs[self.trim_sides(attr_kv[0])] = self.trim_sides(attr_kv[1])
131
+ end
132
+ # Watir expects symbols for keys
133
+ attrs = Hash[attrs.map { |k, v| [k.to_sym, v] }]
134
+ if attrs.key? :id
135
+ attrs.delete_if {|k, v| k != :id}
136
+ end
137
+ return attrs
138
+ end
139
+ end
140
+
141
+ #
142
+ # Parses a key-code argument, provided as an actual character or in ASCII code
143
+ #
144
+ # @param [String] key the key for which to find the java.awt.KeyEvent key-code constant
145
+ # @return [String] the constant for the key event
146
+ #
147
+ def parse_key_code(key)
148
+ if key[0..5].downcase = 'ascii='
149
+ # return the ASCII code directly
150
+ return key[6..key.length]
151
+ else
152
+ # we assume it's a character and try to map it to a constant
153
+ end
154
+ end
155
+
156
+ #
157
+ # Small utility for trimming whitespace from both sides of a string
158
+ #
159
+ # @param [String] s string to trim both sides of
160
+ # @return [Strgin] the same string without whitespace on the left or right sides
161
+ #
162
+ def trim_sides(s)
163
+ s = s.lstrip
164
+ s = s.rstrip
165
+ return s
166
+ end
167
+
168
+ end
169
+
170
+ end
@@ -0,0 +1,14 @@
1
+ *** Settings ***
2
+ Library Remote http://localhost:8270
3
+
4
+ *** Variables ***
5
+ ${BROWSER} firefox
6
+ ${URL} http://bit.ly/watir-example
7
+
8
+ *** Keywords ***
9
+ Open and Maximize
10
+ [Arguments] ${url} ${browser}=firefox
11
+ [Documentation] Open a browser to a specific URL and then maximize it to full-screen.
12
+ Start Browser ${URL} ${BROWSER}
13
+ Maximize Browser Window
14
+
@@ -0,0 +1,171 @@
1
+ *** Settings ***
2
+ Suite Setup Open and Maximize ${URL} ${BROWSER}
3
+ Suite Teardown Close Browser
4
+ Library Remote http://localhost:8270
5
+ Resource resource.txt
6
+
7
+ *** Test Cases ***
8
+ Test Browser
9
+ [Setup] Go To ${URL}
10
+ Url Should Contain google.com
11
+ ${current_url}= Get Url
12
+ Go To http://psd.tutsplus.com
13
+ Url Should Be http://psd.tutsplus.com/
14
+ Go Back
15
+ Go Forward
16
+ Refresh
17
+ Click Image alt=Photoshop to HTML
18
+ Switch To Next Window
19
+ Title Should Be Photoshop to HTML | Rockable Press
20
+ Switch To Previous Window
21
+ Title Should Contain Psdtuts+
22
+ Switch to Window url=http://rockablepress.com/books/photoshop-to-html/
23
+ Url Should Contain rockablepress.com
24
+ Switch to Window url=http://psd.tutsplus.com/
25
+ Close Window 2
26
+ ${windows}= Get Window Count
27
+ ${current_url}= Get Url
28
+ Click Link href=http://rockablepress.com/books/photoshop-to-html/
29
+ ${windows}= Get Window Count
30
+ Switch to Next Window
31
+ Title Should Be Photoshop to HTML | Rockable Press
32
+ ${windows}= Get Window Count
33
+ Close Window url=http://rockablepress.com/books/photoshop-to-html/
34
+ Title Should Contain Psdtuts+
35
+
36
+ Test Button
37
+ [Setup] Go To ${URL}
38
+ Click Button value=Submit
39
+
40
+ Test CheckBox
41
+ [Setup] Go To ${URL}
42
+ Select Checkbox value=Ruby
43
+ Checkbox Should Be Selected value=Ruby
44
+ Unselect Checkbox value=Ruby
45
+ Checkbox Should Not Be Selected value=Ruby
46
+
47
+ Test Element
48
+ [Setup] Go To ${URL}
49
+ Click Element name=submit
50
+ Element Should Be Visible class=errorheader
51
+ Element Should Not Be Visible name=backupCache
52
+ Element Text Should Be class=errorheader Looks like you have a question or two that still needs to be filled out.
53
+ Element Text Should Contain class=errorheader question or two
54
+ Focus name=entry.0.single
55
+ Sleep 2 seconds
56
+ ${attr}= Get Element Attribute id=ss-form method
57
+ ${text}= Get Element Text class=errorheader
58
+
59
+ Test FileField
60
+ Go To http://scribd.com/upload/supload
61
+ Choose File name=upload_doc C:/foobar.txt
62
+ ${file}= Get Filefield Path name=upload_doc
63
+
64
+ Test Form
65
+ [Setup] Go To ${URL}
66
+ Form Should Contain Button id=ss-form value=Submit
67
+ Form Should Not Contain Button id=ss-form value=foobar
68
+ Form Should Contain Checkbox id=ss-form value=Ruby
69
+ Form Should Not Contain Checkbox id=ss-form value=foobar
70
+ Go To http://scribd.com/upload/supload
71
+ Form Should Contain Filefield name=upload_doc id=file
72
+ Form Should Not Contain Filefield name=upload_doc id=foobar
73
+ Go Back
74
+ Form Should Contain Radio Button id=ss-form value=Watir
75
+ Form Should Not Contain Radio Button id=ss-form value=foobar
76
+ Form Should Contain Select List id=ss-form id=entry_6
77
+ Form Should Not Contain Select List id=ss-form id=foobar
78
+ Form Should Contain Textfield id=ss-form id=entry_0
79
+ Form Should Not Contain Textfield id=ss-form id=foobar
80
+
81
+ Test Image
82
+ [Setup] Go To ${URL}
83
+ Go To http://psd.tutsplus.com/
84
+ Click Image alt=Photoshop to HTML
85
+ Switch To Window title=Photoshop to HTML | Rockable Press
86
+ Url Should Be http://rockablepress.com/books/photoshop-to-html/
87
+ ${count}= Get Window Count
88
+ Switch To Other Window
89
+ Close Other Window
90
+
91
+ Test Link
92
+ [Setup] Go To ${URL}
93
+ Click Link text=Terms of Service
94
+ Url Should Be http://www.google.com/accounts/TOS
95
+ Go Back
96
+
97
+ Test List
98
+ [Setup] Go To ${URL}
99
+ ${items}= Get List Items class=ss-choices
100
+
101
+ Test Native
102
+ [Setup] Go To ${URL}
103
+ Capture Screenshot
104
+
105
+ Test Page
106
+ [Setup] Go To ${URL}
107
+ Title Should Be Watir Example
108
+ ${source}= Get Page Source
109
+ Log Page Source
110
+ ${text}= Get Page Text
111
+ ${status}= Get Page Status
112
+ ${title}= Get Title
113
+ ${all_links}= Get All Elements By Xpath //a
114
+ Log Executing JavaScript has been tested. It is simple wrapper around WebDriver's execute_script function, so if there's a problem, it's at the WebDriver level.
115
+ Page Should Contain What testing tool do you like?
116
+ Page Should Not Contain Supercalafragilisticexbialadocious
117
+ Page Should Contain Button value=Submit
118
+ Page Should Not Contain Button value=Foobar
119
+ Page Should Contain Checkbox value=Ruby
120
+ Page Should Not Contain Checkbox value=Clojure
121
+ Page Should Contain Element id=ss-form
122
+ Page Should Not Contain Element id=foobar
123
+ Go To http://www.google.com/
124
+ Page Should Contain Image src=/images/nav_logo29.png
125
+ Page Should Not Contain Image src=foobar
126
+ Go Back
127
+ Page Should Contain Link href=http://docs.google.com
128
+ Page Should Not Contain Link href=foobar
129
+ Page Should Contain List class=ss-choices false
130
+ Page Should Not Contain List class=foobar
131
+ Page Should Contain Radio Button value=Watir
132
+ Page Should Not Contain Radio Button value=foobar
133
+ Page Should Contain Textfield name=entry.0.single
134
+ Page Should Not Contain Textfield name=foobar
135
+ Title Should Be Watir Example
136
+ Title Should Contain Watir
137
+
138
+ Test Radio
139
+ [Setup] Go To ${URL}
140
+ Select Radio Button value=Watir
141
+ Radio Button Should Be Selected value=Watir
142
+ Radio Button Should Not Be Selected value=Selenium
143
+
144
+ Test Select
145
+ [Setup] Go To ${URL}
146
+ Go to http://drupal.org/project/issues/search/drupal
147
+ Select Item From List id=edit-status text=active
148
+ Item Should Be Selected id=edit-status text=active
149
+ Item Should Not Be Selected id=edit-status text=needs work
150
+ Select Item From List id=edit-status text=needs review
151
+ Item Should Be Selected id=edit-status text=needs review
152
+ Clear Items From List id=edit-status
153
+ Item should not be selected id=edit-status text=active
154
+ Item should not be selected id=edit-status text=needs review
155
+
156
+ Test Table
157
+ [Setup] Go To ${URL}
158
+ Go To http://www.facebook.com
159
+ ${cell_1}= Get Table Cell ${EMPTY} 1 1
160
+ Table Cell Should Contain ${EMPTY} 1 1 Email
161
+ Table Column Should Contain ${EMPTY} 1 logged in
162
+ Table Row Should Contain ${EMPTY} 3 your password?
163
+ Table Should Contain ${EMPTY} Password
164
+
165
+ Test TextField
166
+ [Setup] Go To ${URL}
167
+ Focus TextField name=entry.0.single
168
+ Input Text name=entry.0.single Foobar
169
+ Textfield Should Contain name=entry.0.single Foo
170
+ Textfield Should Be name=entry.0.single Foobar
171
+