How to add a python module which uses Sel2Lib with

2019-05-29 05:17发布

问题:

I'm trying to import a python module to ride for more than 3 hours with no success. I went through the steps explained in fourth answer here where it suggests to create a python module Selenium2LibraryExt. How to get All Text in robot framework ?

the problem that i observe is that since i use Selenim2Library in my other codes of the same test now i import Selenium2LibraryExt which inherits from Selenim2Library, my test doesn't know anymore that e.g. Click Element keyword comesfrom Selenim2Library or Selenium2LibraryExt and it gives me multiple keyword error

So i did 1-I removed

       from Selenium2Library import Selenium2Library

from the head of my python module but i let it stay as a library in my test case: settings

 Library          Selenium2Library  

it didn't work out. 2-Then i removed

Library          Selenium2Library  

from my test case but added:

  from Selenium2Library import Selenium2Library  

in the head of my python module. but in both cases i get errors. how should i do not to have 2 selenium2library libraries seen by my test?

Thanks

回答1:

If you go with a library that inherits, then your test data needs to import either Selenium2Library or your custom library, but not both. If you only import through a shared resource file, and not directly in the tests, this is easier to control.

Another option is to create a library that extends Selenium2Library without replacing it:

from robot.libraries.BuiltIn import BuiltIn

class Selenium2LibraryExt(object):

    @property
    def _s2l(self):
        return BuiltIn().get_library_instance('Selenium2Library')

    def get_all_texts(self, locator):
        """Returns the text values of elements identified by `locator`."""
        elements = self._s2l._element_find(locator, False, True)
        return [e.text for e in elements]

If you are using a recent version of Selenium2Library (>= 1.7), Get Webelement and Get Webelements allow you to do a lot of things there are not keyword for...

@{texts}    Create List
@{elems}    Get Webelements    some locator
:FOR    ${elem}    IN    @{elems}
\    ${text}    Get Text    ${elem}
\    Append To List    ${texts}    ${text}

Same thing but getting the text using the extended variable syntax to work with a webelement.

@{texts}    Create List
@{elems}    Get Webelements    some locator
:FOR    ${elem}    IN    @{elems}
\    Append To List    ${texts}    ${elem.text}

Or in Python:

from robot.libraries.BuiltIn import BuiltIn

class Selenium2LibraryExt(object):

    def get_all_texts(self, locator):
        """Returns the text values of elements identified by `locator`."""
        s2l = BuiltIn().get_library_instance('Selenium2Library')
        elements = s2l.get_webelements(locator)
        # or elements = BuiltIn().run_keyword('Get Webelements', locator)
        return [e.text for e in elements]

See also https://stackoverflow.com/a/35323931/2532697



回答2:

So you can easily get around this by specifying the particular library you want to use. For example:

Selenium2LibraryExt.Click Element