1

Being new to the framework, I couldn't understand the difference if I create a keyword using 'Get Library Instance' in RF. Please explain with an example or any reference document. It's a bit confusing for me.

I have already visited: BuiltIn.Get Library Instance

Edited: Referring to the above link, I can see that the custom method uses "BuiltIn.Get Library Instance" to find the title on a page. So, what is the difference if I write my own keyword in Robot Framework using Get Title having same functionality as title_should_start_with or writing the same method in python using Inheritance as explained 1) here and 2) here.

code:
1) Using Inheritance

from SeleniumLibrary import SeleniumLibrary
class ExtendedSeleniumLibrary(SeleniumLibrary):
def title_should_start_with(self, expected):
        title = self.get_title()
        if not title.startswith(expected):
            raise AssertionError("Title '%s' did not start with '%s'"
                                 % (title, expected))

2) Using get_library_instance

from robot.libraries.BuiltIn import BuiltIn
def title_should_start_with(expected):
    seleniumlib = BuiltIn().get_library_instance('SeleniumLibrary')
    title = seleniumlib.get_title()
    if not title.startswith(expected):
        raise AssertionError("Title '%s' did not start with '%s'"
                             % (title, expected))

3) RF keyword

*** Settings ***
Library    SeleniumLibrary
*** keywords ***
Verify Title
    ${title}  Get Title
    .
    .
nik
  • 85
  • 2
  • 13
  • Can you describe what it is you want to achieve? The description of the issue you're facing doesn't explain this to me. Working with a Library Instance is a means to an end. Please describe the latter so we can craft a soltution that best suits your end goal. – A. Kootstra Apr 09 '18 at 11:51
  • What do you mean by "create a keyword using 'Get Library Instance'? `Get Library instance` doesn't create any keywords. Also, you ask what's the difference, but didn't say what you're comparing it to. What's the difference between `Get Library Instance` and .... what? – Bryan Oakley Apr 09 '18 at 13:14
  • @BryanOakley Hi, by suing the term "create a keyword using" I meant ..What if I create a custom keyword and use `Get Library Instance` keyword inside it. I have edit my question for better understanding. Thanks. – nik Apr 10 '18 at 04:33
  • This forum is powered by volunteers, spending spare (private) time to answer questions when they are able. They are not paid to do so and as such should not be directed to do so. – A. Kootstra Apr 11 '18 at 07:21

3 Answers3

0

This looks more of a design question to me rather than of implementation. The answer lies in the concepts of modular and reusable design. Do not re-invent if it is already available.

The example cited in 'BuiltIn.Get Library Instance' documentation is perhaps simplistic but if you are using RF and want to get the title of a page, why not reuse the API provided by SeleniumLibrary?

Anshuman Manral
  • 609
  • 8
  • 16
  • @Ashauman, I think I have made it clear with my edits, the, difference between three i.e: writing the same method without using `Get Library Instance` in python and writing a keyword which uses `Get Title` inside RF. – nik Apr 11 '18 at 04:05
0

The same question was posted in Robot Framework user group and my answer can be found from there: https://groups.google.com/forum/#!msg/robotframework-users/Ui3lWPMu8pI/l7hGeb1QBAAJ

Tatu
  • 9
  • 1
0

In case of Inheritance code can run standalone without the need for robot-framework (this assumes SeleniumLibrary doesn't require robot-framework).

In case of get_library_instance we can use the same instance of the SeleniumLibrary in our module. That means we can perform preconditions with robot-framework (like opening the browser), then call our keyword which retrieves the instance created by robot-framework when the SeleniumLibrary was first instanced. Also, the code will work only if it is being called by robot-framework, as it will require its execution context.
Example here explains the use of "Inheritance" and "Instance". Please read from beginning till the end.

Also, the real finding is, it more depends how we want our library to work. If we want to create our own library in a such way that keywords do not collide with the SeleniumLibrary then it can be easily done with getting the SeleniumLibrary instance.
The decision has to be taken if we want the keywords to be directly library keywords or want them to be more like user keywords, which are based on library keywords. Once that is somewhat clear, then it will lead to the implementation details, like using inheritance or getting active library instance.

Consolidated the responses. Reference

nik
  • 85
  • 2
  • 13