2018-05-02 16:01:17 +00:00
|
|
|
import base64
|
|
|
|
from io import BytesIO
|
|
|
|
import os
|
2018-05-16 19:59:36 +00:00
|
|
|
|
|
|
|
import time
|
2018-06-21 16:40:27 +00:00
|
|
|
from timeit import timeit
|
|
|
|
|
2018-05-02 16:01:17 +00:00
|
|
|
from PIL import Image, ImageChops
|
2018-01-14 17:43:36 +00:00
|
|
|
from appium.webdriver.common.mobileby import MobileBy
|
2018-05-16 19:59:36 +00:00
|
|
|
from appium.webdriver.common.touch_action import TouchAction
|
2018-01-26 11:07:09 +00:00
|
|
|
from selenium.common.exceptions import NoSuchElementException
|
|
|
|
from selenium.common.exceptions import TimeoutException
|
2017-08-28 10:02:20 +00:00
|
|
|
from selenium.webdriver.support.wait import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions
|
2020-11-11 15:37:27 +00:00
|
|
|
from tests import transl
|
2017-08-28 10:02:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BaseElement(object):
|
|
|
|
class Locator(object):
|
|
|
|
|
|
|
|
def __init__(self, by, value):
|
|
|
|
self.by = by
|
|
|
|
self.value = value
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def xpath_selector(locator, value):
|
2017-09-13 14:34:42 +00:00
|
|
|
return locator(MobileBy.XPATH, value)
|
2017-08-28 10:02:20 +00:00
|
|
|
|
2017-09-11 10:43:42 +00:00
|
|
|
@classmethod
|
|
|
|
def accessibility_id(locator, value):
|
|
|
|
return locator(MobileBy.ACCESSIBILITY_ID, value)
|
|
|
|
|
2018-04-26 06:22:11 +00:00
|
|
|
@classmethod
|
|
|
|
def text_selector(locator, text):
|
|
|
|
return BaseElement.Locator.xpath_selector('//*[@text="' + text + '"]')
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def text_part_selector(locator, text):
|
|
|
|
return BaseElement.Locator.xpath_selector('//*[contains(@text, "' + text + '")]')
|
|
|
|
|
2018-08-10 10:09:19 +00:00
|
|
|
@classmethod
|
|
|
|
def id(locator, value):
|
|
|
|
return locator(MobileBy.ID, value)
|
|
|
|
|
2018-10-19 14:16:29 +00:00
|
|
|
@classmethod
|
|
|
|
def webview_selector(cls, value):
|
|
|
|
xpath_expression = '//*[@text="{0}"] | //*[@content-desc="{desc}"]'.format(value, desc=value)
|
|
|
|
return cls(MobileBy.XPATH, xpath_expression)
|
|
|
|
|
2020-11-11 15:37:27 +00:00
|
|
|
@classmethod
|
2020-12-16 17:08:57 +00:00
|
|
|
def translation_id(cls, id, suffix='', uppercase = False):
|
2020-11-11 15:37:27 +00:00
|
|
|
text = transl[id]
|
2020-12-03 12:59:20 +00:00
|
|
|
if uppercase:
|
|
|
|
text = transl[id].upper()
|
2020-11-23 12:07:24 +00:00
|
|
|
return BaseElement.Locator.xpath_selector('//*[@text="' + text + '"]' + suffix)
|
2020-11-11 15:37:27 +00:00
|
|
|
|
2017-08-28 10:02:20 +00:00
|
|
|
def __str__(self, *args, **kwargs):
|
|
|
|
return "%s:%s" % (self.by, self.value)
|
|
|
|
|
|
|
|
def __init__(self, driver):
|
|
|
|
self.driver = driver
|
|
|
|
self.locator = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
|
|
|
def navigate(self):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def find_element(self):
|
2018-06-14 10:57:04 +00:00
|
|
|
for _ in range(3):
|
|
|
|
try:
|
|
|
|
return self.driver.find_element(self.locator.by, self.locator.value)
|
2018-08-10 10:09:19 +00:00
|
|
|
except NoSuchElementException:
|
2018-08-15 12:51:52 +00:00
|
|
|
raise NoSuchElementException(
|
|
|
|
"Device %s: '%s' is not found on the screen" % (self.driver.number, self.name)) from None
|
2018-08-10 10:09:19 +00:00
|
|
|
except Exception as exception:
|
|
|
|
if 'Internal Server Error' in str(exception):
|
2018-06-14 10:57:04 +00:00
|
|
|
continue
|
2017-08-28 10:02:20 +00:00
|
|
|
|
2017-10-05 19:41:17 +00:00
|
|
|
def find_elements(self):
|
|
|
|
return self.driver.find_elements(self.locator.by, self.locator.value)
|
|
|
|
|
2018-06-28 18:46:51 +00:00
|
|
|
def click(self):
|
|
|
|
self.find_element().click()
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info('Tap on %s' % self.name)
|
2018-06-28 18:46:51 +00:00
|
|
|
|
2017-08-31 13:39:41 +00:00
|
|
|
def wait_for_element(self, seconds=10):
|
2018-01-26 11:07:09 +00:00
|
|
|
try:
|
2018-05-02 16:01:17 +00:00
|
|
|
return WebDriverWait(self.driver, seconds) \
|
2018-01-26 11:07:09 +00:00
|
|
|
.until(expected_conditions.presence_of_element_located((self.locator.by, self.locator.value)))
|
2018-08-10 10:09:19 +00:00
|
|
|
except TimeoutException:
|
2018-08-15 12:51:52 +00:00
|
|
|
raise TimeoutException(
|
|
|
|
"Device %s: '%s' is not found on the screen" % (self.driver.number, self.name)) from None
|
2018-08-29 14:04:12 +00:00
|
|
|
|
|
|
|
def wait_for_elements(self, seconds=10):
|
|
|
|
try:
|
|
|
|
return WebDriverWait(self.driver, seconds) \
|
|
|
|
.until(expected_conditions.presence_of_all_elements_located((self.locator.by, self.locator.value)))
|
|
|
|
except TimeoutException:
|
|
|
|
raise TimeoutException(
|
|
|
|
"Device %s: '%s' is not found on the screen" % (self.driver.number, self.name)) from None
|
2017-08-28 10:02:20 +00:00
|
|
|
|
2018-07-19 09:57:45 +00:00
|
|
|
def wait_for_visibility_of_element(self, seconds=10, ignored_exceptions=None):
|
2018-03-15 20:01:08 +00:00
|
|
|
try:
|
2018-07-19 09:57:45 +00:00
|
|
|
return WebDriverWait(self.driver, seconds, ignored_exceptions=ignored_exceptions) \
|
2018-03-15 20:01:08 +00:00
|
|
|
.until(expected_conditions.visibility_of_element_located((self.locator.by, self.locator.value)))
|
2018-08-10 10:09:19 +00:00
|
|
|
except TimeoutException:
|
2018-08-15 12:51:52 +00:00
|
|
|
raise TimeoutException(
|
|
|
|
"Device %s: '%s' is not found on the screen" % (self.driver.number, self.name)) from None
|
2018-03-15 20:01:08 +00:00
|
|
|
|
2018-05-25 17:29:07 +00:00
|
|
|
def wait_for_invisibility_of_element(self, seconds=10):
|
|
|
|
try:
|
|
|
|
return WebDriverWait(self.driver, seconds) \
|
|
|
|
.until(expected_conditions.invisibility_of_element_located((self.locator.by, self.locator.value)))
|
2018-08-10 10:09:19 +00:00
|
|
|
except TimeoutException:
|
2018-08-15 12:51:52 +00:00
|
|
|
raise TimeoutException("Device %s: '%s' is still visible on the screen after %s seconds" % (
|
|
|
|
self.driver.number, self.name, seconds)) from None
|
2018-05-25 17:29:07 +00:00
|
|
|
|
2018-11-21 10:44:30 +00:00
|
|
|
def scroll_to_element(self, depth: int = 9, direction='down'):
|
|
|
|
for _ in range(depth):
|
2017-08-31 13:39:41 +00:00
|
|
|
try:
|
2017-10-11 20:10:57 +00:00
|
|
|
return self.find_element()
|
2017-08-31 13:39:41 +00:00
|
|
|
except NoSuchElementException:
|
2020-05-19 08:35:02 +00:00
|
|
|
self.driver.info('Scrolling %s to %s' % (direction, self.name))
|
2020-07-09 15:24:19 +00:00
|
|
|
size = self.driver.get_window_size()
|
2018-11-21 10:44:30 +00:00
|
|
|
if direction == 'down':
|
2020-07-09 15:24:19 +00:00
|
|
|
self.driver.swipe(500, size["height"]*0.4, 500, size["height"]*0.05)
|
2018-11-21 10:44:30 +00:00
|
|
|
else:
|
2020-07-14 16:32:47 +00:00
|
|
|
self.driver.swipe(500, size["height"]*0.25, 500, size["height"]*0.8)
|
2019-07-23 23:10:55 +00:00
|
|
|
else:
|
|
|
|
raise NoSuchElementException(
|
|
|
|
"Device %s: '%s' is not found on the screen" % (self.driver.number, self.name)) from None
|
2017-08-31 13:39:41 +00:00
|
|
|
|
2020-10-06 14:48:08 +00:00
|
|
|
def scroll_and_click(self):
|
|
|
|
self.scroll_to_element()
|
|
|
|
self.click()
|
|
|
|
|
2017-09-13 14:34:42 +00:00
|
|
|
def is_element_present(self, sec=5):
|
|
|
|
try:
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info('Wait for %s' % self.name)
|
2018-03-15 20:01:08 +00:00
|
|
|
return self.wait_for_element(sec)
|
|
|
|
except TimeoutException:
|
|
|
|
return False
|
|
|
|
|
2018-07-19 09:57:45 +00:00
|
|
|
def is_element_displayed(self, sec=5, ignored_exceptions=None):
|
2018-03-15 20:01:08 +00:00
|
|
|
try:
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info('Wait for %s' % self.name)
|
2018-07-19 09:57:45 +00:00
|
|
|
return self.wait_for_visibility_of_element(sec, ignored_exceptions=ignored_exceptions)
|
2017-09-13 14:34:42 +00:00
|
|
|
except TimeoutException:
|
|
|
|
return False
|
|
|
|
|
2017-09-21 17:01:04 +00:00
|
|
|
@property
|
|
|
|
def text(self):
|
|
|
|
return self.find_element().text
|
|
|
|
|
2018-05-02 16:01:17 +00:00
|
|
|
@property
|
|
|
|
def template(self):
|
|
|
|
try:
|
|
|
|
return self.__template
|
|
|
|
except FileNotFoundError:
|
|
|
|
raise FileNotFoundError('Please add %s image as template' % self.name)
|
|
|
|
|
|
|
|
@template.setter
|
|
|
|
def template(self, value):
|
|
|
|
self.__template = Image.open(os.sep.join(__file__.split(os.sep)[:-1]) + '/elements_templates/%s' % value)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def image(self):
|
|
|
|
return Image.open(BytesIO(base64.b64decode(self.find_element().screenshot_as_base64)))
|
|
|
|
|
2019-10-03 16:08:06 +00:00
|
|
|
def attribute_value(self, value):
|
2019-10-08 08:28:52 +00:00
|
|
|
attribute_value = self.find_element().get_attribute(value)
|
2019-10-09 13:00:49 +00:00
|
|
|
if attribute_value.lower() == 'true':
|
|
|
|
attribute_state = True
|
|
|
|
elif attribute_value.lower() == 'false':
|
|
|
|
attribute_state = False
|
2019-10-08 08:28:52 +00:00
|
|
|
else:
|
2019-10-09 13:00:49 +00:00
|
|
|
attribute_state = attribute_value
|
|
|
|
return attribute_state
|
2019-10-03 16:08:06 +00:00
|
|
|
|
2020-03-17 17:27:10 +00:00
|
|
|
# Method-helper for renew screenshots in case if changed
|
2020-11-06 11:23:12 +00:00
|
|
|
def save_new_screenshot_of_element(self, name: str):
|
|
|
|
full_path_to_file = os.sep.join(__file__.split(os.sep)[:-1]) + '/elements_templates/%s' % name
|
2020-03-17 17:27:10 +00:00
|
|
|
screen = Image.open(BytesIO(base64.b64decode(self.find_element().screenshot_as_base64)))
|
|
|
|
screen.save(full_path_to_file)
|
|
|
|
|
2018-05-16 19:59:36 +00:00
|
|
|
def is_element_image_equals_template(self, file_name: str = ''):
|
|
|
|
if file_name:
|
|
|
|
self.template = file_name
|
2018-05-02 16:01:17 +00:00
|
|
|
return not ImageChops.difference(self.image, self.template).getbbox()
|
|
|
|
|
2019-05-17 14:40:35 +00:00
|
|
|
def swipe_left_on_element(self):
|
2018-05-16 19:59:36 +00:00
|
|
|
element = self.find_element()
|
|
|
|
location, size = element.location, element.size
|
|
|
|
x, y = location['x'], location['y']
|
|
|
|
width, height = size['width'], size['height']
|
2018-07-03 18:50:18 +00:00
|
|
|
self.driver.swipe(start_x=x + width * 0.75, start_y=y + height / 2, end_x=x, end_y=y + height / 2)
|
2018-05-16 19:59:36 +00:00
|
|
|
|
2020-03-26 11:36:41 +00:00
|
|
|
def swipe_to_web_element(self, depth=700):
|
2019-05-17 14:40:35 +00:00
|
|
|
element = self.find_element()
|
|
|
|
location = element.location
|
|
|
|
x, y = location['x'], location['y']
|
2020-02-24 11:30:45 +00:00
|
|
|
self.driver.swipe(start_x=x, start_y=y, end_x=x, end_y=depth)
|
2019-05-17 14:40:35 +00:00
|
|
|
|
2018-05-16 19:59:36 +00:00
|
|
|
def long_press_element(self):
|
|
|
|
element = self.find_element()
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info('Long press %s' % self.name)
|
2018-05-16 19:59:36 +00:00
|
|
|
action = TouchAction(self.driver)
|
|
|
|
action.long_press(element).release().perform()
|
|
|
|
|
2018-06-21 16:40:27 +00:00
|
|
|
def measure_time_before_element_appears(self, max_wait_time=30):
|
|
|
|
def wrapper():
|
|
|
|
return self.wait_for_visibility_of_element(max_wait_time)
|
2018-06-28 18:46:51 +00:00
|
|
|
|
2018-06-21 16:40:27 +00:00
|
|
|
return timeit(wrapper, number=1)
|
|
|
|
|
|
|
|
def measure_time_while_element_is_shown(self, max_wait_time=30):
|
|
|
|
def wrapper():
|
|
|
|
return self.wait_for_invisibility_of_element(max_wait_time)
|
2018-06-28 18:46:51 +00:00
|
|
|
|
2018-06-21 16:40:27 +00:00
|
|
|
return timeit(wrapper, number=1)
|
|
|
|
|
2017-08-28 10:02:20 +00:00
|
|
|
|
|
|
|
class BaseEditBox(BaseElement):
|
|
|
|
|
|
|
|
def __init__(self, driver):
|
|
|
|
super(BaseEditBox, self).__init__(driver)
|
|
|
|
|
|
|
|
def send_keys(self, value):
|
|
|
|
self.find_element().send_keys(value)
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info("Type '%s' to %s" % (value, self.name))
|
2017-08-28 10:02:20 +00:00
|
|
|
|
2017-10-05 19:41:17 +00:00
|
|
|
def set_value(self, value):
|
|
|
|
self.find_element().set_value(value)
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info("Type '%s' to %s" % (value, self.name))
|
2017-10-05 19:41:17 +00:00
|
|
|
|
2017-10-30 11:11:58 +00:00
|
|
|
def clear(self):
|
|
|
|
self.find_element().clear()
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info('Clear text in %s' % self.name)
|
2017-10-30 11:11:58 +00:00
|
|
|
|
2018-05-16 19:59:36 +00:00
|
|
|
def delete_last_symbols(self, number_of_symbols_to_delete: int):
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info('Delete last %s symbols from %s' % (number_of_symbols_to_delete, self.name))
|
2018-05-16 19:59:36 +00:00
|
|
|
self.click()
|
|
|
|
for _ in range(number_of_symbols_to_delete):
|
|
|
|
time.sleep(1)
|
|
|
|
self.driver.press_keycode(67)
|
|
|
|
|
|
|
|
def paste_text_from_clipboard(self):
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info('Paste text from clipboard into %s' % self.name)
|
2018-05-16 19:59:36 +00:00
|
|
|
self.long_press_element()
|
|
|
|
time.sleep(2)
|
|
|
|
action = TouchAction(self.driver)
|
|
|
|
location = self.find_element().location
|
|
|
|
x, y = location['x'], location['y']
|
2020-06-23 07:02:44 +00:00
|
|
|
action.press(x=x + 25, y=y - 50).release().perform()
|
2018-05-16 19:59:36 +00:00
|
|
|
|
|
|
|
def cut_text(self):
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info('Cut text in %s' % self.name)
|
2018-05-16 19:59:36 +00:00
|
|
|
location = self.find_element().location
|
|
|
|
x, y = location['x'], location['y']
|
|
|
|
action = TouchAction(self.driver)
|
|
|
|
action.long_press(x=x, y=y).release().perform()
|
|
|
|
time.sleep(2)
|
2018-06-28 18:46:51 +00:00
|
|
|
action.press(x=x + 50, y=y - 50).release().perform()
|
2018-05-16 19:59:36 +00:00
|
|
|
|
2017-08-28 10:02:20 +00:00
|
|
|
|
|
|
|
class BaseText(BaseElement):
|
|
|
|
|
|
|
|
def __init__(self, driver):
|
|
|
|
super(BaseText, self).__init__(driver)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def text(self):
|
2017-09-21 17:01:04 +00:00
|
|
|
text = self.find_element().text
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info('%s is %s' % (self.name, text))
|
2017-09-21 17:01:04 +00:00
|
|
|
return text
|
2017-08-28 10:02:20 +00:00
|
|
|
|
2020-11-10 17:07:39 +00:00
|
|
|
def wait_for_element_text(self, text, wait_time=30):
|
|
|
|
counter = 0
|
|
|
|
while True:
|
|
|
|
if counter >= wait_time:
|
|
|
|
self.driver.fail(
|
|
|
|
"'%s' is not equal to expected '%s' in %s sec" % (self.find_element().text, text, wait_time))
|
|
|
|
elif self.find_element().text != text:
|
|
|
|
counter += 10
|
|
|
|
time.sleep(10)
|
|
|
|
self.driver.info('Wait for text element %s to be equal to %s' % (self.name, text))
|
|
|
|
else:
|
|
|
|
self.driver.info('Element %s text is equal to %s' % (self.name, text))
|
|
|
|
return
|
2017-08-28 10:02:20 +00:00
|
|
|
|
|
|
|
class BaseButton(BaseElement):
|
|
|
|
|
|
|
|
def __init__(self, driver):
|
|
|
|
super(BaseButton, self).__init__(driver)
|
|
|
|
|
|
|
|
def click(self):
|
|
|
|
self.find_element().click()
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info('Tap on %s' % self.name)
|
2017-08-28 10:02:20 +00:00
|
|
|
return self.navigate()
|
2018-02-09 15:16:07 +00:00
|
|
|
|
2020-09-04 15:34:58 +00:00
|
|
|
def wait_and_click(self, time=30):
|
|
|
|
self.driver.info('Waiting for element %s for max %s sec and click when it is available' % (self.name, time))
|
|
|
|
self.wait_for_visibility_of_element(time)
|
|
|
|
self.click()
|
|
|
|
|
2019-10-29 16:02:07 +00:00
|
|
|
def click_until_presence_of_element(self, desired_element, attempts=4):
|
2018-02-09 15:16:07 +00:00
|
|
|
counter = 0
|
2018-03-28 10:21:39 +00:00
|
|
|
while not desired_element.is_element_present(1) and counter <= attempts:
|
2018-02-09 15:16:07 +00:00
|
|
|
try:
|
2018-08-15 12:51:52 +00:00
|
|
|
self.driver.info('Tap on %s' % self.name)
|
2018-02-09 15:16:07 +00:00
|
|
|
self.find_element().click()
|
2020-09-09 15:06:07 +00:00
|
|
|
self.driver.info('Wait for %s to be displayed' % desired_element.name)
|
2018-02-09 15:16:07 +00:00
|
|
|
desired_element.wait_for_element(5)
|
|
|
|
return self.navigate()
|
|
|
|
except (NoSuchElementException, TimeoutException):
|
|
|
|
counter += 1
|
2020-07-02 12:00:35 +00:00
|
|
|
else:
|
|
|
|
self.driver.info("%s element not found" % desired_element.name)
|
2019-11-04 13:41:18 +00:00
|
|
|
|
|
|
|
def click_until_absense_of_element(self, desired_element, attempts=3):
|
|
|
|
counter = 0
|
|
|
|
while desired_element.is_element_present(1) and counter <= attempts:
|
|
|
|
try:
|
|
|
|
self.driver.info('Tap on %s' % self.name)
|
|
|
|
self.find_element().click()
|
2020-09-09 15:06:07 +00:00
|
|
|
self.driver.info('Wait for %s to disappear' % desired_element.name)
|
2019-11-04 13:41:18 +00:00
|
|
|
counter += 1
|
|
|
|
except (NoSuchElementException, TimeoutException):
|
|
|
|
return self.navigate()
|