Selenium robot框架:在测试之间重用现有的浏览器窗口
我想使用Selenium robot框架:在测试之间重用现有的浏览器窗口,selenium,selenium-webdriver,robotframework,Selenium,Selenium Webdriver,Robotframework,我想使用pybot运行测试,然后使用第一个pybot打开的相同浏览器窗口,使用pybot运行更多测试 所以 我不知道怎么做 我在第一次测试中尝试了打开浏览器www.mysite.com alias=1,然后在其他测试中尝试了切换浏览器1,但他们只是错误地发现没有打开的浏览器实际上,这是绝对可能的,但是您必须在测试套件中组织测试 testsuites/ __初始化机器人 测试1.机器人 测试2.机器人 测试3.机器人 在\uuuu init\uuuuu.robot中,您必须在套件设置中打开浏览器,
pybot
运行测试,然后使用第一个pybot
打开的相同浏览器窗口,使用pybot
运行更多测试
所以
我不知道怎么做
我在第一次测试中尝试了
打开浏览器www.mysite.com alias=1
,然后在其他测试中尝试了切换浏览器1
,但他们只是错误地发现没有打开的浏览器
实际上,这是绝对可能的,但是您必须在测试套件中组织测试
testsuites/
__初始化机器人
测试1.机器人
测试2.机器人
测试3.机器人
在\uuuu init\uuuuu.robot
中,您必须在套件设置中打开浏览器,并在套件拆卸f.e.中销毁它
*** Settings ***
Suite Setup Open Browser
Suite Teardown Close Browser
然后运行测试
pybot./testsuites
我不知道效率有多高,但我需要相同的过程,我使用类似这样的代码来完成这个
open browser ${HOMEPAGE} ${BROWSER}
#opens the browser to the homepage using the browser of my choice
maximize browser window
#maximizes the window
(DOES STUFF)
#completes some processes
select window title=calendar
#selects the window with the name calendar
(DOES MORE STUFF)
这对我有用,试试看。
如果您需要有关“选择窗口”的任何信息,请查看此处:
或者你可以随时尝试
switch browser 1
我对robotframework还不熟悉,但我希望这能有所帮助。这是一个有待解决的问题。有一些变通方法和自定义代码修改似乎可以实现这一点。selenium驱动程序实例有两个属性描述其与selenium webdriver的连接-连接url和会话id。通过将这些设置为已运行的url的值,您可以有效地“劫持”它,并可以自由使用 免责声明-该解决方案使用内部SE结构,因此可以在较新版本上中断。此外,当您连接到运行的webdriver作为
远程
驱动程序时,即使您想关闭它,也无法关闭它-因此这可能会导致运行它的机器上的资源泄漏;e、 该webdriver最终必须由任务管理器手动终止
因此,首先要做的事情是:您有一个正在运行的浏览器实例,并且需要获取其属性以用于将来的连接。它们是2-driver.command\u executor.\u url
和driver.session\u id
,其中driver
是运行实例的对象名称。此python代码将实现以下功能:
from robot.libraries.BuiltIn import BuiltIn
def return_driver_props()
seLib = BuiltIn().get_library_instance('SeleniumLibrary')
# the driver is instantiated in the SeleniumLibrary, but not provided publicly, thus accessing it through this py code
remote_url = seLib.driver.command_executor._url # for local instance, this is a value in the form 'http://localhost:57856'
session_id = seLib.driver.session_id
return remote_url, session_id
将该文件作为库导入,通过调用函数/方法,您将获得以下两个特性:
${conn_url} ${session_id}= Return Driver Props
# and do whatever is needed to make them known - log, store in a file, DB, etc.
现在,在需要重新连接的第二次运行中,有了这两个值,您只需使用关键字openbrowser
并指定远程连接:
Open Browser about:about remote_url=${that_known_url} browser=${the_used_browser_type} # the last args - chrome|firefox|edge - whatever you're connecting two
棘手的部分是,当您连接到远程服务器时,selenium会自动启动一个新的会话,这是第二个浏览器实例(它在selenium3附近启动,但我不确定确切的时间)。例如,如果您现在就开始使用它,则不是您想要的浏览器,而是全新的浏览器。这也是我将“about:about”作为目标地址的原因,因此它可以非常快速地加载一个虚拟页面
此时必须发生两件事-a)您必须摆脱“虚拟”SE会话,以及b)切换到前一个会话:
def set_driver_session_id(sesion_id):
""" Sets the sessoin_id of the current driver insance to the provided one. """
seLib = BuiltIn().get_library_instance('SeleniumLibrary')
if seLib.driver.session_id != sesion_id: # this is pretty much guaranteed to be the case
seLib.driver.close() # this closes the session's window
seLib.driver.quit() # for remote connections (like ours), this deletes the session, but doesn't stop the SE
# set to the session that's already running
seLib.driver.session_id = sesion_id
使用已知会话id调用此函数/关键字:
Set Driver Session ID ${session_id}
,瞧,您现在控制着上一个浏览器,它的完整状态是:它所在的url、cookies、localStorage等等
我将向读者介绍如何自动传递url和会话id。
我自己正在做的是在运行第一个工件后将它们存储在临时文件夹中的一个文件中,然后在后续运行中从那里读取,并对其进行一些错误处理—文件丢失或损坏,连接无法发生,等等,我不认为你想要的是什么。这里有一个Firefox的解决方案,也许它也能解决你的问题。这个问题专门询问了两次运行pybot的情况。
Set Driver Session ID ${session_id}