Robot Framework SeleniumLibrary
javascript.py
Go to the documentation of this file.
1 # Copyright 2008-2011 Nokia Networks
2 # Copyright 2011-2016 Ryan Tomac, Ed Manlove and contributors
3 # Copyright 2016- Robot Framework Foundation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 import os
18 from collections import namedtuple
19 
20 from robot.utils import plural_or_not, seq2str
21 
22 from SeleniumLibrary.base import LibraryComponent, keyword
23 
24 
26 
27  js_marker = 'JAVASCRIPT'
28  arg_marker = 'ARGUMENTS'
29 
30  @keyword
31 
68  def execute_javascript(self, *code):
69  js_code, js_args = self._get_javascript_to_execute_get_javascript_to_execute(code)
70  self._js_logger_js_logger('Executing JavaScript', js_code, js_args)
71  return self.driverdriverdriver.execute_script(js_code, *js_args)
72 
73  @keyword
74 
98  def execute_async_javascript(self, *code):
99  js_code, js_args = self._get_javascript_to_execute_get_javascript_to_execute(code)
100  self._js_logger_js_logger('Executing Asynchronous JavaScript', js_code, js_args)
101  return self.driverdriverdriver.execute_async_script(js_code, *js_args)
102 
103  def _js_logger(self, base, code, args):
104  message = '%s:\n%s\n' % (base, code)
105  if args:
106  message = ('%sBy using argument%s:\n%s'
107  % (message, plural_or_not(args), seq2str(args)))
108  else:
109  message = '%sWithout any arguments.' % message
110  self.infoinfo(message)
111 
112  def _get_javascript_to_execute(self, code):
113  js_code, js_args = self._separate_code_and_args_separate_code_and_args(code)
114  if not js_code:
115  raise ValueError('JavaScript code was not found from code argument.')
116  js_code = ''.join(js_code)
117  path = js_code.replace('/', os.sep)
118  if os.path.isfile(path):
119  js_code = self._read_javascript_from_file_read_javascript_from_file(path)
120  return js_code, js_args
121 
122  def _separate_code_and_args(self, code):
123  code = list(code)
124  self._check_marker_error_check_marker_error(code)
125  index = self._get_marker_index_get_marker_index(code)
126  if self.arg_markerarg_marker not in code:
127  return code[index.js + 1:], []
128  if self.js_markerjs_marker not in code:
129  return code[0:index.arg], code[index.arg + 1:]
130  else:
131  if index.js == 0:
132  return code[index.js + 1:index.arg], code[index.arg + 1:]
133  else:
134  return code[index.js + 1:], code[index.arg + 1:index.js]
135 
136  def _check_marker_error(self, code):
137  if not code:
138  raise ValueError('There must be at least one argument defined.')
139  message = None
140  template = '%s marker was found two times in the code.'
141  if code.count(self.js_markerjs_marker) > 1:
142  message = template % self.js_markerjs_marker
143  if code.count(self.arg_markerarg_marker) > 1:
144  message = template % self.arg_markerarg_marker
145  index = self._get_marker_index_get_marker_index(code)
146  if index.js > 0 and index.arg != 0:
147  message = template % self.js_markerjs_marker
148  if message:
149  raise ValueError(message)
150 
151  def _get_marker_index(self, code):
152  Index = namedtuple('Index', 'js arg')
153  if self.js_markerjs_marker in code:
154  js = code.index(self.js_markerjs_marker)
155  else:
156  js = -1
157  if self.arg_markerarg_marker in code:
158  arg = code.index(self.arg_markerarg_marker)
159  else:
160  arg = -1
161  return Index(js=js, arg=arg)
162 
163  def _read_javascript_from_file(self, path):
164  self.infoinfo('Reading JavaScript from file <a href="file://%s">%s</a>.'
165  % (path.replace(os.sep, '/'), path), html=True)
166  with open(path) as file:
167  return file.read().strip()
def execute_async_javascript(self, *code)
Executes asynchronous JavaScript code with possible arguments.
Definition: javascript.py:98
def execute_javascript(self, *code)
Executes the given JavaScript code with possible arguments.
Definition: javascript.py:68