import os
from framework.logger import *
from selenium import webdriver
from selenium.webdriver.common.by import By
logger = Logger(logger='base_page').get_log()
class BasePage:
driver = None
def __init__(self, driver):
self.driver = driver
def back(self):
self.driver.back()
logger.info("Click back on current page.")
def forward(self):
self.driver.forward()
logger.info("Click forward on current page.")
def quit_browser(self):
self.driver.quit()
def wait(self, seconds):
self.driver.implicitly_wait(seconds)
logger.info("Quit browser.")
def close(self):
try:
self.driver.close()
logger.info("Closing and quit the browser.")
except Exception as e:
logger.info('Fail to quit the browser')
def get_windows_img(self):
file_path = os.path.dirname(os.getcwd()) + '\\Screenshots\\'
rq = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
screen_name = file_path + rq + '.png'
try:
self.driver.get_screenshot_as_file(screen_name)
logger.info('开始截图并保存')
except Exception as e:
logger.error('截图异常')
def find_element(self, selector):
element = ''
if '=>' not in selector:
return
select_by = selector.split('=>')[0]
select_value = selector.split('=>')[1]
if select_by == 'id':
try:
element = self.driver.find_element(By.ID, select_value)
logger.info(f'had find element:{element.text}, by:{select_by}, value:{select_value}')
except Exception as e:
logger.error(f"NoSuchElementException:{e}")
self.get_windows_img()
elif select_by == 'name':
try:
element = self.driver.find_element(By.NAME, select_value)
logger.info(f'had find element:{element.text}, by:{select_by}, value:{select_value}')
except Exception as e:
logger.error(f"NoSuchElementException:{e}")
self.get_windows_img()
elif select_by == 'class_name':
try:
element = self.driver.find_element(By.CLASS_NAME, select_value)
logger.info(f'had find element:{element.text}, by:{select_by}, value:{select_value}')
except Exception as e:
logger.error(f"NoSuchElementException:{e}")
self.get_windows_img()
elif select_by == 'link_text':
try:
element = self.driver.find_element(By.LINK_TEXT, select_value)
logger.info(f'had find element:{element.text}, by:{select_by}, value:{select_value}')
except Exception as e:
logger.error(f"NoSuchElementException:{e}")
self.get_windows_img()
elif select_by == 'partial_link_text':
try:
element = self.driver.find_element(By.PARTIAL_LINK_TEXT, select_value)
logger.info(f'had find element:{element.text}, by:{select_by}, value:{select_value}')
except Exception as e:
logger.error(f"NoSuchElementException:{e}")
self.get_windows_img()
elif select_by == 'tag_name':
try:
element = self.driver.find_element(By.TAG_NAME, select_value)
logger.info(f'had find element:{element.text}, by:{select_by}, value:{select_value}')
except Exception as e:
logger.error(f"NoSuchElementException:{e}")
self.get_windows_img()
elif select_by == 'css_selector':
try:
element = self.driver.find_element(By.CSS_SELECTOR, select_value)
logger.info(f'had find element:{element.text}, by:{select_by}, value:{select_value}')
except Exception as e:
logger.error(f"NoSuchElementException:{e}")
self.get_windows_img()
elif select_by == 'xpath':
try:
element = self.driver.find_element(By.XPATH, select_value)
logger.info(f'had find element:{element.text}, by:{select_by}, value:{select_value}')
except Exception as e:
logger.error(f"NoSuchElementException:{e}")
self.get_windows_img()
else:
raise NameError("Please enter a valid type of targeting elements.")
return element
def type(self, selector, text):
element = self.find_element(selector)
element.clear()
try:
element.send_keys(text)
logger.info(f"Had type {text} in inputBox")
except NameError as e:
logger.error(f"Failed to type in input box with {e}")
self.get_windows_img()
def clear(self, selector):
element = self.find_element(selector)
try:
element.clear()
logger.info("Clear text in input box before typing.")
except NameError as e:
logger.error(f"Failed to clear in input box with {e}")
self.get_windows_img()
def click(self, selector):
element = self.find_element(selector)
try:
element.click()
logger.info(f"The element {element.text} was clicked.")
except NameError as e:
logger.error(f"Failed to click the element with {e}")
def get_page_title(self):
logger.info(f"Current page title is {self.driver.title}")
return self.driver.title
@staticmethod
def sleep(seconds):
time.sleep(seconds)
logger.info(f"Sleep for {seconds} seconds")
if __name__ == '__main__':
driver = webdriver.Chrome()
BasePage(driver).get_windows_img()