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原理

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...