forked from appium/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_by_image_tests.py
More file actions
91 lines (75 loc) · 3.51 KB
/
find_by_image_tests.py
File metadata and controls
91 lines (75 loc) · 3.51 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import base64
import unittest
import pytest
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from appium import webdriver
from helper import desired_capabilities
@pytest.mark.skip(reason="Need to fix broken test")
class FindByImageTests(unittest.TestCase):
def setUp(self):
desired_caps = desired_capabilities.get_desired_capabilities('ApiDemos-debug.apk')
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# relax template matching
self.driver.update_settings({"fixImageFindScreenshotDims": False,
"fixImageTemplateSize": True,
"autoUpdateImageElementPosition": True})
def tearDown(self):
self.driver.quit()
def test_find_based_on_image_template(self):
image_path = desired_capabilities.PATH('file/find_by_image_success.png')
print(image_path)
with open(image_path, 'rb') as png_file:
b64_data = base64.b64encode(png_file.read()).decode('UTF-8')
el = WebDriverWait(self.driver, 3).until(
EC.presence_of_element_located((By.IMAGE, b64_data))
)
size = el.size
self.assertIsNotNone(size['width'])
self.assertIsNotNone(size['height'])
loc = el.location
self.assertIsNotNone(loc['x'])
self.assertIsNotNone(loc['y'])
rect = el.rect
self.assertIsNotNone(rect['width'])
self.assertIsNotNone(rect['height'])
self.assertIsNotNone(rect['x'])
self.assertIsNotNone(rect['y'])
self.assertTrue(el.is_displayed())
el.click()
self.driver.find_element_by_accessibility_id("Alarm")
def test_find_multiple_elements_by_image_just_returns_one(self):
WebDriverWait(self.driver, 3).until(
EC.presence_of_element_located((By.ACCESSIBILITY_ID, "App"))
)
image_path = desired_capabilities.PATH('file/find_by_image_success.png')
els = self.driver.find_elements_by_image(image_path)
els[0].click()
self.driver.find_element_by_accessibility_id("Alarm")
def test_find_throws_no_such_element(self):
image_path = desired_capabilities.PATH('file/find_by_image_failure.png')
with open(image_path, 'rb') as png_file:
b64_data = base64.b64encode(png_file.read()).decode('UTF-8')
with self.assertRaises(TimeoutException):
WebDriverWait(self.driver, 3).until(
EC.presence_of_element_located((By.IMAGE, b64_data))
)
with self.assertRaises(NoSuchElementException):
self.driver.find_element_by_image(image_path)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(FindByImageTests)
unittest.TextTestRunner(verbosity=2).run(suite)