Skip to content

Commit de9d233

Browse files
authored
Creates plugin API for SeleniumLibrary (robotframework#1296)
Fixes robotframework#1292 by creating plugin API which allows a way to modify, add library keywords and modify some of the internal functionality without creating new library or hacking the source code.
1 parent b545862 commit de9d233

24 files changed

Lines changed: 647 additions & 6 deletions
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from SeleniumLibrary.base import LibraryComponent, keyword
2+
3+
4+
class FailPlugin(LibraryComponent):
5+
6+
def __init__(self, ctx):
7+
LibraryComponent.__init__(self, ctx)
8+
raise ValueError('Error in import')
9+
10+
@keyword
11+
def keyword(self):
12+
pass
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from SeleniumLibrary.base import keyword
2+
3+
@keyword
4+
def keyword():
5+
pass
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from robot.api import logger
2+
3+
from SeleniumLibrary.base import LibraryComponent, keyword
4+
from SeleniumLibrary.locators import ElementFinder
5+
6+
7+
class DummyFinder(object):
8+
9+
def __init__(self, ctx):
10+
self.ctx = ctx
11+
12+
def find(self, *args):
13+
logger.info('DummyFinder args "%s"' % str(args))
14+
logger.info('Original finder %s'
15+
% self.ctx._original_element_finder )
16+
return 'Dummy find'
17+
18+
19+
class MyPlugin(LibraryComponent):
20+
21+
def __init__(self, ctx):
22+
LibraryComponent.__init__(self, ctx)
23+
ctx._original_element_finder = ElementFinder(ctx)
24+
self.element_finder = DummyFinder(ctx)
25+
26+
@keyword
27+
def new_keyword(self):
28+
"""Adding new keyword."""
29+
self.info('New Keyword')
30+
return 'New Keyword'
31+
32+
@keyword()
33+
def open_browser(self, location):
34+
"""Overwrite existing keyword."""
35+
self.info(location)
36+
return location
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from collections import OrderedDict
2+
3+
from SeleniumLibrary.base import LibraryComponent, keyword
4+
5+
6+
class PluginWithAllArgs(LibraryComponent):
7+
8+
def __init__(self, ctx, arg, *varargs, **kwargs):
9+
LibraryComponent.__init__(self, ctx)
10+
self.arg = arg
11+
self.varargs = varargs
12+
self.kwargs = kwargs
13+
14+
@keyword
15+
def return_all_args_as_string(self):
16+
joined_str = 'start: arg=%s,' % self.arg
17+
for arg in self.varargs:
18+
joined_str = '%s %s,' % (joined_str, arg)
19+
kwargs = OrderedDict(sorted(self.kwargs.items()))
20+
for key in kwargs:
21+
joined_str = '%s %s=%s,' % (joined_str, key, kwargs[key])
22+
return joined_str[:-1]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from SeleniumLibrary.base import LibraryComponent, keyword
2+
3+
4+
class PluginWithArgs(LibraryComponent):
5+
6+
def __init__(self, ctx, arg1, arg2):
7+
LibraryComponent.__init__(self, ctx)
8+
self.arg1 = arg1
9+
self.arg2 = arg2
10+
11+
@keyword
12+
def return_arg1_arg2_as_string(self):
13+
return '%s %s' % (self.arg1, self.arg2)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from collections import OrderedDict
2+
3+
from SeleniumLibrary.base import LibraryComponent, keyword
4+
5+
6+
class PluginWithKwArgs(LibraryComponent):
7+
8+
def __init__(self, ctx, **kwargs):
9+
LibraryComponent.__init__(self, ctx)
10+
self.kwargs = kwargs
11+
12+
@keyword
13+
def return_kw_args_as_string(self):
14+
kwargs = OrderedDict(sorted(self.kwargs.items()))
15+
joined_str = 'start:'
16+
for key in kwargs:
17+
joined_str = '%s %s=%s,' % (joined_str, key, kwargs[key])
18+
return joined_str[:-1]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from SeleniumLibrary.base import LibraryComponent, keyword
2+
3+
4+
class PluginWithVarArgs(LibraryComponent):
5+
6+
def __init__(self, ctx, *args):
7+
LibraryComponent.__init__(self, ctx)
8+
self.args = args
9+
10+
@keyword
11+
def return_var_args_as_string(self):
12+
joined_str = 'start:'
13+
for arg in self.args:
14+
joined_str = '%s %s,' % (joined_str, arg)
15+
return joined_str[:-1]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*** Settings ***
2+
Library SeleniumLibrary plugins=${CURDIR}/MyPlugin.py
3+
4+
*** Test Cases ***
5+
Adding New Keyword From Class
6+
${text} = New Keyword
7+
Should Be Equal ${text} New Keyword
8+
9+
Overwriting Exsisting Keyword
10+
${text} = Open Browser text is returned
11+
Should Be Equal ${text} text is returned
12+
13+
Oerwriting ElementFinder
14+
${element} = Get WebElement //div
15+
Should Be Equal ${element} Dummy find
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*** Settings ***
2+
Library SeleniumLibrary plugins=${CURDIR}/PluginWithArgs.py;${ARG1};${ARG2},${CURDIR}/MyPlugin.py
3+
4+
*** Variables ***
5+
${ARG1} Text1
6+
${ARG2} Text2
7+
8+
*** Test Cases ***
9+
Testing Plugin With Arguments
10+
${text1} = Return Arg1 Arg2 As String
11+
Should Be Equal As Strings ${text1} Text1 Text2
12+
${text2} = New Keyword
13+
Should Be Equal As Strings ${text2} New Keyword
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*** Test Cases ***
2+
Importing SeleniumLibrary Should Fail If Plugin Is Not Found
3+
[Documentation] When importing plugin fails, the SeleniumLibrary import fails and
4+
... therefore Open Browser keyword is not found.
5+
... FAIL STARTS: Initializing test library 'SeleniumLibrary' with arguments
6+
Import Library
7+
... SeleniumLibrary
8+
... plugins=${CURDIR}/FailPlugin.py
9+
10+
11+
SeleniumLibrary Open Browser Keyword Should Not Be Found
12+
[Documentation] FAIL No keyword with name 'Open Browser' found.
13+
Open Browser
14+
... foobar
15+
... Not Here

0 commit comments

Comments
 (0)