Revision: 3916
Author: pekka.klarck
Date: Wed Aug 25 07:48:09 2010
Log: Get Library Instance keyword, issue 622.
http://code.google.com/p/robotframework/source/detail?r=3916

Added:
 /trunk/atest/robot/standard_libraries/builtin/get_library_instance.txt
 /trunk/atest/testdata/standard_libraries/builtin/get_library_instance.txt
Modified:
 /trunk/src/robot/libraries/BuiltIn.py
 /trunk/src/robot/running/namespace.py

=======================================
--- /dev/null
+++ /trunk/atest/robot/standard_libraries/builtin/get_library_instance.txt Wed Aug 25 07:48:09 2010
@@ -0,0 +1,31 @@
+*** Settings ***
+Suite Setup Run Tests ${EMPTY} standard_libraries/builtin/get_library_instance.txt
+Force Tags      regression
+Default Tags    pybot  jybot
+Resource        atest_resource.txt
+
+*** Test Cases ***
+
+Library imported normally
+    Check Test Case  ${TESTNAME}
+
+Module library
+    Check Test Case  ${TESTNAME}
+
+Java library
+    [Tags]  jybot
+    Check Test Case  ${TESTNAME}
+
+Library with alias
+    Check Test Case  ${TESTNAME}
+
+`Import Library` keyword
+    Check Test Case  ${TESTNAME}
+
+Non-existing library
+    Check Test Case  ${TESTNAME}
+
+Library scopes
+    Check Test Case  ${TESTNAME} 1
+    Check Test Case  ${TESTNAME} 2
+
=======================================
--- /dev/null
+++ /trunk/atest/testdata/standard_libraries/builtin/get_library_instance.txt Wed Aug 25 07:48:09 2010
@@ -0,0 +1,67 @@
+*** Settings ***
+Library  OperatingSystem
+Library  module_library
+Library  ExampleJavaLibrary
+Library  ParameterLibrary  first   WITH NAME  1st
+Library  ParameterLibrary  second  WITH NAME  2nd
+Library  libraryscope.Test
+Library  libraryscope.Suite
+Library  libraryscope.Global
+
+
+*** Test Cases ***
+
+Library imported normally
+    ${lib} =  Get Library Instance  BuiltIn
+    Should Be Equal  ${lib.convert_to_integer('42')}  ${42}
+    ${lib} =  Get Library Instance  Operating System
+    Should Not Be Empty  ${lib.list_directory('.')}
+
+Module library
+    ${lib} =  Get Library Instance  module_library
+    Should Be Equal  ${lib.returning()}  Hello from module library
+
+Java library
+    ${lib} =  Get Library Instance  ExampleJavaLibrary
+    Should Be Equal  ${lib.getCount()}  ${1}
+    Should Be Equal  ${lib.getCount()}  ${2}
+    Should Be Equal  ${lib.getCount()}  ${3}
+
+Library with alias
+    [Documentation]  FAIL  No library with name 'ParameterLibrary' found.
+    ${lib} =  Get Library Instance  1st
+    Should Be Equal  ${lib.parameters()[0]}  first
+    ${lib} =  Get Library Instance  2nd
+    Should Be Equal  ${lib.parameters()[0]}  second
+    Get Library Instance  ParameterLibrary
+
+`Import Library` keyword
+    Import Library  String
+    ${lib} =  Get Library Instance  String
+    Should Be Equal  ${lib.replace_string('Hello', 'e', 'i')}  Hillo
+
+Non-existing library
+    [Documentation]  FAIL  No library with name 'NonExisting' found.
+    Get Library Instance  NonExisting
+
+Library scopes 1
+    ${test} =  Get Library Instance  libraryscope.Test
+    ${suite} =  Get Library Instance  libraryscope.Suite
+    ${global} =  Get Library Instance  libraryscope.Global
+    Log  ${test.register('Test 1')}
+    Log  ${suite.register('Suite 1')}
+    Log  ${global.register('Global 1')}
+    Log  ${test.should_be_registered('Test 1')}
+    Log  ${suite.should_be_registered('Suite 1')}
+    Log  ${global.should_be_registered('Global 1')}
+
+Library scopes 2
+    ${test} =  Get Library Instance  libraryscope.Test
+    ${suite} =  Get Library Instance  libraryscope.Suite
+    ${global} =  Get Library Instance  libraryscope.Global
+    Log  ${test.register('Test 2')}
+    Log  ${suite.register('Suite 2')}
+    Log  ${global.register('Global 2')}
+    Log  ${test.should_be_registered('Test 2')}
+    Log  ${suite.should_be_registered('Suite 1', 'Suite 2')}
+    Log  ${global.should_be_registered('Global 1', 'Global 2')}
=======================================
--- /trunk/src/robot/libraries/BuiltIn.py       Wed Aug 25 05:33:12 2010
+++ /trunk/src/robot/libraries/BuiltIn.py       Wed Aug 25 07:48:09 2010
@@ -1525,6 +1525,30 @@
         else:
             test.tags = handler(test)

+    def get_library_instance(self, name):
+ """Returns the currently active instance of the specified test library.
+
+        This keyword makes it easy for test libraries to interact with
+        other test libraries that have state. This is illustrated by
+        the Python example below:
+
+        | 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))
+
+        It is also possible to use this keyword in the test data and
+        pass the returned library instance to another keyword. If a
+        library is imported with a custom name, the `name` used to get
+        the instance must be that name and not the original library
+        name.
+        """
+        return NAMESPACES.current.get_library_instance(name)
+

 class BuiltIn(_Verify, _Converter, _Variables, _RunKeyword, _Misc):
     """An always available standard library with often needed keywords.
=======================================
--- /trunk/src/robot/running/namespace.py       Tue Aug 24 03:42:08 2010
+++ /trunk/src/robot/running/namespace.py       Wed Aug 25 07:48:09 2010
@@ -225,7 +225,10 @@
         self.uk_handlers.pop()

     def get_library_instance(self, libname):
-        return self._testlibs[libname].get_instance()
+        try:
+            return self._testlibs[libname.replace(' ', '')].get_instance()
+        except KeyError:
+            raise DataError("No library with name '%s' found." % libname)

     def get_handler(self, name):
         try:

Reply via email to