Skip to content

Commit 45e56b9

Browse files
Fix flaky android tests by using wait
1 parent 723b304 commit 45e56b9

File tree

14 files changed

+191
-154
lines changed

14 files changed

+191
-154
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
describe 'android/dynamic.rb' do
22
t 'dynamic_code_to_string' do
3-
act = dynamic_code_to_string 3, 'hi'
4-
act.must_equal 'textContains( "hi" )'
3+
wait { dynamic_code_to_string(3, 'hi').must_equal 'textContains( "hi" )' }
54
end
65
end

android_tests/lib/android/specs/android/element/alert.rb

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
describe 'android/element/alert.rb' do
22
def open_alert
33
# trigger the alert
4-
button('OK Cancel dialog with a long message').click
4+
wait { button('OK Cancel dialog with a long message').click }
55
end
66

77
t 'alert_click' do
88
# nav to alert activity
9-
find('app').click
10-
find('alert').click
9+
wait { find('app').click }
10+
wait { find('alert').click }
1111

1212
# test
13-
open_alert
14-
alert_click('Something')
13+
wait { open_alert }
14+
wait { alert_click('Something') }
1515
end
1616

1717
t 'alert_accept' do
18-
open_alert
19-
alert_accept
18+
wait { open_alert }
19+
wait { alert_accept }
2020
end
2121

2222
t 'alert_accept_text' do
23-
open_alert
24-
alert_accept_text.must_equal 'OK'
25-
alert_accept
23+
wait { open_alert }
24+
wait { alert_accept_text.must_equal 'OK' }
25+
wait { alert_accept }
2626
end
2727

2828
t 'alert_dismiss' do
29-
open_alert
30-
alert_dismiss
29+
wait { open_alert }
30+
wait { alert_dismiss }
3131
end
3232

3333
t 'alert_dismiss_text' do
34-
open_alert
35-
alert_dismiss_text.must_equal 'Cancel'
36-
alert_dismiss
34+
wait { open_alert }
35+
wait { alert_dismiss_text.must_equal 'Cancel' }
36+
wait { alert_dismiss }
3737

3838
# nav to start activity
3939
2.times { back }

android_tests/lib/android/specs/android/element/button.rb

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
describe 'android/element/button' do
33
def before_first
44
# nav to buttons activity
5-
find('App').click
6-
find('Activity').click
7-
find('Animation').click
5+
wait { find('App').click }
6+
wait { find('Activity').click }
7+
wait { find('Animation').click }
88
end
99

1010
def after_last
@@ -20,35 +20,35 @@ def fade_in
2020

2121
t 'button' do
2222
# by index
23-
button(1).name.must_equal fade_in
23+
wait { button(1).name.must_equal fade_in }
2424

2525
# by name contains
26-
button('ade').name.must_equal fade_in
26+
wait { button('ade').name.must_equal fade_in }
2727
end
2828

2929
t 'buttons' do
3030
exp = ['Zoom in', 'Modern zoom in', 'Thumbnail zoom']
31-
buttons('zoom').map { |e| e.text }.must_equal exp
31+
wait { buttons('zoom').map { |e| e.text }.must_equal exp }
3232
end
3333

3434
t 'first_button' do
35-
first_button.name.must_equal fade_in
35+
wait { first_button.name.must_equal fade_in }
3636
end
3737

3838
t 'last_button' do
39-
last_button.name.must_equal 'Thumbnail zoom'
39+
wait { last_button.name.must_equal 'Thumbnail zoom' }
4040
end
4141

4242
t 'button_exact' do
43-
button_exact(fade_in).name.must_equal fade_in
43+
wait { button_exact(fade_in).name.must_equal fade_in }
4444
end
4545

4646
t 'buttons_exact' do
47-
buttons_exact(fade_in).first.name.must_equal fade_in
47+
wait { buttons_exact(fade_in).first.name.must_equal fade_in }
4848
end
4949

5050
t 'e_buttons' do
51-
e_buttons.length.must_equal 6
51+
wait { e_buttons.length.must_equal 6 }
5252
end
5353

5454
t { after_last }

android_tests/lib/android/specs/android/element/generic.rb

+11-13
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,38 @@ def partial
1010
end
1111

1212
t 'find works before and after get_source' do
13-
find(partial).text.must_equal content
14-
get_source.class.must_equal String
15-
find(partial).text.must_equal content
13+
wait { find(partial).text.must_equal content }
14+
wait { get_source.class.must_equal String }
15+
wait { find(partial).text.must_equal content }
1616
end
1717

1818
t 'find' do
19-
find(partial).text.must_equal content
19+
wait { find(partial).text.must_equal content }
2020
end
2121

2222
t 'finds' do
23-
finds(partial).first.text.must_equal content
23+
wait { finds(partial).first.text.must_equal content }
2424
end
2525

2626
t 'find_exact' do
27-
find_exact(content).text.must_equal content
27+
wait { find_exact(content).text.must_equal content }
2828
end
2929

3030
t 'finds_exact' do
31-
finds_exact(content).first.text.must_equal content
31+
wait { finds_exact(content).first.text.must_equal content }
3232
end
3333

3434
# scroll_to is broken
3535
t 'scroll_to' do
36-
find('Views').click
37-
e = scroll_to 'rotating button'
38-
e.text.must_equal 'Rotating Button'
36+
wait { find('Views').click }
37+
wait { scroll_to('rotating button').text.must_equal 'Rotating Button' }
3938
# back to start activity
4039
back
4140
end
4241

4342
t 'scroll_to_exact' do
44-
find('Views').click
45-
e = scroll_to_exact 'Rotating Button'
46-
e.text.must_equal 'Rotating Button'
43+
wait { find('Views').click }
44+
wait { scroll_to_exact('Rotating Button').text.must_equal 'Rotating Button' }
4745
# back to start activity
4846
back
4947
end

android_tests/lib/android/specs/android/element/text.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,34 @@ def must_raise_no_element &block
66
end
77

88
t 's_text' do
9-
s_text(1).text.must_equal 'API Demos'
10-
s_text('mos').text.must_equal 'API Demos'
9+
wait { s_text(1).text.must_equal 'API Demos' }
10+
wait { s_text('mos').text.must_equal 'API Demos' }
1111
end
1212

1313
t 's_texts' do
14-
s_texts('i').length.must_equal 6
14+
wait { s_texts('i').length.must_equal 7 }
1515
end
1616

1717
t 'first_s_text' do
18-
first_s_text.text.must_equal 'API Demos'
18+
wait { first_s_text.text.must_equal 'API Demos' }
1919
end
2020

2121
t 'last_s_text' do
22-
last_s_text.text.must_equal 'Views'
22+
wait { last_s_text.text.must_equal 'Views' }
2323
end
2424

2525
t 's_text_exact' do
2626
must_raise_no_element { s_text_exact 'mos' }
2727

2828
# should pass
29-
s_text_exact('API Demos').text.must_equal 'API Demos'
29+
wait { s_text_exact('API Demos').text.must_equal 'API Demos' }
3030
end
3131

3232
t 's_texts_exact' do
33-
s_texts_exact('API Demos').length.must_equal 1
33+
wait { s_texts_exact('API Demos').length.must_equal 1 }
3434
end
3535

3636
t 'e_s_texts' do
37-
e_s_texts.length.must_equal 12
37+
wait { e_s_texts.length.must_equal 14 }
3838
end
3939
end

android_tests/lib/android/specs/android/element/textfield.rb

+12-12
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ def right
1313

1414
def before_first
1515
# nav to activity
16-
find('app').click
17-
find('activity').click
18-
find('custom title').click
16+
wait { find('app').click }
17+
wait { find('activity').click }
18+
wait { find('custom title').click }
1919
end
2020

2121
def after_last
@@ -26,34 +26,34 @@ def after_last
2626
t { before_first }
2727

2828
t 'textfield' do
29-
textfield(1).text.must_equal left
30-
textfield('right').text.must_equal right
29+
wait { textfield(1).text.must_equal left }
30+
wait { textfield('right').text.must_equal right }
3131
end
3232

3333
t 'textfields' do
34-
textfields('right').first.text.must_equal right
34+
wait { textfields('right').first.text.must_equal right }
3535
end
3636

3737
t 'first_textfield' do
38-
first_textfield.text.must_equal left
38+
wait { first_textfield.text.must_equal left }
3939
end
4040

4141
t 'last_textfield' do
42-
last_textfield.text.must_equal right
42+
wait { last_textfield.text.must_equal right }
4343
end
4444

4545
t 'textfield_exact' do
4646
must_raise_no_element { textfield_exact 'zz' }
47-
textfield_exact(left).text.must_equal left
47+
wait { textfield_exact(left).text.must_equal left }
4848
end
4949

5050
t 'textfields_exact' do
51-
textfields_exact('zz').must_equal []
52-
textfields_exact(left).first.text.must_equal left
51+
wait { textfields_exact('zz').must_equal [] }
52+
wait { textfields_exact(left).first.text.must_equal left }
5353
end
5454

5555
t 'e_textfields' do
56-
e_textfields.length.must_equal 2
56+
wait { e_textfields.length.must_equal 2 }
5757
end
5858

5959
t { after_last }
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
# rake android[android/helper]
22
describe 'android/helper' do
33
t 'tags' do
4-
exp = ["API Demos", "Accessibility", "Animation", "App", "Content", "Graphics", "Media", "NFC", "OS", "Preference", "Text", "Views"]
5-
6-
# must wait for page to load
7-
ignore { wait_true(10) { tags('android.widget.TextView').map { |e| e.text } == exp } }
8-
act = tags('android.widget.TextView').map { |e| e.text }
9-
10-
act.must_equal exp
4+
wait { tags('android.widget.TextView').length.must_equal 14 }
115
end
126

137
def page_class_data
@@ -26,16 +20,18 @@ def page_class_data
2620
t 'get_page_class' do
2721
# digit values change based on screen size
2822
# larger screens have more elements
29-
exp = page_class_data.gsub(/\d+/, '')
30-
act = get_page_class.gsub(/\d+/, '')
31-
act.must_equal exp
23+
wait do
24+
exp = page_class_data.gsub(/\d+/, '')
25+
act = get_page_class.gsub(/\d+/, '')
26+
act.must_equal exp
27+
end
3228
end
3329

3430
# t 'page_class' do # tested by get_page_class
3531

3632
t 'get_android_inspect' do
3733
# line count changes based on screen size
38-
get_android_inspect.split("\n").length.must_be :>=, 36
34+
wait { get_android_inspect.split("\n").length.must_be :>=, 40 }
3935
end
4036

4137
# t 'get_inspect' do # tested by get_android_inspect
@@ -50,27 +46,35 @@ def id_value
5046
end
5147

5248
t 'xml_keys' do
53-
id_pair = xml_keys id_key
54-
id_pair = id_pair.to_a[0]
55-
id_pair.length.must_equal 2
56-
id_pair.first.must_equal id_key
57-
id_pair.last.must_equal id_value
49+
wait do
50+
id_pair = xml_keys id_key
51+
id_pair = id_pair.to_a[0]
52+
id_pair.length.must_equal 2
53+
id_pair.first.must_equal id_key
54+
id_pair.last.must_equal id_value
55+
end
5856
end
5957

6058
t 'resolve_id' do
61-
str = resolve_id id_key
62-
str.must_equal id_value
59+
wait do
60+
str = resolve_id id_key
61+
str.must_equal id_value
62+
end
6363
end
6464

6565
t 'xml_values' do
66-
value = xml_values(id_value).first.last
67-
value.must_equal id_value
66+
wait do
67+
value = xml_values(id_value).first.last
68+
value.must_equal id_value
69+
end
6870
end
6971

7072
t 'find by id' do
71-
find('accessibility').click
72-
find('accessibility node provider').click
73-
id 'accessibility_node_provider' # Accessibility/Accessibility Node Provider
74-
2.times { back }
73+
wait do
74+
find('accessibility').click
75+
find('accessibility node provider').click
76+
id 'accessibility_node_provider' # Accessibility/Accessibility Node Provider
77+
2.times { back }
78+
end
7579
end
7680
end

0 commit comments

Comments
 (0)