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()

页面类


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值