1

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

Community
  • 1
  • 1
Michelle
  • 156
  • 1
  • 5
  • 16
  • Your problem looks similar to this: http://stackoverflow.com/questions/32230658/issue-in-creating-custom-keyword-in-python-with-selenium2library/32258272 – Pekka Jun 24 '16 at 15:45

2 Answers2

2

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

ombre42
  • 2,344
  • 15
  • 21
  • Thanks,, i removed one of the selenium2libraries from my files and put my custom library inside my rf directory. but it still shows me the same error. – Michelle Jun 27 '16 at 09:59
  • Thanks,, i removed one of the selenium2libraries from my files and put my custom library inside my rf directory. but it still shows me the same error. then i decided to use get webelement(s) but i just get which is of no use to me. Let me explain that I have several widget headers which are NOT in a list but they have the same html tag. so i'm trying to get all the headers of these widgets in one time. – Michelle Jun 27 '16 at 10:06
  • when i use xpath helper plugin and write xpath helper plugin and ${webelement} Get Webelement //div[@class="rqhdrtitle"] it results in all the headers. but when i do the same thing in Ride using either ${widgets headers} Get Text //div[@class="rqhdrtitle"] or as you said ${webelement} Get Webelement //div[@class="rqhdrtitle"] i get either the first widget header of that "". any Idea? please? – Michelle Jun 27 '16 at 10:07
  • get webelement only returns the first element. use get webelementS. It returns objects that can be used instead of locators in other keywords (example added). Also you can use an object's methods or properties as in my example. When RF converts the object to a string, yes you get something with little use. – ombre42 Jun 27 '16 at 17:44
0

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

Selenium2LibraryExt.Click Element
shicky
  • 2,076
  • 5
  • 30
  • 44
  • This doesn't overcome the issue of the custom library and Selenium2Library do not share state (browser instances). – ombre42 Jun 24 '16 at 18:30