forked from boakley/robotframework-pageobjectlibrary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keywords.py
176 lines (127 loc) · 6.47 KB
/
keywords.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""PageObjectLibrary
A library to support the creation of page objects using
selenium and Seleniuim2Library.
"""
from __future__ import print_function, absolute_import, unicode_literals
import six
import robot.api
from robot.libraries.BuiltIn import BuiltIn
from .pageobject import PageObject
try:
from urlparse import urlparse
except ImportError:
from urllib.parse import urlparse
class PageObjectLibraryKeywords(object):
ROBOT_LIBRARY_SCOPE = "TEST SUITE"
def __init__(self):
self.builtin = BuiltIn()
self.logger = robot.api.logger
@property
def se2lib(self):
# this is implemented as a property so that this class
# can be imported outside the context of a running
# suite (ie: by libdoc, robotframework-hub, etc)
try:
se2 = self.builtin.get_library_instance("Selenium2Library")
except RuntimeError:
self.builtin.import_library("Selenium2Library")
se2 = self.builtin.get_library_instance("Selenium2Library")
return se2
def the_current_page_should_be(self, page_name):
"""Fails if the name of the current page is not the given page name
``page_name`` is the name you would use to import the page.
This keyword will import the given page object, put it at the
front of the Robot library search order, then call the method
``_is_current_page`` on the library. The default
implementation of this method will compare the page title to
the ``PAGE_TITLE`` attribute of the page object, but this
implementation can be overridden by each page object.
"""
page = self._get_page_object(page_name)
# This causes robot to automatically resolve keyword
# conflicts by looking in the current page first.
if page._is_current_page():
# only way to get the current order is to set a
# new order. Once done, if there actually was an
# old order, preserve the old but make sure our
# page is at the front of the list
old_order = self.builtin.set_library_search_order()
new_order = ([str(page)],) + old_order
self.builtin.set_library_search_order(new_order)
return
# If we get here, we're not on the page we think we're on
raise Exception("Expected page to be %s but it was not" % page_name)
def go_to_page(self, page_name, page_root = None):
"""Go to the url for the given page object.
Unless explicitly provided, the URL root will be based on the
root of the current page. For example, if the current page is
http://www.example.com:8080 and the page object URL is
``/login``, the url will be http://www.example.com:8080/login
== Example ==
Given a page object named ``ExampleLoginPage`` with the URL
``/login``, and a browser open to ``http://www.example.com``, the
following statement will go to ``http://www.example.com/login``,
and place ``ExampleLoginPage`` at the front of Robot's library
search order.
| Go to Page ExampleLoginPage
The effect is the same as if you had called the following three
keywords:
| Selenium2Library.Go To http://www.example.com/login
| Import Library ExampleLoginPage
| Set Library Search Order ExampleLoginPage
Tags: selenium, page-object
"""
page = self._get_page_object(page_name)
if page.IS_DYNAMIC:
raise Exception("go_to_page method can not be called for dynamic page, use go_to_dynamic_page instead")
url = page_root if page_root is not None else self.se2lib.get_location()
(scheme, netloc, path, parameters, query, fragment) = urlparse(url)
url = "%s://%s%s" % (scheme, netloc, page.PAGE_URL)
with page._wait_for_page_refresh():
# self.logger.console("\ntrying to go to '%s'" % url) # <-- Remove hashmark if you want this message on console
self.se2lib.go_to(url)
# should I be calling this keyword? Should this keyword return
# true/false, or should it throw an exception?
self.the_current_page_should_be(page_name)
def go_to_dynamic_page(self, page_name, *args):
"""Go to the url for the given page object.
Unless explicitly provided, the URL root will be based on the
root of the current page. For example, if the current page is
http://www.example.com:8080 and the page object URL is
``/login``, the url will be http://www.example.com:8080/login
== Example ==
Given a page object named ``ExampleLoginPage`` with the URL
``/login``, and a browser open to ``http://www.example.com``, the
following statement will go to ``http://www.example.com/login``,
and place ``ExampleLoginPage`` at the front of Robot's library
search order.
| Go to Page ExampleLoginPage
The effect is the same as if you had called the following three
keywords:
| Selenium2Library.Go To http://www.example.com/login
| Import Library ExampleLoginPage
| Set Library Search Order ExampleLoginPage
Tags: selenium, page-object
"""
page = self._get_page_object(page_name)
if page.IS_DYNAMIC == False:
raise Exception("go_to_dynamic_page method can not be called for static page, use go_to_page instead")
url = self.se2lib.get_location()
(scheme, netloc, path, parameters, query, fragment) = urlparse(url)
url = "%s://%s%s" % (scheme, netloc, page.PAGE_URL % tuple(args))
with page._wait_for_page_refresh():
# self.logger.console("\ntrying to go to '%s'" % url) # <-- Remove hashmark if you want this message on console
self.se2lib.go_to(url)
# should I be calling this keyword? Should this keyword return
# true/false, or should it throw an exception?
self.the_current_page_should_be(page_name)
def _get_page_object(self, page_name):
"""Import the page object if necessary, then return the handle to the library
Note: If the page object has already been imported, it won't be imported again.
"""
try:
page = self.builtin.get_library_instance(page_name)
except RuntimeError:
self.builtin.import_library(page_name)
page = self.builtin.get_library_instance(page_name)
return page