├── README.rst ├── basics ├── README.rst ├── basics.py └── basics.robot ├── class ├── LibraryClass.py ├── README.rst ├── class.robot └── class2.robot ├── dynamic ├── DynamicLibrary.py ├── README.rst └── dynamic.robot ├── hybrid ├── HybridLibrary.py ├── README.rst └── hybrid.robot └── java └── README.rst /README.rst: -------------------------------------------------------------------------------- 1 | Robot Framework test library API examples 2 | ========================================= 3 | 4 | `Robot Framework `_ is a generic open source test 5 | automation framework. It has easy-to-use tabular test data syntax and it 6 | utilizes the keyword-driven testing approach. Its testing capabilities can be 7 | extended by test libraries implemented either with Python or Java. 8 | 9 | This project contains executable examples demonstrating different features 10 | of Robot Framework test library API. The API itself is documented thoroughly 11 | in `Robot Framework User Guide `_. 12 | 13 | **Work in progress.** 14 | -------------------------------------------------------------------------------- /basics/README.rst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robotframework/LibraryApiExamples/74583d41dbbd0e5d58d16ad664d767eebf862398/basics/README.rst -------------------------------------------------------------------------------- /basics/basics.py: -------------------------------------------------------------------------------- 1 | def keyword(): 2 | pass 3 | 4 | def argument(arg): 5 | pass 6 | 7 | def two_arguments(arg1, arg2): 8 | pass 9 | 10 | def default_values(arg1, arg2='default value', arg3='2nd default'): 11 | print '%s, %s, %s' % (arg1, arg2, arg3) 12 | 13 | def varargs(*args): 14 | print args 15 | 16 | def kwargs(**args): 17 | print args 18 | 19 | def combined(arg1, arg2=2, *varargs, **kwargs): 20 | pass 21 | 22 | def use_print(): 23 | print 'Hello, world!' 24 | 25 | import time 26 | 27 | def print_with_levels(): 28 | print '*INFO* Hello, world!' 29 | time.sleep(0.1) 30 | print '*INFO* Second message (not bold)' 31 | print '*HTML* HTML message' 32 | print '*DEBUG* Debug message' 33 | 34 | import logging 35 | 36 | def use_standard_logging(): 37 | logging.info('Hello, world!') 38 | time.sleep(0.1) 39 | logging.debug('Debug, world!') 40 | 41 | 42 | from robot.api import logger 43 | 44 | def use_robot_logger(): 45 | logger.info('Hello, world!') 46 | time.sleep(0.1) 47 | logger.debug('Debug, world!', html=True) 48 | 49 | 50 | def buggy_keyword(): 51 | not_defined 52 | 53 | 54 | def should_be_positive(number): 55 | if float(number) <= 0: 56 | raise AssertionError('%s is not positive' % number) 57 | 58 | def should_be_negative(number): 59 | if float(number) >= 0: 60 | error = AssertionError('%s is not negative' % number) 61 | error.ROBOT_CONTINUE_ON_FAILURE = True 62 | raise error 63 | 64 | 65 | def add(*numbers): 66 | return sum(float(n) for n in numbers) 67 | 68 | 69 | def get_unit(name): 70 | return Unit(3, name, 'foo') 71 | 72 | 73 | class Unit(object): 74 | 75 | def __init__(self, id, name, type): 76 | self.id = str(id) 77 | self.name = name 78 | self.type = type 79 | 80 | 81 | def _private(): 82 | pass 83 | -------------------------------------------------------------------------------- /basics/basics.robot: -------------------------------------------------------------------------------- 1 | *** Settings *** 2 | Library basics.py 3 | 4 | *** Test Cases *** 5 | Keyword name 6 | Keyword 7 | 8 | Arguments 9 | Argument arg 10 | Two Arguments a1 a2 11 | Default Values mandatory 12 | Default Values mandatory optional 1 13 | Default Values mandatory optional 1 optional 2 14 | Default Values mandatory arg3=optional 2 15 | Varargs 16 | Varargs 1 17 | Varargs 1 2 3 4 5 18 | Kwargs a=1 b=2 c=3 19 | Combined required 20 | Combined required foo=bar 21 | Combined required optional hello=world 22 | Combined required optional 1 2 3 23 | 24 | Logging 25 | Use print 26 | Print with levels 27 | Use standard logging 28 | Use Robot logger 29 | 30 | Traceback 31 | Buggy keyword 32 | 33 | Failures 34 | Should be positive 1 35 | Should be positive ${42} 36 | Should be positive 0 37 | 38 | Continuable failures 39 | Should be negative -1 40 | Should be negative 0 41 | Should be negative 1 42 | 43 | Return values 44 | ${result} = Add 1 2 45 | ${result} = Add ${result} 4 5 46 | Should be equal ${result} ${12} 47 | 48 | Object as return value 49 | ${unit} = Get Unit xyz 50 | Should be equal ${unit.name} xyz 51 | Should be equal ${unit.type} foo 52 | Should be equal ${unit.id} 3 53 | 54 | -------------------------------------------------------------------------------- /class/LibraryClass.py: -------------------------------------------------------------------------------- 1 | class LibraryClass(object): 2 | """Documentation for this library. 3 | 4 | This is shown only in docs generated by Libdoc. 5 | 6 | = Section = 7 | 8 | We can have sections here. 9 | 10 | == Sub section == 11 | 12 | And sub sections too. 13 | """ 14 | 15 | def __init__(self): 16 | self.result = None 17 | 18 | def keyword(self, arg): 19 | """Example keyword. 20 | 21 | Given ``arg`` is just printed i.e. _logged_. 22 | """ 23 | print arg 24 | 25 | def add(self, *numbers): 26 | """Adds given numbers. 27 | 28 | Sets result internally. It can be verified with `Result Should Be`. 29 | 30 | Numbers can be given also as strings, they are converted to 31 | floats. 32 | 33 | Example: 34 | | Add | 1 | 2 | 35 | | Result Should Be | 3 | | 36 | 37 | For more useless information, see `section` and `sub section`. 38 | """ 39 | self.result = sum(float(num) for num in numbers) 40 | 41 | def result_should_be(self, expected): 42 | """Verifies result of previous `Add` keyword.""" 43 | if self.result != float(expected): 44 | raise AssertionError('%s != %s' % (self.result, expected)) 45 | 46 | 47 | class TestSuiteScope(object): 48 | ROBOT_LIBRARY_SCOPE = 'TEST SUITE' 49 | 50 | def __init__(self, default_result=None): 51 | self.result = float(default_result) if default_result else None 52 | 53 | def add_2(self, *numbers): 54 | self.result = sum(float(num) for num in numbers) 55 | 56 | def result_should_be_2(self, expected): 57 | if self.result != float(expected): 58 | raise AssertionError('%s != %s' % (self.result, expected)) 59 | 60 | -------------------------------------------------------------------------------- /class/README.rst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robotframework/LibraryApiExamples/74583d41dbbd0e5d58d16ad664d767eebf862398/class/README.rst -------------------------------------------------------------------------------- /class/class.robot: -------------------------------------------------------------------------------- 1 | *** Settings *** 2 | Library LibraryClass 3 | Library LibraryClass.TestSuiteScope 4 | 5 | *** Test Cases *** 6 | Keyword name and args 7 | Keyword arg 8 | 9 | State 10 | Add 1 2 11 | Result should be 3 12 | Result should be 2 13 | 14 | Test case scope 15 | Result should be 3 16 | 17 | Test suite scope 1a 18 | Add 2 1 2 19 | Result should be 2 3 20 | 21 | Test suite scope 1b 22 | Result should be 2 3 23 | -------------------------------------------------------------------------------- /class/class2.robot: -------------------------------------------------------------------------------- 1 | *** Settings *** 2 | Library LibraryClass.TestSuiteScope 7 3 | 4 | *** Test Cases *** 5 | Test suite scope 2 6 | Result should be 2 3 7 | 8 | -------------------------------------------------------------------------------- /dynamic/DynamicLibrary.py: -------------------------------------------------------------------------------- 1 | class DynamicLibrary(object): 2 | 3 | def get_keyword_names(self): 4 | return ['Keyword', 'Another Keyword', 'kwargs'] 5 | 6 | def run_keyword(self, name, args, kwargs=None): 7 | print 'Running keyword', name, 'with arguments', args, kwargs 8 | 9 | def get_keyword_arguments(self, name): 10 | if name == 'Another Keyword': 11 | return ['arg1', 'arg2=default', 'arg3=default 2'] 12 | if name == 'kwargs': 13 | return ['**kws'] 14 | return None 15 | 16 | def get_keyword_documentation(self, name): 17 | if name == '__intro__': 18 | return 'Overall library doc here.' 19 | if name == 'Another Keyword': 20 | return 'The doc for this keyword.\n\nMainly useful w/ Libdoc.' 21 | return None 22 | -------------------------------------------------------------------------------- /dynamic/README.rst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robotframework/LibraryApiExamples/74583d41dbbd0e5d58d16ad664d767eebf862398/dynamic/README.rst -------------------------------------------------------------------------------- /dynamic/dynamic.robot: -------------------------------------------------------------------------------- 1 | *** Settings *** 2 | Library DynamicLibrary 3 | 4 | *** Test Cases *** 5 | Example 6 | Keyword 7 | Keyword arg1 arg2 8 | 9 | Argument spec 10 | Another keyword first 11 | Another keyword first second third 12 | Another keyword mandatory arg3=given 13 | 14 | Kwargs 15 | Kwargs a=1 b=2 c=3 16 | -------------------------------------------------------------------------------- /hybrid/HybridLibrary.py: -------------------------------------------------------------------------------- 1 | class HybridLibrary(object): 2 | KEYWORDS = ['Keyword', 'Another Keyword', 'kwargs'] 3 | 4 | def get_keyword_names(self): 5 | return self.KEYWORDS 6 | 7 | def Keyword(self, *args): 8 | pass 9 | 10 | def NotKeyword(self): 11 | pass 12 | 13 | def __getattr__(self, name): 14 | if name not in self.KEYWORDS: 15 | raise AttributeError(name) 16 | def keyword(*args, **kws): 17 | print 'Running keyword', name, 'with args', args, kws 18 | return keyword 19 | -------------------------------------------------------------------------------- /hybrid/README.rst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robotframework/LibraryApiExamples/74583d41dbbd0e5d58d16ad664d767eebf862398/hybrid/README.rst -------------------------------------------------------------------------------- /hybrid/hybrid.robot: -------------------------------------------------------------------------------- 1 | *** Settings *** 2 | Library HybridLibrary 3 | 4 | *** Test Cases *** 5 | Example 6 | Keyword 7 | Keyword arg1 arg2 8 | 9 | Not Keyword 10 | Not Keyword 11 | 12 | Argument spec 13 | Another keyword first 14 | Another keyword first second third 15 | Another keyword mandatory arg3=given 16 | 17 | Kwargs 18 | Kwargs a=1 b=2 c=3 19 | -------------------------------------------------------------------------------- /java/README.rst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robotframework/LibraryApiExamples/74583d41dbbd0e5d58d16ad664d767eebf862398/java/README.rst --------------------------------------------------------------------------------