Selenium实验

1.补充

browser.close()      关闭当前页面,防止句柄过多
有新句柄生成时,需要设置隐式等待,这是全局操作,直到页面加载完或超时结束
关闭后仍需要切换句柄

2.重复操作的简化

import unittest2
from selenium import webdriver
import time
from base_keys import *
from ddt import ddt, data


@ddt
class Unittest3(unittest2.TestCase):

    # 前提条件
    def setUp(self):
        self.driver = BaseKeys('Chrome', 'http://www.baidu.com')

    @data('老师', '医生', '美女')
    def test_1(self, text):
        self.driver.input_text('id', 'kw', text)
        self.driver.click_keys('id', 'su')
        time.sleep(2)
        self.driver.quit()

    def tearDown(self):
        self.driver.quit()


使用装饰器实现多个查找重复进行
设置强制等待来看到测试过程
data处也可以传递多组参数, 使用 @unpack装饰器解包

程序执行流程   setUp----test-用例----tearDown  循环执行
函数名称及格式不能改变

3.关键字驱动底层函数封装

from selenium import webdriver
import time


def open_browser(name, url):
    if name == 'Chrome':
        driver = webdriver.Chrome()
        print('start for Chrome')
    driver.maximize_window()
    driver.get(url)
    return driver


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

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

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

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

    # 切换至新窗体
    def switch_new_current(self):
        handles = self.driver.window_handles
        self.driver.switch_to.window(handles[1])

    # 关闭旧窗体
    def close_old_current(self):
        self.driver.close()

    # 切换至旧窗体
    def switch_old_current(self):
        handles = self.driver.window_handles()
        self.driver.switch_to.window(handles[0])

    # 切换至新窗体并关闭旧窗体
    def switch_new_current_and_close_old_current(self):
        handles = self.driver.window_handles
        self.driver.close()
        self.driver.switch_to.window(handles[1])

    # 输入搜索内容
    def input_text(self, locator_type, locator, text):
        if locator_type == 'xpath':
            self.driver.find_element_by_xpath(locator).send_keys(text)
        if locator_type == 'id':
            self.driver.find_element_by_id(locator).send_keys(text)
        if locator_type == 'name':
            self.driver.find_element_by_name(locator).send_keys(text)

    # 点击操作
    def click_keys(self, locator_type, locator):
        if locator_type == 'xpath':
            self.driver.find_element_by_xpath(locator).click()
        if locator_type == 'name':
            self.driver.find_element_by_name(locator).click()
        if locator_type == 'id':
            self.driver.find_element_by_id(locator).click()

    # 获取文本元素进行断言校验
    def assert_by_text(self, locator_type, locator, text):
        if locator_type == 'xpath':
            element_text = self.driver.find_element_by_xpath(locator).text
        if locator_type == 'id':
            element_text = self.driver.find_element_by_id(locator).text
        if locator_type == 'name':
            element_text = self.driver.find_element_by_name(locator).text
        if element_text == text:
            print('ok, that is all right')
        print(element_text + 'VS' + text)
        print('this is an Error')

    # 切换至Iframe窗体
    def switch_to_iframe(self, locator_type, locator):
        if locator_type == 'xpath':
            self.driver.switch_to.frame(self.driver.find_element_by_xpath(locator))
        elif 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))

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

4.企业中的PO模式自动化

PO模式原理

    采用分层的形式,来定义出不同页面的操作内容
    
    1.BasePage:基础层,用于提供最为底层的功能,让其他页面对象直接进行继承
    
    2.Page:页面层,用于提供元素的获取,各个元素操作方法的封装
    
    3.业务层:通过这一层的调用,传入参数进行自动化

    优点:针对指定的被测业务系统流程,执行自动化时非常简便
    缺点:对于多个系统的复用性相对较差

代码实现

基础类

# 定义页面基础类, 所有的页面都需要继承这个基础类
import time


class BasePage(object):
    # 初始化基础类
    def __init__(self, driver, url):
        self.driver = driver
        self.url = url
        
    # 启动浏览器,访问指定页面
    def open(self):
        self.driver.get(self.url)
        self.driver.maximize_window()
        
    # 定位元素
    def locator_element(self, *locator):
        ele = self.driver.find_element(*locator)
        return ele
    
    # 浏览器退出
    def quit(self):
        time.sleep(2)
        self.driver.quit()

页面类


你可能感兴趣的:(Selenium实验)