selenium - 机器人框架 : Reuse existing Browser Window between Tests

标签 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

最佳答案

selenium 驱动程序实例有两个属性来表征它与 selenium webdriver 的连接 - 连接 url 和 session ID。通过将这些设置为已经运行的值,您可以有效地“劫持”它,并且可以自由使用。

免责声明 - 该解决方案使用内部 SE 结构,因此可以在较新版本上中断。此外,当您连接到正在运行的 webdriver 时, Remote一,即使您想关闭它也无法关闭 - 因此这可能导致运行它的机器上的资源泄漏;例如该 webdriver 最终必须由任务管理器手动终止。

所以首先要做的事情是——你有一个正在运行的浏览器实例,你需要获取它的属性以供将来连接。他们是 2 - driver.command_executor._url , 和 driver.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 个 Prop :
${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 会自动启动一个新 session - 这是第二个浏览器实例(这在 selenium3 附近开始,尽管我不确定确切的时间)。例如。如果您现在开始使用它 - 即 不是 你想要的浏览器,但一个全新的。这也是我给出目标地址“about:about”的原因——所以它加载了一个虚拟页面,非常快。

此时必须发生两件事 - a) 您必须摆脱“虚拟”SE session ,以及 b) 切换到前一个 session :
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

使用已知 session ID 调用此函数/关键字:
Set Driver Session ID    ${session_id}

,瞧,您现在可以控制以前的浏览器及其完整状态 - 它所在的网址、cookies、localStorage 等。

我将作为练习留给读者如何自动传递 url 和 session ID。
我自己正在做的是在运行第一部分后将它们存储在临时文件夹中的文件中,并在后续运行中从那里读取,并对其进行一些错误处理 - 文件丢失或错误,连接无法发生,并且依此类推,回退到新实例创建。

关于selenium - 机器人框架 : Reuse existing Browser Window between Tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31661189/

相关文章:

java - 我如何创建 Java 中测试执行的结果/报告

java - 在以下 HTML 的 Selenium webdriver 中出现 "Element is not currently visible and so may not be interacted with"错误

selenium-webdriver - 将控制台与 Selenium Webdriver.Chrome() 一起使用会导致异常

python - 如何在机器人框架中将列表连接到字符串

linux - 是否可以使用机器人框架从另一台远程服务器连接到远程服务器

java - 有没有办法将wireshark http跟踪导入到java中?

java - 在 Selenium 的单个窗口中更改不同页面之间的驱动程序焦点

java - 我不想在Java中重复代码selenium

unit-testing - Wait Until 不存在,不适用于 Selenium C#

python - 如何在 robotframework 库中使用 selenium webdriver 方法?