selenium自动化测试debug模式,打开浏览器

debug模式打开浏览器后,会使用浏览器的缓存进行操作

debug模式需要打开浏览器程序,以Chrome浏览器为例:

1.找到Chrome.exe文件路径

2.创建.bat文件,内容如下 路径 + 命令及端口

C:\Users\frank\AppData\Local\Google\Chrome\Application\chrome.exe --remote-debugging-port=9222

3.Python测试文件中加入,以pytest框架中 conftest.py文件为例

# coding:utf-8
import os
import pytest
import time
from selenium import webdriver
from selenium.webdriver.common.by import By


@pytest.fixture(scope='session')
def debug_browser():
    print('执行以debug模式打开浏览器的批处理文件')
    os.popen("d:\PythonProject\\testProject\\tools\chrome.bat")
    options = webdriver.ChromeOptions()
    options.debugger_address = '127.0.0.1:9222'
    driver = webdriver.Chrome(options=options)
    # 返回浏览器对象
    yield driver
    # 用例执行后,关闭exe浏览器程序(debug和正常打开不同,使用quit无法关闭程序,需要运行命令)
    os.system('taskkill /im chromedriver.exe /F')
    os.system('taskkill /im chrome.exe /F')
    print("test end!")


def browser():
    driver = webdriver.Chrome()
    driver.get('https://www.zhipin.com/web/geek/recommend')
    time.sleep(6)

4.用例中调用

# coding:utf-8

import pytest
import time


class Test_boss:

    def test_01(self,debug_browser):
        driver = debug_browser
        # 使用debug的浏览器直接打开boss当前账户的页面
        driver.get('https://www.zhipin.com/web/geek/resume?ka=header-resume')
        # 获取当前用户的用户名
        text = driver.find_element('xpath','//*[@id="userinfo"]/div/div[2]/p').text
        print(text)
        assert text=='xxxx'


if __name__ == '__main__':
    pytest.main(['-v', '-s', '-m',  'boss_test.py'])

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值