微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

selenuim webdriver运行原理和web端自动化实战

一、webdriver运行原理

二、ui自动化框架

框架:selenium.webdriver+pytest+pytest-html

1、po模式模式

po模式:page object。操作流程与页面元素分离。测试用例中不含任何元素定位,操作,只写业务逻辑,页面元素定位写成方法封装到对象中。


Basepage:常用操作:获取元素,显性等待等等
spec page:页面元素定位,页面元素操作封装
测试用例:业务逻辑

2、pytest测试自动化框架


几个重要概念:


fixture脚手架:放置在conftest py文件里:前置后置操作:如前置:用户登陆(创建实例获取浏览器driver,登陆用户),后置:关闭页面

创建测试函数:测试用例
              参数化: @pytest.mark.parametrize("a, b", [(1,2),(3,4)])
              跳过测试:@pytest.mark.skip()
执行用例      

              pytest .py文件        执行指定文件
              pytest testcase/      执行目录下文件
生成测试报告  pytest test_report.py --html=d:\log.html   (使用库pip install -U pytest-html)

三、实践

目的:解决项目web端频繁登陆的问题.由于比较简单,没有用这个pytest框架和po模式

from selenium import webdriver


class WebLogin:
    def __init__(self):
        self.country = '中国'
        self.mobile = '14448542546'
        self.pw = 'ab12345'
        self.login_url = 'http://xx.ocloud-test.wanyol.com/'

        self.browser = webdriver.Chrome()

    def login(self):
        self.browser.get(self.login_url)
        self.browser.implicitly_wait(10)
        # 切换iframe
        self.browser.switch_to.frame("cloud_iframe")

        # 输入号码+国家编码
        self.browser.find_element('xpath', '//input[@type="email"]').send_keys(self.mobile)
        self.browser.find_element('xpath', '//div[@class="input_zone "]').click()
        self.browser.find_element('xpath', '//li[text()="{}"]'.format(self.country)).click()

        # 输入密码
        self.browser.find_element('xpath', '//input[@type="password"]').send_keys(self.pw)

        # 点击登录
        self.browser.find_element('xpath', '//button[text()="登录"]').click()


if __name__ == '__main__':
    weblogin = WebLogin()
    weblogin.login()

参考:

Selenium Webdriver原理终于搞清楚了_慕城南风的博客-CSDN博客_selenium webdriver原理

原文地址:https://www.jb51.cc/wenti/3282709.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐