-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2.2.1_locate_using_lambda_spec.rb
55 lines (44 loc) · 1.75 KB
/
2.2.1_locate_using_lambda_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
class GreeterPage2_2_1 < WatirPump::Page
uri '/greeter.html'
# This notation can be considered a shorthand for a regular method declaration
text_field :name, -> { root.text_field(id: 'name') }
# The line above is equivalent to this pseudocode:
# def name
# # whatever is inside the body of the lambda comes here
# element = root.text_field(id: 'name')
# raise unless element.is_a? TextField
# element
# end
# The lambda can be parametrized
button :set, ->(id) { root.button(id: id) }
# The line above is equivalent to the following pseudocode:
# def set(id)
# element = root.button(id: id)
# raise unless element.is_a? Button
# element
# end
# The type of the located element is validated against the class macro name. Here: button
button :not_a_button, -> { root.text_field(id: 'name') }
# As the spec below demostrates method created this way is exptected to throw an exception
# Let's add a 'regularly' located element
div :greeting, id: 'greeting'
# Elements located with lambdas can refer to other elements
span :greeted_name, -> { greeting.span }
# There is no way to achieve this behavior with watir methods and locator hashes
end
RSpec.describe GreeterPage2_2_1 do
it 'displays greeting' do
GreeterPage2_2_1.open do
expect(name).to be_a(Watir::TextField)
name.set 'Bogdan'
expect(set('set_name')).to be_a(Watir::Button)
set('set_name').click
expect(greeting).to be_a(Watir::Div)
expect(greeting.text).to eq 'Hello Bogdan!'
expect(greeted_name).to be_a(Watir::Span)
expect(greeted_name.text).to eq 'Bogdan'
expect { not_a_button }.to raise_error(WatirPump::Errors::ElementMismatch)
end
end
end