admin管理员组

文章数量:1543650

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.webdrivermon.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/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/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'])

本文标签: 打开浏览器模式测试seleniumDebug