机械手框架:在测试之间重用现有的浏览器窗口 [英] robot framework: Reuse existing Browser Window between Tests

查看:79
本文介绍了机械手框架:在测试之间重用现有的浏览器窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to run tests with pybot, then run more tests with pybot using the same browser window that the first pybot opened.

所以……

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

I've tried Open Browser www.mysite.com alias=1 in the first test and then Switch Browser 1 in the others, but they just error with No browser is open

推荐答案

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

A selenium driver instance has two properties characterizing its connection to a selenium webdriver - a connection url, and session id. By setting those to the values of an already running one, you effectively "hijack" it, and can use freely.

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

Disclaimer - the solution uses internal SE structures, so can break on newer versions. Also, as you are connecting to the running webdriver as to aRemote one, you cannot close it even if you want to - thus this can lead to resources leakage on the machine that it runs on; e.g. that webdriver has to be eventually terminated manually by a task manager.

首先,首先-您有一个正在运行的浏览器实例,并且需要获取其属性以用于将来的连接.它们是2-driver.command_executor._urldriver.session_id,其中driver是正在运行的实例的对象名称.这段python代码可以做到这一点:

So first things first - you have a running browser instance, and you need to get its properties for future connections. They are 2 - driver.command_executor._url, and driver.session_id where driver is the object name of the running instance. This python code will do just that:

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

通过调用函数/方法将该文件导入为库,您将获得以下两个道具:

Importing that file as a Library, by calling the function/method you'll have the 2 props:

${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并指定远程连接即可:

Now in the second run that needs to reattach, and with the 2 values in hand, you just use the keyword Open Browser and specify a remote connection:

 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"的原因-因此它可以非常快速地加载虚拟页面.

The tricky part - the moment you connect to a remote server, selenium automatically starts a new session - which is a second browser instance (this started somewhere around selenium3, though I'm not sure on the exact timing). E.g. if you start using it right now - that is not the browser you wanted, but a brand new. That's also the reason why I gave as target address "about:about" - so it loads a dummy page, very fast.

此时必须发生两件事-a)您必须摆脱虚拟" SE会话,并且b)切换到上一个:

Two things must happen at this point - a) you have to get rid of the "dummy" SE session, and b) switch to the previous one:

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调用此函数/关键字:

This function/keyword is called with the known session id:

Set Driver Session ID    ${session_id}

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

,and voilà, you are now in control of the previous browser, with its full state - the url it was at, cookies, localStorage, etc.

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

I'm leaving as an exercise to the reader how to automatically pass the url and the session id.
What I myself am doing is storing them in a file in a temp folder after running the first piece, and reading from there in the follow-up runs, with some error handling around it - missing or bad file, the connection cannot happen, and so on, with fallbacks to new instance creation.

这篇关于机械手框架:在测试之间重用现有的浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆