1 import os
2 import uuid
3
4 import pytest
5
6 from selenium import webdriver
7 from _pytest.python import FixtureRequest
8 from _pytest.mark import MarkInfo
9
10 import jinja2
11 from tlib.base import TestHelper
12 from tlib.base.PytestTester import PytestTester
13 from selenium.webdriver.support.ui import WebDriverWait
14 from selenium.webdriver.support import expected_conditions
15 from selenium.webdriver.common.by import By
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
159 guid = uuid.uuid4()
160
161 htm_file = "%(tc_name)s_%(id)s.htm" % { "tc_name": self.__class__._test_params, "id": guid }
162 relative_filename = os.path.join(self.__class__._test_case_id, htm_file)
163 full_filename = os.path.join(self.screenshot_folder(), relative_filename)
164
165 f = open(full_filename, "w")
166 try:
167 f.write(html)
168 finally:
169 f.close()
170
171
172 self.__class__._screenshot_report[self.__class__._test_case_id + " - " + request.keywords.node.name] = relative_filename
173
174 request.addfinalizer(generate_screenshots)
175
176
177 - def navigate(self, page, save_screenshot=True):
185
186
193
194
196 """
197 Saves screen shot for the current page
198 """
199
200 self._tlib_logger.debug("Saving screenshot")
201 self.wait_for_page_loaded()
202
203
204 if self.__class__._test_params is None:
205
206 test_folder = os.path.join(self.screenshot_folder(), self.__class__._test_case_id)
207 else:
208
209 test_folder = os.path.join(self.screenshot_folder(), self.__class__._test_case_id, self.__class__._test_params)
210
211 try:
212 os.makedirs(test_folder)
213 except WindowsError:
214 pass
215
216
217 i = len(os.listdir(test_folder)) + 1
218
219
220 img_filename = "%02d.jpg" % i
221 full_path = os.path.join(test_folder, img_filename)
222
223 self.__class__._screenshots[i] = {"filename": "%s/%s" % (self.__class__._test_params, img_filename),
224 "description": description}
225
226 self.browser.get_screenshot_as_file(full_path)
227
228
229 - def wait_for_page_loaded(self, timeout=10):
230 """
231 Waist until document.readyState is equal to complete
232 @type timeout: Integer
233 @param timeout: Number of seconds before timing out
234 """
235 if self.browser.execute_script("return document.readyState") == "complete":
236 self._tlib_logger.debug("Page '%s' already loaded" % self.browser.current_url)
237 return
238 else:
239 self._tlib_logger.debug("Waiting for '%s' to load" % self.browser.current_url)
240
241 condition = lambda *args: self.browser.execute_script("return document.readyState") == "complete"
242 try:
243 WebDriverWait(self.browser, timeout).until(condition)
244 except TimeoutException:
245 self.test_logger.error('Timeout while waiting for page to load')
246 pytest.fail('Timeout while waiting for page to load')
247
248 self._tlib_logger.debug("Page '%s' finished loading" % self.browser.current_url)
249
250 @pytest.fixture(scope='function', autouse=True)
252 """
253 Goes to homepage before each test, unless marker skipsetup is given
254 """
255
256 marks = {}
257 for k,v in request.node.function.__dict__.iteritems():
258 if isinstance(v, MarkInfo):
259 marks[k] = v
260
261
262 if not marks.has_key('skipsetup'):
263 self.browser.delete_all_cookies()
264 self.go_home()
265 else:
266 self.test_logger.info("Skipping setup")
267
268
270 try:
271 WebDriverWait(self.browser, timeout).until(expected_conditions.visibility_of_element_located((locator_strategy, locator_string)))
272 except TimeoutException:
273 if error_msg is None:
274 error_msg = "Timeout while waiting for element '%s' to be visible" % locator_string
275 self.save_screenshot("[ERROR] %s" % error_msg)
276 pytest.fail(error_msg)
277
278
280 try:
281 WebDriverWait(self.browser, timeout).until(expected_conditions.element_to_be_clickable((locator_strategy, locator_string)))
282 except TimeoutException:
283 if error_msg is None:
284 error_msg = "Timeout while waiting for element '%s' to be clickable" % locator_string
285 self.save_screenshot("[ERROR] %s" % error_msg)
286 pytest.fail(error_msg)
287
288
290 try:
291 WebDriverWait(self.browser, timeout).until(expected_conditions.alert_is_present((locator_strategy, locator_string)))
292 except TimeoutException:
293 if error_msg is None:
294 error_msg = "Timeout while waiting for element '%s' to be present" % locator_string
295 self.save_screenshot("[ERROR] %s" % error_msg)
296 pytest.fail(error_msg)
297
298
300 try:
301 WebDriverWait(self.browser, timeout).until(expected_conditions.element_located_to_be_selected((locator_strategy, locator_string)))
302 except TimeoutException:
303 if error_msg is None:
304 error_msg = "Timeout while waiting for element '%s' to be selected" % locator_string
305 self.save_screenshot("[ERROR] %s" % error_msg)
306 pytest.fail(error_msg)
307
308
310 try:
311 WebDriverWait(self.browser, timeout).until(expected_conditions.invisibility_of_element_located((locator_strategy, locator_string)))
312 except TimeoutException:
313 if error_msg is None:
314 error_msg = "Timeout while waiting for element '%s' to be invisible" % locator_string
315 self.save_screenshot("[ERROR] %s" % error_msg)
316 pytest.fail(error_msg)
317
318
320 try:
321 WebDriverWait(self.browser, timeout).until(expected_conditions.text_to_be_present_in_element((locator_strategy, locator_string), text))
322 except TimeoutException:
323 if error_msg is None:
324 error_msg = "Timeout while waiting for text %(text)s to be present in element '%(element)s'" % {"text": text, "element":locator_string}
325 self.save_screenshot("[ERROR] %s" % error_msg)
326 pytest.fail(error_msg)
327
328
330 try:
331 WebDriverWait(self.browser, timeout).until(expected_conditions.text_to_be_present_in_element_value((locator_strategy, locator_string), text))
332 except TimeoutException:
333 if error_msg is None:
334 error_msg = "Timeout while waiting for text %(text)s to be present in the value of element '%(element)s'" % {"text": text, "element":locator_string}
335 self.save_screenshot("[ERROR] %s" % error_msg)
336 pytest.fail(error_msg)
337