from time import sleep
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
from SYS_Operation import sys_operation
class basefunction:
def click_element(self, ele):
ele.click()
def send_keys_to_element(self,ele, value):
ele.clear()
ele.send_keys(value)
def get_attribute_from_element(self,ele, attributename):
attribute_value = ele.get_attribute(attributename)
return attribute_value
def get_webdriver_waiter(self, browser):
waiter = WebDriverWait(browser, 5)
return waiter
def find_element_when_presence(self, browser, ele_tuple):
waiter = self.get_webdriver_waiter(browser)
ele = waiter.until(ec.presence_of_element_located(ele_tuple))
return ele
def find_element_and_click_when_presence(self,browser, ele_tuple):
ele = self.find_element_when_presence(browser, ele_tuple)
self.click_element(ele)
def find_element_and_send_keys_when_presence(self,browser, ele_tuple, value):
ele = self.find_element_when_presence(browser, ele_tuple)
self.send_keys_to_element(ele,value)
def find_element_and_get_attribute_when_presence(self, browser, ele_tuple, attributename):
ele = self.find_element_when_presence(browser, ele_tuple)
attribute_value = self.get_attribute_from_element(ele,attributename)
return attribute_value
def find_element_and_get_text_when_presence(self, browser, ele_tuple):
ele = self.find_element_when_presence(browser, ele_tuple)
return ele.text
def find_elements_when_presence(self, browser, ele_tuple):
try:
waiter = self.get_webdriver_waiter(browser)
ele_list = waiter.until(ec.presence_of_all_elements_located(ele_tuple))
return ele_list
except:
return []
def find_elements_and_click_by_index_when_presence(self, browser, ele_tuple, index):
ele_list = self.find_elements_when_presence(browser, ele_tuple)
self.click_element(ele_list[index])
def element_present_in_web_or_not(self, browser, ele_tuple):
try:
waiter = self.get_webdriver_waiter(browser)
waiter.until(ec.presence_of_element_located(ele_tuple))
return True
except:
return False
def get_web_picture(self,browser, picture_path):
picture =picture_path + "\\"+sys_operation().get_timestr() + ".png"
sleep(2)
browser.switch_to.default_content()
browser.get_screenshot_as_file(picture)
def switch_to_frmame_by_webdriverwait(self, browser, ele_tuple):
waiter = self.get_webdriver_waiter(browser)
waiter.until(ec.frame_to_be_available_and_switch_to_it(ele_tuple))
def switch_to_web_by_index(self, browser, index):
handles = browser.window_handles
browser.switch_to.window(handles[index])
def title_is_some_text_or_not(self, browser, str):
try:
waiter = self.get_webdriver_waiter(browser)
r = waiter.until(ec.title_is(str))
return r
except:
return False