机器人框架:在测试之间重用现有的浏览器窗口

时间:2015-07-27 19:01:15

标签: selenium selenium-webdriver robotframework

我想使用pybot运行测试,然后使用与第一个pybot打开的浏览器窗口相同的浏览器窗口,使用pybot运行更多测试。

因此...

pybot test1.txt
#opens browser window and runs test1.txt and doesn't close the window
#pybot completes execution
pybot test2.txt
#uses the same browser window from test1
#pybot completes execution
pybot test3.txt
#uses the same browser window from test1
#pybot completes execution    

无法弄清楚如何做到这一点......

我在第一次测试中尝试Open Browser www.mysite.com alias=1,然后在其他测试中尝试Switch Browser 1,但他们只是在No browser is open

时出错了

4 个答案:

答案 0 :(得分:0)

实际上绝对可以这样做,但是你必须在测试套件中组织你的测试 testsuites/ __init__.robot test1.robot test2.robot test3.robot

__init__.robot中你必须在套件设置中打开浏览器并在套件拆解中销毁它。

*** Settings ***
Suite Setup   Open Browser
Suite Teardown    Close Browser

并运行测试pybot ./testsuites

答案 1 :(得分:0)

我不知道它有多高效,但我需要相同的过程,我使用这样的代码来完成这个......

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)

这对我有用,试一试。 如果您需要有关“选择窗口”的任何信息,请在此处查看:http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Select%20Window

或者你可以随时尝试

switch browser  1

我是机器人框架的新手,但我希望这有帮助。

答案 2 :(得分:0)

new functionality to re-use existing browser session有一个未解决的问题。有一些解决方法提及和自定义代码修改似乎可以实现此目的。

答案 3 :(得分:0)

Selenium驱动程序实例具有两个表征其与Selenium Webdriver的连接的属性-连接URL和会话ID。通过将这些值设置为已经运行的值,您可以有效地“劫持”它,并可以自由使用。

免责声明-该解决方案使用内部SE结构,因此可以在较新的版本上使用。另外,当您连接到正在运行的Remote网络驱动程序时,即使您要关闭它也无法将其关闭-这样会导致运行它的机器上的资源泄漏。例如该网络驱动程序最终必须由任务管理器手动终止。

首先,首先-您有一个正在运行的浏览器实例,并且需要获取其属性以用于将来的连接。它们是2-driver.command_executor._urldriver.session_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

将该文件作为库导入,通过调用函数/方法,您将有2个道具:

${conn_url}  ${session_id}=    Return Driver Props
# and do whatever is needed to make them known - log, store in a file, DB, etc.

现在,在第二次运行中,需要重新连接,并拥有2个值,您只需使用关键字Open Browser并指定远程连接即可:

 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}

和voilà,您现在可以控制以前的浏览器,并保持其完整状态-所在的URL,Cookie,localStorage等。


我要让读者练习如何自动传递url和会话ID。
我自己正在做的是在运行第一部分后将它们存储在temp文件夹中的文件中,并在后续运行中从那里读取内容,并对其进行了一些错误处理-丢失或错误的文件,无法发生连接以及等等,还有对新实例创建的回退。

相关问题