1 import os
2 import base64
3 import uuid
4
5 import pytest
6
7 from selenium import webdriver
8 from _pytest.python import FixtureRequest
9 from _pytest.mark import MarkInfo
10
11 import jinja2
12 from tlib.base import TestHelper
13 from tlib.base.PytestTester import PytestTester
14 from selenium.webdriver.support.ui import WebDriverWait
15 from selenium.webdriver.support import expected_conditions
16 from selenium.common.exceptions import TimeoutException
17 import logging
130
131
132 @pytest.fixture(scope='function', autouse=True)
134 """
135 Generates HTML page with screenshots for each test
136 @type request: FixtureRequest
137 """
138
139 self.__class__._test_params = request.keywords.node._genid
140 self.__class__._screenshots = {}
141
142
143 marker = request.node.get_marker("testcasename")
144 if marker is None:
145 self._test_logger.warn("Test case doesn't have marker testcasename")
146 self.__class__._test_case_id = "UNKNOWN_TEST_CASE_ID"
147 else:
148 self.__class__._test_case_id = marker.args[0]
149
150
151 def generate_screenshots():
152 if len(self.__class__._screenshots) > 0:
153
154 html = self.__class__._tc_screenshot_template.render(test_case_id=self.__class__._test_case_id,
155 test_case_name=request.keywords.node.name,
156 screenshots=self.__class__._screenshots)
157
158 htm_file = "%(id)s.htm" % { "id": uuid.uuid4() }
159 relative_filename = os.path.join(self.__class__._test_case_id, htm_file)
160 full_filename = os.path.join(self.screenshot_folder(), relative_filename)
161
162 f = open(full_filename, "w")
163 try:
164 f.write(html)
165 finally:
166 f.close()
167
168
169 self.__class__._screenshot_report[self.__class__._test_case_id + " - " + request.keywords.node.name] = relative_filename
170
171 request.addfinalizer(generate_screenshots)
172
173
174 - def navigate(self, page, save_screenshot=True):
182
183
190
191
193 """
194 Saves screen shot for the current page
195 """
196
197 def get_params():
198 if self.__class__._test_params is None:
199 return ""
200 else:
201 return self.__class__._test_params
202
203
204 self._tlib_logger.debug("Saving screenshot")
205 self.wait_for_page_loaded()
206
207
208
209 rndstr = base64.urlsafe_b64encode(".".join([self.__class__._test_case_id, get_params()]))
210 test_folder = os.path.join(self.screenshot_folder(), self.__class__._test_case_id, rndstr)
211
212 try:
213 os.makedirs(test_folder)
214 except WindowsError:
215 pass
216
217
218 i = len(os.listdir(test_folder)) + 1
219
220
221 img_filename = "%02d.jpg" % i
222 full_path = os.path.join(test_folder, img_filename)
223
224 self.__class__._screenshots[i] = {"filename": "%s/%s" % (rndstr, img_filename),
225 "description": description}
226
227 self.browser.get_screenshot_as_file(full_path)
228
229
230 - def wait_for_page_loaded(self, timeout=10):
231 """
232 Waist until document.readyState is equal to complete
233 @type timeout: Integer
234 @param timeout: Number of seconds before timing out
235 """
236 if self.browser.execute_script("return document.readyState") == "complete":
237 self._tlib_logger.debug("Page '%s' already loaded" % self.browser.current_url)
238 return
239 else:
240 self._tlib_logger.debug("Waiting for '%s' to load" % self.browser.current_url)
241
242 condition = lambda *args: self.browser.execute_script("return document.readyState") == "complete"
243 try:
244 WebDriverWait(self.browser, timeout).until(condition)
245 except TimeoutException:
246 self.test_logger.error('Timeout while waiting for page to load')
247 pytest.fail('Timeout while waiting for page to load')
248
249 self._tlib_logger.debug("Page '%s' finished loading" % self.browser.current_url)
250
251 @pytest.fixture(scope='function', autouse=True)
253 """
254 Goes to homepage before each test, unless marker skipsetup is given
255 """
256
257 marks = {}
258 for k,v in request.node.function.__dict__.iteritems():
259 if isinstance(v, MarkInfo):
260 marks[k] = v
261
262
263 if not marks.has_key('skipsetup'):
264 self.browser.delete_all_cookies()
265 self.go_home()
266 else:
267 self.test_logger.info("Skipping setup")
268
269
271 try:
272 WebDriverWait(self.browser, timeout).until(expected_conditions.visibility_of_element_located((locator_strategy, locator_string)))
273 except TimeoutException:
274 if error_msg is None:
275 error_msg = "Timeout while waiting for element '%s' to be visible" % locator_string
276 self.save_screenshot("[ERROR] %s" % error_msg)
277 pytest.fail(error_msg)
278
279
281 try:
282 WebDriverWait(self.browser, timeout).until(expected_conditions.element_to_be_clickable((locator_strategy, locator_string)))
283 except TimeoutException:
284 if error_msg is None:
285 error_msg = "Timeout while waiting for element '%s' to be clickable" % locator_string
286 self.save_screenshot("[ERROR] %s" % error_msg)
287 pytest.fail(error_msg)
288
289
291 try:
292 WebDriverWait(self.browser, timeout).until(expected_conditions.alert_is_present((locator_strategy, locator_string)))
293 except TimeoutException:
294 if error_msg is None:
295 error_msg = "Timeout while waiting for element '%s' to be present" % locator_string
296 self.save_screenshot("[ERROR] %s" % error_msg)
297 pytest.fail(error_msg)
298
299
301 try:
302 WebDriverWait(self.browser, timeout).until(expected_conditions.element_located_to_be_selected((locator_strategy, locator_string)))
303 except TimeoutException:
304 if error_msg is None:
305 error_msg = "Timeout while waiting for element '%s' to be selected" % locator_string
306 self.save_screenshot("[ERROR] %s" % error_msg)
307 pytest.fail(error_msg)
308
309
311 try:
312 WebDriverWait(self.browser, timeout).until(expected_conditions.invisibility_of_element_located((locator_strategy, locator_string)))
313 except TimeoutException:
314 if error_msg is None:
315 error_msg = "Timeout while waiting for element '%s' to be invisible" % locator_string
316 self.save_screenshot("[ERROR] %s" % error_msg)
317 pytest.fail(error_msg)
318
319
321 try:
322 WebDriverWait(self.browser, timeout).until(expected_conditions.text_to_be_present_in_element((locator_strategy, locator_string), text))
323 except TimeoutException:
324 if error_msg is None:
325 error_msg = "Timeout while waiting for text %(text)s to be present in element '%(element)s'" % {"text": text, "element":locator_string}
326 self.save_screenshot("[ERROR] %s" % error_msg)
327 pytest.fail(error_msg)
328
329
331 try:
332 WebDriverWait(self.browser, timeout).until(expected_conditions.text_to_be_present_in_element_value((locator_strategy, locator_string), text))
333 except TimeoutException:
334 if error_msg is None:
335 error_msg = "Timeout while waiting for text %(text)s to be present in the value of element '%(element)s'" % {"text": text, "element":locator_string}
336 self.save_screenshot("[ERROR] %s" % error_msg)
337 pytest.fail(error_msg)
338