-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_page.py
85 lines (69 loc) · 2.59 KB
/
base_page.py
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
import time
import allure
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import (
StaleElementReferenceException,
TimeoutException)
from selenium.webdriver import ActionChains
from ui.locators import basic_locators
class PageNotOpenedExeption(Exception):
pass
class BasePage(object):
locators = basic_locators.BasePageLocators()
URL = None
def is_opened(self, timeout=15):
started = time.time()
while time.time() - started < timeout:
if self.driver.current_url == self.URL:
return True
raise PageNotOpenedExeption(
f'{self.URL} did not open in {timeout} current url {self.driver.current_url}')
def __init__(self, driver):
self.driver = driver
def wait(self, timeout=None):
if timeout is None:
timeout = 15
return WebDriverWait(self.driver, timeout=timeout)
def find(self, locator, timeout=None):
return self.wait(timeout).until(
EC.presence_of_element_located(locator)
)
@allure.step('Click')
def click(self, locator, timeout=None):
try:
self.find(locator, timeout=timeout)
if self.visibility_element(locator):
elem = self.wait(timeout).until(
EC.element_to_be_clickable(locator)
)
elem.click()
except StaleElementReferenceException:
time.sleep(1)
self.click(locator)
@allure.step('Filled the field with a value')
def fill_field(self, query, locator):
"""Finds an element on the page and enters the desired query"""
elem = self.find(locator)
if self.visibility_element(locator):
elem.clear()
elem.send_keys(query)
def visibility_element(self, locator, timeout=None):
"""Displays the visibility of an element"""
try:
elem = self.wait(timeout).until(
EC.visibility_of_all_elements_located(locator)
)
return elem
except TimeoutException:
return
def get_value_attribute(self, locator, attribute_name):
self.visibility_element(locator)
elem = self.driver.find_element(*locator)
return elem.get_attribute(attribute_name)
def go_to_url(self, url):
self.driver.get(url)
@allure.step('Upload file')
def upload_file(self, locator, file_path):
self.find(locator).send_keys(file_path)