简体   繁体   English

RobotFramework-使用自定义库打开的浏览器无法在selenium2library中识别

[英]RobotFramework - Browser opened with custom library cannot be recognized in selenium2library

I'm starter in Python and Robot framework. 我是Python和Robot框架的入门者。 I'm trying to create and learn to use custom library in my robot framework test suite. 我正在尝试在我的机器人框架测试套件中创建并学习使用自定义库。

I have created a custom library with the following code: 我用以下代码创建了一个自定义库:

from selenium import webdriver
import time

class CustomLibrary:
def Open_My_Browser(self):
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("http://demo.guru99.com/V4/")
    time.sleep(5)

I imported this custom library and had specified the keyword "Open My Browser". 我导入了此自定义库,并指定了关键字“打开我的浏览器”。 This keyword executes the code from my custom library but the next steps are from selenium2library like click button. 这个关键字从我的自定义库中执行代码,但是接下来的步骤就像单击按钮一样来自selenium2library。

The execution stops and i get message "No browser is open". 执行停止,我收到消息“未打开浏览器”。 I understood that my selenium2library is not recognizing the browser which is opened by my customlibrary. 我知道我的selenium2library无法识别由我的自定义库打开的浏览器。 But I'm not able to fix this problem. 但我无法解决此问题。 Can anyone please shed some advice 任何人都可以请一些建议

My robot file: 我的机器人文件:

Documentation    Test the Guru99 Banking Website
Library         Selenium2Library
Library     CustomLibrary.py

*** Test Cases ***

Test Case: 001 - The user should be able to navigate to Guru99
    [Tags]  Smoke
    Open the Guru99 website

*** Keywords ***
Open the Guru99 website
    Open My Browser ```

Well, of course the browser session will not be reused - it is owned by a separate object, the SeleniumLibrary/Selenium2Library has no knowledge or access to it. 好吧,当然,浏览器会话将不会被重用-它由一个单独的对象拥有,SeleniumLibrary / Selenium2Library不了解或无法访问它。
It is the same as if you establish a DB or ssh connection manually, and then expect a library just to start using it - that doesn't happen. 就像您手动建立数据库或ssh连接,然后期望库只是开始使用它一样,这不会发生。

If you want to use the keywords in the SeleniumLibrary, you need to use its Open Browser , so it has reference to it (the browser). 如果要在SeleniumLibrary中使用关键字,则需要使用其Open Browser ,因此它具有对它的引用(浏览器)。

You can add external Python classes(keywords) as plugins . 您可以将外部Python类(关键字)添加为plugins

*** Settings ***
Library    SeleniumLibrary         plugins=${CURDIR}/Plugin.py

*** Test Cases ***
Open My Browser
    Open My Browser

Content in Plugin.py as follow: Plugin.py内容如下:

from SeleniumLibrary import BrowserManagementKeywords
from robot.api.deco import keyword
import time


class Plugin(BrowserManagementKeywords):
    @keyword
    def open_my_browser(self):
        self.open_browser("http://demo.guru99.com/V4/", "chrome")
        self.driver.maximize_window()
        time.sleep(5)

By the way, you can also creating a new library by Extending SeleniumLibrary . 顺便说一句,您还可以通过扩展SeleniumLibrary创建一个新的库。 Then replace Library Selenium2Library to Library <YourSeleniumLibrary> 然后将Library Selenium2Library替换为Library <YourSeleniumLibrary>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM