python - 创建 Pyinstaller 包后无法通过调用 Chrome 驱动程序来运行 Python Unittest Selenium 脚本
问题描述
我正在创建一个在 Windows 上运行的独立包,这样我的客户端就不需要安装任何 IDE 或 Python 或 Selenium 等。
我的自动化测试脚本是使用以下东西开发的: Python 3.x unittest framework Selenium (Chrome driver) Openpyxl to read/write excel files
此外,脚本使用来自 settings.ini 文件的一些路径。
此外,我的脚本位于不同的 Python 目录/包中,如下所示:
主文件夹
------------测试用例(Python包)
--------------------------- test_main_driver.py
-- ----------页面对象(Python包)
---------------** home.py
-- -------------------------** dashboard.py
------------Utils(Python包)
--- ------------------------------------** string_util.py
test_main_driver.py 调用chrome驱动和导入其他包,还有测试类,设置和测试方法**包含代码
我正在使用 Pyinstaller 使用规范文件创建独立包,但是每当我在创建包后执行 exe 文件时,就会出现并关闭空 cmd。
为了调试问题,我尝试了简单的自动化脚本而不使用 unittest 框架(即使没有定义类或方法)。修复路径问题并创建 exe 后,它运行良好。在相同的脚本中,我使用了 openpyxl,但这也不起作用。
在我看来,主要问题是“Python 的单元测试框架”。之后,第二个问题是更正我在主测试驱动程序文件中使用的包的路径。但是,下面是我在创建 exe 后用来解决单元测试相关问题的代码。
使用 Pyinstaller 制作包后,使用以下代码(没有类/方法/包/单元测试)调用 Chrome:
current_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe()))[0]))
chromedriver = os.path.join(current_folder, "..\\..\\chromedriver.exe")
driver = webdriver.Chrome(executable_path=chromedriver)
driver.get('http://stackoverflow.com')
使用 Pyinstaller 制作包后,Chrome 不会使用以下代码(使用类/方法/单元测试)调用:
class MyTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
current_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe()))[0]))
chromedriver = os.path.join(current_folder, "..\\..\\chromedriver.exe")
print(chromedriver)
time.sleep(5)
cls.driver = webdriver.Chrome(executable_path=chromedriver)
def test_method(self):
self.driver.get('http://stackoverflow.com')
规格文件:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['start.py'],
pathex=['.....\\TestCases'],
binaries=[(".....\\chromedriver.exe", ".")],
datas=[(".....\\TTestData", ".")],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='start',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
[('chromedriver.exe', 'chromedriver.exe', 'DATA')],
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='start')
解决方案
推荐阅读
- javascript - 未记录从 Firebase 读取的数据
- python - 从列表中删除完全匹配的元素
- asp.net - 在 ASP.NET 4.8 中等待后 HttpContext 为空
- javascript - 单击链接时使用 javascript 获取表格行的内容
- python - Groupby 数据框从每个组成员中获取非空元素
- regex - 在gvim中搜索带方括号的多个字符串
- r - 使用 tidyverse 和 sapply 在列表上创建函数
- api - Active Collab API - 自托管的 URL 结构
- c - 调用 CheckTokenMembership 总是失败(返回值总是 False) - 从 SCADA (WinCC) 调用
- react-native - React-Native 62.2 代码图片上传错误:网络错误