Robot Framework:如何从Robot Framework库中访问类属性

huangapple go评论80阅读模式
英文:

Robot Framework: How to access a class property from a Robot Framework library

问题

To implement a custom function, I am trying to access the driver property from the SeleniumLibrary which looks like below in the python package:

class SeleniumLibrary(DynamicCore):

    ROBOT_LIBRARY_SCOPE = "GLOBAL"
    ROBOT_LIBRARY_VERSION = __version__

    @property
    def driver(self) -> WebDriver:
        """Current active driver.

        :rtype: selenium.webdriver.remote.webdriver.WebDriver
        :raises SeleniumLibrary.errors.NoOpenBrowser: If browser is not open.
        """
        if not self._drivers.current:
            raise NoOpenBrowser("No browser is open.")
        return self._drivers.current

I have created the webdriver using the Open Browser command like Open Browser url=${url} browser=${browser} alias=Test options=${options} executable_path=${driverPath}.

I have tried multiple commands in the robot script to access the @property driver but it is not working:

  • Log ${SeleniumLibrary.driver}
  • ${driver}= SeleniumLibrary.driver
  • ${driver}= Evaluate SeleniumLibrary.driver

Error Message: Evaluating expression 'SeleniumLibrary.driver' failed: AttributeError: module 'SeleniumLibrary' has no attribute 'driver'

Reference: https://github.com/robotframework/SeleniumLibrary/blob/master/docs/extending/extending.rst#id14

英文:

To implement a custom function, I am trying to access the driver property from the SeleniumLibary which looks like below in the python package:

class SeleniumLibrary(DynamicCore):

    ROBOT_LIBRARY_SCOPE = "GLOBAL"
    ROBOT_LIBRARY_VERSION = __version__

    @property
    def driver(self) -> WebDriver:
        """Current active driver.

        :rtype: selenium.webdriver.remote.webdriver.WebDriver
        :raises SeleniumLibrary.errors.NoOpenBrowser: If browser is not open.
        """
        if not self._drivers.current:
            raise NoOpenBrowser("No browser is open.")
        return self._drivers.current

I have create the webdriver using the Open Browser command like Open Browser url=${url} browser=${browser} alias=Test options=${options} executable_path=${driverPath}.

I have tried multiple commands in the robot script to access the @property driver but it is not working:

  • Log ${SeleniumLibrary.driver}
  • ${driver}= SeleniumLibrary.driver
  • ${driver}= Evaluate SeleniumLibrary.driver

Error Message: Evaluating expression 'SeleniumLibrary.driver' failed: AttributeError: module 'SeleniumLibrary' has no attribute 'driver'

Reference: https://github.com/robotframework/SeleniumLibrary/blob/master/docs/extending/extending.rst#id14

答案1

得分: 1

我能够使用Robot Framework找到解决方案。

${SeleniumLibrary}    Get Library Instance    SeleniumLibrary
${driver}=    Set Variable    ${SeleniumLibrary.driver}
英文:

I was able to find the solution using Robot Framework.

${SeleniumLibrary}    Get Library Instance    SeleniumLibrary
${driver}=    Set Variable    ${SeleniumLibrary.driver}

huangapple
  • 本文由 发表于 2023年6月6日 02:22:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定