且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在python中为硒Webdriver导入和导出FireFox配置文件

更新时间:2022-04-18 22:31:07

您必须确定一个位置来存储缓存的配置文件,然后使用os库中的函数检查该位置中是否存在文件,并加载它.要首先缓存配置文件,您应该能够从webdriver.firefox_profile.path获取配置文件的路径,然后将内容复制到缓存位置.

You have to decide on a location to store the cached profile, then use functions in the os library to check if there is a file in that location, and load it. To cache the profile in the first place, you should be able to get the path to the profile from webdriver.firefox_profile.path, then copy the contents to your cache location.

所有这些,我真的建议您反对.通过缓存在测试运行时创建的配置文件,您可以根据以前的行为对测试进行更改,这意味着它不再是孤立的并且可靠地可重复.我建议您与测试分开创建一个配置文件,然后始终将其用作基础配置文件.这使您的测试具有可预测的可重复性.甚至将Selenium设置为可以很好地使用此模式,因为它实际上并不使用您提供的配置文件,而是将其复制并使用复制的文件来启动浏览器.

All that said, I'd really recommend against this. By caching the profile created at test runtime, you are making your test mutate based upon previous behavior, which means it is no longer isolated and reliably repeatable. I'd recommend that you create a profile separately from the test, then use that as the base profile all the time. This makes your tests predictably repeatable. Selenium is even set up to work well with this pattern, as it doesn't actually use the profile you provide it, but instead duplicates it and uses the duplicate to launch the browser.