Skip to content

Commit 22730a7

Browse files
authored
add strict find image (#193)
* add strict find image * fix rubocop * add fixScaleTemplateImage * set landscape and portrait
1 parent f6a3f7f commit 22730a7

File tree

2 files changed

+145
-31
lines changed

2 files changed

+145
-31
lines changed

test/functional/android/android/device_test.rb

+77
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,83 @@ def test_file_management
402402
File.delete 'pic_folder.zip'
403403
end
404404

405+
def test_image_element
406+
skip 'Requres `npm install -g appium opencv4nodejs`' unless `npm list -g opencv4nodejs`.include? 'opencv4nodejs'
407+
408+
@driver.rotation = :portrait
409+
410+
el = @driver.find_element :accessibility_id, 'NFC'
411+
@driver.save_element_screenshot el, 'test/functional/data/test_android_nfc.png'
412+
413+
image_element = @driver.find_element_by_image AppiumLibCoreTest.path_of('test/functional/data/test_android_nfc.png')
414+
415+
assert image_element.inspect
416+
assert image_element.hash
417+
assert image_element.ref =~ /\Aappium-image-element-[a-z0-9\-]+/
418+
419+
el_location = el.location
420+
image_location = image_element.location
421+
assert_in_delta el_location.x, image_location.x, 1
422+
assert_in_delta el_location.y, image_location.y, 1
423+
424+
el_size = el.size
425+
image_size = image_element.size
426+
assert_in_delta el_size.width, image_size.width, 1
427+
assert_in_delta el_size.height, image_size.height, 1
428+
429+
el_rect = el.rect
430+
image_rect = image_element.rect
431+
assert_in_delta el_rect.x, image_rect.x, 1
432+
assert_in_delta el_rect.y, image_rect.y, 1
433+
assert_in_delta el_rect.width, image_rect.width, 1
434+
assert_in_delta el_rect.height, image_rect.height, 1
435+
436+
assert_equal el.displayed?, image_element.displayed?
437+
image_element.click
438+
439+
assert @driver.find_element :accessibility_id, 'TechFilter'
440+
@driver.back
441+
end
442+
443+
def test_image_elements
444+
skip 'Requres `npm install -g appium opencv4nodejs`' unless `npm list -g opencv4nodejs`.include? 'opencv4nodejs'
445+
446+
@driver.rotation = :landscape
447+
448+
el = @driver.find_element :accessibility_id, 'App'
449+
@driver.save_element_screenshot el, 'test/functional/data/test_android_app.png'
450+
451+
image_elements = @driver.find_elements_by_image AppiumLibCoreTest.path_of('test/functional/data/test_android_app.png')
452+
image_element = image_elements[0]
453+
454+
assert image_element.inspect
455+
assert image_element.hash
456+
assert image_element.ref =~ /\Aappium-image-element-[a-z0-9\-]+/
457+
458+
el_location = el.location
459+
image_location = image_element.location
460+
assert_in_delta el_location.x, image_location.x, 1
461+
assert_in_delta el_location.y, image_location.y, 1
462+
463+
el_size = el.size
464+
image_size = image_element.size
465+
assert_in_delta el_size.width, image_size.width, 1
466+
assert_in_delta el_size.height, image_size.height, 1
467+
468+
el_rect = el.rect
469+
image_rect = image_element.rect
470+
assert_in_delta el_rect.x, image_rect.x, 1
471+
assert_in_delta el_rect.y, image_rect.y, 1
472+
assert_in_delta el_rect.width, image_rect.width, 1
473+
assert_in_delta el_rect.height, image_rect.height, 1
474+
475+
assert_equal el.displayed?, image_element.displayed?
476+
image_element.click
477+
478+
assert @driver.find_element :accessibility_id, 'Action Bar'
479+
@driver.back
480+
end
481+
405482
private
406483

407484
def scroll_to(text)

test/functional/ios/ios/device_test.rb

+68-31
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,85 @@ def setup
1111
@@driver ||= @@core.start_driver
1212
end
1313

14+
def teardown
15+
save_reports(@@driver)
16+
end
17+
1418
def test_image_element
1519
skip 'Requres `npm install -g appium opencv4nodejs`' unless `npm list -g opencv4nodejs`.include? 'opencv4nodejs'
1620

17-
@@driver.update_settings({ fixImageFindScreenshotDims: false,
18-
fixImageTemplateSize: true,
19-
autoUpdateImageElementPosition: true })
20-
21-
e = @@driver.find_element_by_image AppiumLibCoreTest.path_of('test/functional/data/test_button_image_ios.png')
22-
23-
assert e.inspect
24-
assert e.hash
25-
assert e.ref =~ /\Aappium-image-element-[a-z0-9\-]+/
26-
assert e.location.x
27-
assert e.location.y
28-
assert e.size.width
29-
assert e.size.height
30-
assert e.rect.x
31-
assert e.rect.y
32-
assert e.rect.width
33-
assert e.rect.height
34-
assert e.displayed?
21+
@@driver.update_settings({ fixScaleTemplateImage: true })
22+
23+
el = @@driver.find_element :accessibility_id, 'Buttons'
24+
@@driver.save_element_screenshot el, 'test/functional/data/test_ios_button.png'
25+
26+
image_element = @@driver.find_element_by_image AppiumLibCoreTest.path_of('test/functional/data/test_ios_button.png')
27+
28+
assert image_element.inspect
29+
assert image_element.hash
30+
assert image_element.ref =~ /\Aappium-image-element-[a-z0-9\-]+/
31+
32+
el_location = el.location
33+
image_location = image_element.location
34+
assert_in_delta el_location.x, image_location.x, 1
35+
assert_in_delta el_location.y, image_location.y, 1
36+
37+
el_size = el.size
38+
image_size = image_element.size
39+
assert_in_delta el_size.width, image_size.width, 1
40+
assert_in_delta el_size.height, image_size.height, 1
41+
42+
el_rect = el.rect
43+
image_rect = image_element.rect
44+
assert_in_delta el_rect.x, image_rect.x, 1
45+
assert_in_delta el_rect.y, image_rect.y, 1
46+
assert_in_delta el_rect.width, image_rect.width, 1
47+
assert_in_delta el_rect.height, image_rect.height, 1
48+
49+
assert_equal el.displayed?, image_element.displayed?
50+
image_element.click
51+
52+
assert @@driver.find_element :accessibility_id, 'Gray'
53+
@@driver.back
3554
end
3655

3756
def test_image_elements
3857
skip 'Requres `npm install -g appium opencv4nodejs`' unless `npm list -g opencv4nodejs`.include? 'opencv4nodejs'
3958

40-
@@driver.update_settings({ fixImageTemplateSize: true,
41-
autoUpdateImageElementPosition: true })
59+
@@driver.update_settings({ fixScaleTemplateImage: true })
4260

43-
e = @@driver.find_elements_by_image AppiumLibCoreTest.path_of('test/functional/data/test_arrow_multiple_ios.png')
61+
el = @@driver.find_element :accessibility_id, 'Buttons'
62+
@@driver.save_element_screenshot el, 'test/functional/data/test_ios_button.png'
4463

45-
assert e[0].inspect
46-
assert e[0].hash
47-
assert e[0].ref =~ /\Aappium-image-element-[a-z0-9\-]+/
48-
assert e[0].location
49-
assert e[0].size
50-
assert e[0].rect
51-
assert e[0].displayed?
52-
end
64+
image_elements = @@driver.find_elements_by_image AppiumLibCoreTest.path_of('test/functional/data/test_ios_button.png')
65+
image_element = image_elements[0]
5366

54-
def teardown
55-
save_reports(@@driver)
67+
assert image_element.inspect
68+
assert image_element.hash
69+
assert image_element.ref =~ /\Aappium-image-element-[a-z0-9\-]+/
70+
71+
el_location = el.location
72+
image_location = image_element.location
73+
assert_in_delta el_location.x, image_location.x, 1
74+
assert_in_delta el_location.y, image_location.y, 1
75+
76+
el_size = el.size
77+
image_size = image_element.size
78+
assert_in_delta el_size.width, image_size.width, 1
79+
assert_in_delta el_size.height, image_size.height, 1
80+
81+
el_rect = el.rect
82+
image_rect = image_element.rect
83+
assert_in_delta el_rect.x, image_rect.x, 1
84+
assert_in_delta el_rect.y, image_rect.y, 1
85+
assert_in_delta el_rect.width, image_rect.width, 1
86+
assert_in_delta el_rect.height, image_rect.height, 1
87+
88+
assert_equal el.displayed?, image_element.displayed?
89+
image_element.click
90+
91+
assert @@driver.find_element :accessibility_id, 'Gray'
92+
@@driver.back
5693
end
5794

5895
def test_window_size

0 commit comments

Comments
 (0)