python自动化框架常用base.py基本操作工具类

from selenium  import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select

def open_browser(browser):
    '''
    :param browser:
    :return:
    '''
    if browser=="chrome":
        driver = webdriver.Chrome()
    elif browser=="firefox":
        driver = webdriver.firefox()
    elif browser=="ie":
        driver = webdriver.ie()
    else:
        print("please input correct browser name")
        driver = None
    return driver


class Base:
    def __init__(self,driver):
        '''
        :param driver:
        '''
        self.driver = driver

    def open_url(self,url):
        self.driver.get(url)
        self.driver.maximize_window()
    def close(self):
        self.driver.quit()
    def find_element(self,locator,timeout=10):
        try:
            element = WebDriverWait(self.driver,timeout).until(EC.presence_of_element_located(locator))
        except:
            print(f"元素{locator}没找到")
            element = None
        return element
    def find_elements(self,locator,timeout=10):
        try:
            elements = WebDriverWait(self.driver,timeout).until(EC.presence_of_all_elements_located(locator))
        except Exception as e:
            print(f"没有找到{locator}元素组")
            print(e)
            elements = False
        return elements
    def is_text_in_elements(self,locator,text,timeout=10):
        try:
            result = WebDriverWait(self.driver,timeout).until(EC.text_to_be_present_in_element(locator,text))
            return result
        except Exception as e:
            print(e)

    def is_value_in_elements(self,locator,value,timeout=10):
        try:
            result = WebDriverWait(self.driver,timeout).until(EC.text_to_be_present_in_element_value(locator,value))
            return result
        except Exception as e:
            print(e)

    def click(self,locator):
        element = self.find_element(locator)
        try:
            element.click()
        except Exception as e:
            print(e)

    def send_keys(self,locator,text):
        element = self.find_element(locator)
        try:
            element.send_keys(text)
        except Exception as e:
            print(e)
    def send_keys(self,locator,*key):
        element = self.find_element(locator)
        try:
            element.send_keys(*key)
        except Exception as e:
            print(e)

    def clear_text(self,locator):
        element = self.find_element(locator)
        try:
            element.clear()
        except Exception as e:
            print(e)

    def select_by_value(self,locator,value):
        element = self.find_element(locator)
        try:
            Select(element).select_by_value(value)
        except Exception as e:
            print(e)

    def select_by_index(self, locator,index):
        element = self.find_element(locator)
        try:
            Select(element).select_by_index(index)
        except Exception as e:
            print(e)

    def select_by_visible_text(self, locator,text):
        element = self.find_element(locator)
        try:
            Select(element).select_by_visible_text(text)
        except Exception as e:
            print(e)


    def browser_forward(self):
        try:
            self.driver.forward()
        except Exception as e:
            print(e)

    def minwindow(self):
        try:
            self.driver.minimize_window()
        except Exception as e:
            print(e)

    def maxwindow(self):
        try:
            self.driver.maximize_window()
        except Exception as e:
            print(e)

    def browser_refresh(self):
        try:
            self.driver.refresh()
        except Exception as e:
            print(e)

    def browser_back(self):
        try:
            self.driver.back()
        except Exception as e:
            print(e)

    def browser_close(self):
        try:
            self.close()
        except Exception as e:
            print(e)



    def mouse_context_click(self,locator):
        element = self.find_element(locator)
        try:
            ActionChains(self.driver).context_click(element).perform()
        except Exception as e:
            print(e)

    def mouse_double_click(self,locator):
        element = self.find_element(locator)
        try:
            ActionChains(self.driver).double_click(element).perform()
        except Exception as e:
            print(e)

    def drag_A_to_B(self,locatorA,locatorB):
        elementA = self.find_element(locatorA)
        elementB = self.find_element(locatorB)
        try:
            ActionChains(self.driver).drag_and_drop(elementA,elementB).perform()
        except Exception as e:
            print(e)

    def drag_byXY(self,locator,x,y):
        element = self.find_element(locator)
        try:
            ActionChains(self.driver).drag_and_drop_by_offset(element,x,y).perform()
        except Exception as e:
            print(e)

    def move_to(self,locator):
        element = self.find_element(locator)
        try:
            ActionChains(self.drive).move_to_element(element).perform()
        except Exception as e:
            print(e)

    def scrollToJS(self,jsscript):
        try:
            self.driver.execute_script(jsscript)
        except Exception as e:
            print(e)

    def switchToFramebyname(self,frame_name):
        try:
            frame = self.driver.switch_to.frame(frame_name)
        except Exception as e:
            print(e)

    def switchToFramebyindex(self,index):
        try:
            frame = self.driver.switch_to.frame(index)
        except Exception as e:
            print(e)

    def switchToParentFrame(self):
        '''
        退出到上一层
        :return:
        '''
        try:
            frame = self.driver.switch_to.parent_frame()
        except Exception as e:
            print(e)

    def switchToDefaultFrame(self):
        '''
        退出到最外面一层
        :return:
        '''
        try:
            frame = self.driver.switch_to.default_content()
        except Exception as e:
            print(e)

    def switchToMainwindow(self):
        try:
            frame = self.driver.window('main')
        except Exception as e:
            print(e)

    def switchToAlert(self):
        try:
            alert = self.driver.switch_to.alert
        except Exception as e:
            print(e)
            alert = None
        return alert

    def get_current_window_handle(self):
        try:
            id = self.driver.current_window_handle
        except Exception as e:
            print(e)
            id = None
        return id

    def get_window_handles(self):
        try:
            whandles = self.driver.window_handles
        except Exception as e:
            print(e)
            whandles = None
        return whandles

    def switch_to_window(self,id):
        try:
            self.driver.driver.switch_to.window(id)
        except Exception as e:
            print(e)
    def is_selected(self,locator):
        element = self.find_element(locator)
        try:
            result = element.is_selected
        except Exception as e:
            print(e)
            result = None
        return result


if __name__ == '__main__':
    driver = open_browser("chrome")
    base = Base(driver)
    base.open_url("http://www.baidu.com")
    searchlocator=("id","kw")
    searchbutton = ("id","su")
    base.send_keys(searchlocator, "1234567890")
    base.send_keys(searchlocator,(Keys.BACKSPACE,'A'))
    base.click(searchbutton)
    time.sleep(2)
    base.close()


 

上一篇:plt绘图常用命令


下一篇:自动化学习笔记(一)