2017-09-11 10:43:42 +00:00
|
|
|
from appium.webdriver.common.mobileby import By, MobileBy
|
2017-08-28 10:02:20 +00:00
|
|
|
from selenium.common.exceptions import NoSuchElementException, TimeoutException
|
|
|
|
from selenium.webdriver.support.wait import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions
|
2017-08-31 13:39:41 +00:00
|
|
|
from appium.webdriver.common.touch_action import TouchAction
|
2017-09-21 17:01:04 +00:00
|
|
|
import logging
|
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)
|
|
|
|
|
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):
|
2017-09-21 17:01:04 +00:00
|
|
|
logging.info('Looking for %s' % self.name)
|
2017-08-31 13:39:41 +00:00
|
|
|
return self.driver.find_element(self.locator.by, self.locator.value)
|
2017-08-28 10:02:20 +00:00
|
|
|
|
2017-10-05 19:41:17 +00:00
|
|
|
def find_elements(self):
|
|
|
|
logging.info('Looking for %s' % self.name)
|
|
|
|
return self.driver.find_elements(self.locator.by, self.locator.value)
|
|
|
|
|
2017-08-31 13:39:41 +00:00
|
|
|
def wait_for_element(self, seconds=10):
|
2017-08-28 10:02:20 +00:00
|
|
|
return WebDriverWait(self.driver, seconds)\
|
|
|
|
.until(expected_conditions.presence_of_element_located((self.locator.by, self.locator.value)))
|
|
|
|
|
2017-08-31 13:39:41 +00:00
|
|
|
def scroll_to_element(self):
|
2017-10-05 19:41:17 +00:00
|
|
|
action = TouchAction(self.driver)
|
2017-09-06 09:24:09 +00:00
|
|
|
for _ in range(5):
|
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:
|
2017-09-21 17:01:04 +00:00
|
|
|
logging.info('Scrolling to %s' % self.name)
|
2017-09-06 09:24:09 +00:00
|
|
|
action.press(x=0, y=1000).move_to(x=200, y=-1000).release().perform()
|
2017-08-31 13:39:41 +00:00
|
|
|
|
2017-09-13 14:34:42 +00:00
|
|
|
def is_element_present(self, sec=5):
|
|
|
|
try:
|
|
|
|
self.wait_for_element(sec)
|
|
|
|
return True
|
|
|
|
except TimeoutException:
|
|
|
|
return False
|
|
|
|
|
2017-09-21 17:01:04 +00:00
|
|
|
@property
|
|
|
|
def text(self):
|
|
|
|
return self.find_element().text
|
|
|
|
|
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)
|
2017-09-21 17:01:04 +00:00
|
|
|
logging.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)
|
|
|
|
logging.info('Set %s to %s' % (value, self.name))
|
|
|
|
|
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
|
|
|
|
logging.info('%s is %s' % (self.name, text))
|
|
|
|
return text
|
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()
|
2017-09-21 17:01:04 +00:00
|
|
|
logging.info('Tap on %s' % self.name)
|
2017-08-28 10:02:20 +00:00
|
|
|
return self.navigate()
|