2018-09-15 17:18:29 +00:00
|
|
|
import logging
|
2018-10-23 14:49:29 +00:00
|
|
|
import time
|
2018-09-15 17:18:29 +00:00
|
|
|
import pytest
|
2018-10-24 21:01:30 +00:00
|
|
|
import re
|
2018-11-16 09:53:05 +00:00
|
|
|
|
2018-11-09 21:26:36 +00:00
|
|
|
try:
|
|
|
|
import org.sikuli.script.SikulixForJython
|
|
|
|
from sikuli import *
|
|
|
|
except Exception:
|
|
|
|
pass
|
2018-09-15 17:18:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BaseElement(object):
|
2018-11-16 09:53:05 +00:00
|
|
|
def __init__(self, screenshot, x=0, y=0, w=1024, h=768):
|
2018-09-15 17:18:29 +00:00
|
|
|
self.screenshot = screenshot
|
2018-10-24 21:01:30 +00:00
|
|
|
self.name = re.findall('([^\/]+)(?=.png)', self.screenshot)[0].replace('_', ' ').title()
|
2018-11-16 09:53:05 +00:00
|
|
|
self.region = Region(x, y, w, h)
|
2018-09-15 17:18:29 +00:00
|
|
|
|
2018-10-24 21:01:30 +00:00
|
|
|
def find_element(self, log=True):
|
|
|
|
if log:
|
|
|
|
logging.info('Find %s' % self.name)
|
2018-09-15 17:18:29 +00:00
|
|
|
try:
|
2018-11-16 09:53:05 +00:00
|
|
|
self.region.wait(self.screenshot, 10)
|
2018-09-15 17:18:29 +00:00
|
|
|
except FindFailed:
|
2018-10-24 21:01:30 +00:00
|
|
|
pytest.fail('%s was not found' % self.name)
|
2018-09-15 17:18:29 +00:00
|
|
|
|
2018-10-24 21:01:30 +00:00
|
|
|
def click(self, log=True):
|
|
|
|
if log:
|
|
|
|
logging.info('Click %s' % self.name)
|
|
|
|
self.find_element(log=False)
|
2018-11-16 09:53:05 +00:00
|
|
|
self.region.click(self.screenshot)
|
|
|
|
|
|
|
|
def is_visible(self):
|
|
|
|
try:
|
|
|
|
self.region.wait(self.screenshot, 10)
|
|
|
|
return True
|
|
|
|
except FindFailed:
|
|
|
|
return False
|
2018-09-15 17:18:29 +00:00
|
|
|
|
2018-10-23 14:49:29 +00:00
|
|
|
def verify_element_is_not_present(self):
|
2018-10-24 21:01:30 +00:00
|
|
|
logging.info('Verify: %s is not present' % self.name)
|
2018-10-23 14:49:29 +00:00
|
|
|
try:
|
2018-11-16 09:53:05 +00:00
|
|
|
self.region.wait(self.screenshot, 10)
|
2018-10-24 21:01:30 +00:00
|
|
|
pytest.fail('%s is displayed but not expected' % self.name)
|
2018-10-23 14:49:29 +00:00
|
|
|
except FindFailed:
|
|
|
|
pass
|
|
|
|
|
2018-11-16 09:53:05 +00:00
|
|
|
def get_target(self):
|
|
|
|
self.find_element(log=False)
|
|
|
|
return self.region.find(self.screenshot).getTarget()
|
|
|
|
|
2018-09-15 17:18:29 +00:00
|
|
|
|
|
|
|
class InputField(BaseElement):
|
|
|
|
|
|
|
|
def input_value(self, value):
|
2018-10-24 21:01:30 +00:00
|
|
|
logging.info("%s field: set value '%s'" % (self.name, value))
|
|
|
|
self.click(log=False)
|
2018-09-15 17:18:29 +00:00
|
|
|
type(value)
|
2018-10-23 14:49:29 +00:00
|
|
|
|
2018-10-24 21:01:30 +00:00
|
|
|
def send_keys(self, value):
|
|
|
|
logging.info("Type '%s' to %s field" % (value, self.name))
|
|
|
|
self.click(log=False)
|
2018-10-23 14:49:29 +00:00
|
|
|
for i in str(value):
|
|
|
|
type(i)
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
2018-10-24 21:01:30 +00:00
|
|
|
def is_focused(self):
|
|
|
|
self.find_element(log=False)
|
2018-11-16 09:53:05 +00:00
|
|
|
return self.get_target() == Env.getMouseLocation()
|
2018-10-24 21:01:30 +00:00
|
|
|
|
|
|
|
def verify_is_focused(self):
|
|
|
|
logging.info('Verify %s is focused' % self.name)
|
|
|
|
if not self.is_focused():
|
|
|
|
pytest.fail('%s is not focused' % self.name)
|
|
|
|
|
2018-10-23 14:49:29 +00:00
|
|
|
|
|
|
|
class TextElement(object):
|
|
|
|
def __init__(self, text):
|
2018-11-16 09:53:05 +00:00
|
|
|
self.text = text
|
2018-10-23 14:49:29 +00:00
|
|
|
self.element_line = None
|
2018-11-16 09:53:05 +00:00
|
|
|
|
|
|
|
def find_element(self):
|
2018-10-23 14:49:29 +00:00
|
|
|
for _ in range(3):
|
|
|
|
lines = collectLines()
|
|
|
|
for line in lines:
|
2018-11-16 09:53:05 +00:00
|
|
|
if self.text in line.getText().encode('ascii', 'ignore'):
|
2018-10-23 14:49:29 +00:00
|
|
|
self.element_line = line
|
|
|
|
return
|
|
|
|
time.sleep(3)
|
2018-11-16 09:53:05 +00:00
|
|
|
pytest.fail("Element with text '%s' was not found" % self.text)
|
2018-10-23 14:49:29 +00:00
|
|
|
|
|
|
|
def click(self):
|
2018-11-16 09:53:05 +00:00
|
|
|
logging.info("Click %s button" % self.text)
|
|
|
|
self.find_element()
|
2018-10-23 14:49:29 +00:00
|
|
|
self.element_line.click()
|
2018-11-16 09:53:05 +00:00
|
|
|
|
|
|
|
def get_whole_text(self):
|
|
|
|
self.find_element()
|
|
|
|
return self.element_line.getText().encode('ascii', 'ignore')
|
|
|
|
|
|
|
|
def is_visible(self):
|
|
|
|
from _pytest.runner import Failed
|
|
|
|
try:
|
|
|
|
self.find_element()
|
|
|
|
return True
|
|
|
|
except Failed:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def verify_element_is_not_present(self):
|
|
|
|
if self.is_visible():
|
|
|
|
pytest.fail("'%s text is displayed but not expected'" % self.text)
|