【python+selenium-01】基础版本演变为关键字驱动

最初(基础)版本的UI自动化脚本样式如下:

(以下均以百度首页为例)

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
driver.implicitly_wait(30)

driver.get("http://www.baidu.com")

driver.find_element_by_id("kw").send_keys("python")
driver.find_element_by_id("su").click()
sleep(3)
driver.quit()

通过selenium支持的基础功能,定位元素然后对于的send_keys() 或者 click()操作。

 

关键字驱动

什么是关键字驱动?

基于selenium底层的方法,我们在写自动化代码时,实际上反复的调用find_element_by_id,*by_xpath,.send_keys()或者 .click(),诸如此类的方法反反复复的调用。

而每次调用的区别就在于什么操作,以什么方式,定位什么元素,需要传什么参数。

所以我们做了封装,只需要传入有区别的参数进去即可。

 

为什么要实现关键字驱动?

  • 减少代码冗余;
  • 减少代码过多重复;
  • 重点突出"关键字",便于维护;
  • 增强可阅读性;

 

封装好的类:

from selenium import webdriver
import time


# 浏览器初始化
def open_browser(name, url):
    if name == "chrome":
        driver = webdriver.Chrome()
        print("starting for Chrome!")
    elif name == "firefox":
        driver = webdriver.Firefox()
        print("starting for Firefox!")
    elif name == "ie":
        driver = webdriver.Ie()
        print("starting for IE!")
    driver.get(url)
    driver.maximize_window()
    return driver


class WebUIInit(object):
    # 初始化
    def __init__(self, name, url):
        self.driver = open_browser(name, url)

    # 关闭浏览器
    def quit(self):
        self.driver.quit()

    # 依据不同的定位方法,进行输入操作
    def send_keys(self, locator_type, locator, text):
        if locator_type == "id":
            self.driver.find_element_by_id(locator).send_keys(text)
        elif locator_type == "name":
            self.driver.find_element_by_name(locator).send_keys(text)
        elif locator_type == "xpath":
            self.driver.find_element_by_xpath(locator).send_keys(text)

    # 依据不同的定位方法,进行点击操作
    def click(self, locator_type, locator):
        if locator_type == "id":
            self.driver.find_element_by_id(locator).click()
        elif locator_type == "name":
            self.driver.find_element_by_name(locator).click()
        elif locator_type == "xpath":
            self.driver.find_element_by_xpath(locator).click()

    # 定义强制等待
    def sleep(self, seconds):
        time.sleep(seconds)

    # 定义隐式等待
    def wait(self, seconds):
        self.driver.implicitly_wait(seconds)

    # 切换至新窗口
    def switch_to_new_window(self):
        handles = self.driver.window_handles
        self.driver.switch_to_window(handles[1])

    # 关闭旧窗口
    def close_old_window(self):
        self.driver.close()

    # 切换至旧窗口
    def switch_to_old_window(self):
        handles = self.driver.window_handles
        self.driver.switch_to_window(handles[0])

    # 切换新窗口,关闭旧窗口
    def switch_to_new_and_close_old_window(self):
        handles = self.driver.window_handles
        self.driver.close()
        self.driver.switch_to_window(handles[1])

    # 获取元素文本内容进行断言校验
    def assert_by_text(self, locator_type, locator, text):
        if locator_type == "id":
            element_text = self.driver.find_element_by_id(locator).text
        elif locator_type == "name":
            element_text = self.driver.find_element_by_name(locator).text
        elif locator_type == "xpath":
            element_text = self.driver.find_element_by_xpath(locator).text

        if element_text == text:
            print("daladala")
        else:
            print(element_text + " VS " + text)
            print("wrong!")

    # 切换iframe窗体
    def switch_to_iframe(self, locator_type, locator):
        if locator_type == "id":
            self.driver.switch_to.frame(self.driver.find_element_by_id(locator))
        elif locator_type == "name":
            self.driver.switch_to.frame(self.driver.find_element_by_name(locator))
        elif locator_type == "xpath":
            self.driver.switch_to.frame(self.driver.find_element_by_xpath(locator))

    # 切换回默认iframe窗体
    def switch_to_default(self):
        self.driver.switch_to_default_content()

基于关键字驱动的封装类,再次实现百度首页的UI自动化测试代码:

from auto.tool.webui_autotest import *
from time import sleep

driver = WebUIInit("firefox", "http://www.baidu.com")
driver.send_keys("id", "kw", "python")
driver.click("id", "su")
sleep(3)
driver.quit()

 

posted @ 2021-01-31 07:25  愚人李愚  阅读(109)  评论(0编辑  收藏  举报