1 import os
2 import inspect
3
4 import pytest
5 import collections
6 from time import sleep
7
8 from _pytest.python import FixtureRequest
9
10 import jinja2
11 from tlib.base import TestHelper
12 from tlib.base import FileHelper
13 from tlib.base.PytestTester import PytestTester
14 from selenium.webdriver.common.by import By
15 from selenium.webdriver.support.ui import WebDriverWait
16 from selenium.webdriver.support import expected_conditions
17 from selenium.common.exceptions import TimeoutException, NoAlertPresentException, NoSuchElementException
18 from selenium.webdriver.remote.webelement import WebElement
19 from TestHelper import Singleton
20 from uuid import uuid4
119
120 @pytest.fixture(scope='function', autouse=True)
172
173 request.addfinalizer(generate_report)
174
203
204 - def wait_for_page_loaded(self, timeout=10):
205 raise NotImplementedError("This method should be implemented by derived classes")
206
208 """
209 Waist until an alert is visible
210 @type timeout: Integer
211 @param timeout: Number of seconds before timing out
212 @rtype: bool
213 """
214 def is_alert_visible():
215 try:
216
217 alert = self._driver.switch_to_alert().text
218 return True
219 except NoAlertPresentException as e:
220 return False
221
222 condition = lambda *args: is_alert_visible()
223 try:
224 WebDriverWait(self._driver, timeout).until(condition)
225 return self._driver.switch_to_alert()
226 except TimeoutException:
227 self.test_logger.error('Timeout while waiting for alert to appear')
228 pytest.fail('Timeout while waiting for alert to appear')
229
231 """
232 Wait until an element becomes visible
233 @param locator_strategy: Location strategy to use
234 @type locator_strategy: By
235 @param locator_string: String used to locate element
236 @type locator_string: str
237 @param error_msg: Error string to show if element is not found
238 @type error_msg: str
239 @param timeout: Maximum time in seconds to wait for the element to be visible
240 @type timeout: int
241 @rtype: WebElement
242 """
243 try:
244 element = WebDriverWait(self._driver, timeout).\
245 until(expected_conditions.visibility_of_element_located((locator_strategy, locator_string)))
246 return element
247 except TimeoutException:
248 if error_msg is None:
249 error_msg = "Timeout while waiting for element '%s' to be visible" % locator_string
250 self.save_screenshot("[ERROR] %s" % error_msg)
251 pytest.fail(error_msg)
252
254 """
255 Wait until an element cna be clicked
256 @param locator_strategy: Location strategy to use
257 @type locator_strategy: By
258 @param locator_string: String used to locate element
259 @type locator_string: str
260 @param error_msg: Error string to show if element is not found
261 @type error_msg: str
262 @param timeout: Maximum time in seconds to wait for the element to be clickable
263 @type timeout: int
264 @rtype: WebElement
265 """
266 try:
267 element = WebDriverWait(self._driver, timeout).\
268 until(expected_conditions.element_to_be_clickable((locator_strategy, locator_string)))
269 return element
270 except TimeoutException:
271 if error_msg is None:
272 error_msg = "Timeout while waiting for element '%s' to be clickable" % locator_string
273 self.save_screenshot("[ERROR] %s" % error_msg)
274 pytest.fail(error_msg)
275
277 """
278 Wait until an element is present
279 @param locator_strategy: Location strategy to use
280 @type locator_strategy: By
281 @param locator_string: String used to locate element
282 @type locator_string: str
283 @param error_msg: Error string to show if element is not found
284 @type error_msg: str
285 @param timeout: Maximum time in seconds to wait for the element to be present
286 @type timeout: int
287 @rtype: WebElement
288 """
289 try:
290 element = WebDriverWait(self._driver, timeout).\
291 until(expected_conditions.presence_of_element_located((locator_strategy, locator_string)))
292 return element
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
300 """
301 Wait until an element is selected
302 @param locator_strategy: Location strategy to use
303 @type locator_strategy: By
304 @param locator_string: String used to locate element
305 @type locator_string: str
306 @param error_msg: Error string to show if element is not found
307 @type error_msg: str
308 @param timeout: Maximum time in seconds to wait for the element to be selected
309 @type timeout: int
310 @rtype: WebElement
311 """
312 try:
313 element = WebDriverWait(self._driver, timeout).\
314 until(expected_conditions.element_located_to_be_selected((locator_strategy, locator_string)))
315 return element
316 except TimeoutException:
317 if error_msg is None:
318 error_msg = "Timeout while waiting for element '%s' to be selected" % locator_string
319 self.save_screenshot("[ERROR] %s" % error_msg)
320 pytest.fail(error_msg)
321
323 """
324 Wait until an element becomes invisible
325 @param locator_strategy: Location strategy to use
326 @type locator_strategy: By
327 @param locator_string: String used to locate element
328 @type locator_string: str
329 @param error_msg: Error string to show if element is not found
330 @type error_msg: str
331 @param timeout: Maximum time in seconds to wait for the element to be hidden
332 @type timeout: int
333 @rtype: WebElement
334 """
335 try:
336 element = WebDriverWait(self._driver, timeout).\
337 until(expected_conditions.invisibility_of_element_located((locator_strategy, locator_string)))
338 return element
339 except TimeoutException:
340 if error_msg is None:
341 error_msg = "Timeout while waiting for element '%s' to be invisible" % locator_string
342 self.save_screenshot("[ERROR] %s" % error_msg)
343 pytest.fail(error_msg)
344
346 """
347 Wait until an element that moves on the screen stops moving
348 @param locator_strategy: Location strategy to use
349 @type locator_strategy: By
350 @param locator_string: String used to locate element
351 @type locator_string: str
352 @param error_msg: Error string to show if element is not found
353 @type error_msg: str
354 @param timeout: Maximum time in seconds to wait for the element to be visible
355 @type timeout: int
356 @rtype: WebElement
357 """
358 try:
359 element = WebDriverWait(self._driver, timeout).\
360 until(expected_conditions.visibility_of_element_located((locator_strategy, locator_string)))
361
362
363 old_location = {'x': 0, 'y': 0}
364 while old_location != element.location:
365 self.tlib_logger.debug("Pop-up is still moving. Previous position: %s, current position: %s" %
366 (old_location, element.location))
367 old_location = element.location
368 sleep(0.1)
369 element = self._driver.find_element(locator_strategy, locator_string)
370
371 return element
372 except TimeoutException:
373 if error_msg is None:
374 error_msg = "Timeout while waiting for element '%s' to be visible" % locator_string
375 self.save_screenshot("[ERROR] %s" % error_msg)
376 pytest.fail(error_msg)
377
378 - def wait_for_text_to_be_present_in_element(self, locator_strategy, locator_string, text,
379 error_msg=None, timeout=10):
380 """
381 Wait for an element that contains specified text
382 @param locator_strategy: Location strategy to use
383 @type locator_strategy: By
384 @param locator_string: String used to locate element
385 @type locator_string: str
386 @param error_msg: Error string to show if element is not found
387 @type error_msg: str
388 @param timeout: Maximum time in seconds to wait
389 @type timeout: int
390 """
391 try:
392 WebDriverWait(self._driver, timeout).\
393 until(expected_conditions.text_to_be_present_in_element((locator_strategy, locator_string), text))
394 except TimeoutException:
395 if error_msg is None:
396 error_msg = "Timeout while waiting for text %(text)s to be present in element '%(element)s'" % \
397 {"text": text, "element": locator_string}
398 self.save_screenshot("[ERROR] %s" % error_msg)
399 pytest.fail(error_msg)
400
401 - def wait_for_text_to_be_present_in_element_value(self, locator_strategy, locator_string, text,
402 error_msg=None, timeout=10):
403 """
404 Wait for an element's value to contain some test
405 @param locator_strategy: Location strategy to use
406 @type locator_strategy: By
407 @param locator_string: String used to locate element
408 @type locator_string: str
409 @param error_msg: Error string to show if element is not found
410 @type error_msg: str
411 @param timeout: Maximum time in seconds to wait
412 @type timeout: int
413 """
414 try:
415 WebDriverWait(self._driver, timeout).\
416 until(expected_conditions.text_to_be_present_in_element_value((locator_strategy, locator_string), text))
417 except TimeoutException:
418 if error_msg is None:
419 error_msg = "Timeout while waiting for text %(text)s to be present " \
420 "in the value of element '%(element)s'" % {"text": text, "element": locator_string}
421 self.save_screenshot("[ERROR] %s" % error_msg)
422 pytest.fail(error_msg)
423
424
425 - def get_webelement_by_link_text(self, locator_string):
426 """
427 Get the webelement by link text
428 @param locator_string: String used to locate element
429 @type locator_string: str
430 @param error_msg: Error string to show if element is not found
431 @type error_msg: str
432 """
433 try:
434 return self.browser.find_element_by_link_text(locator_string)
435 except NoSuchElementException:
436 error_msg="Could not find the link: '%s'"
437 self.save_screenshot(error_msg % locator_string)
438 pytest.fail(error_msg % locator_string)
439
441 """
442 Get the webelement by xpath
443 @param locator_string: String used to locate element
444 @type locator_string: str
445 @param error_msg: Error string to show if element is not found
446 @type error_msg: str
447 """
448 try:
449 self.wait_for_element_to_be_visible(By.XPATH, locator_string)
450 return self.browser.find_element_by_xpath(locator_string)
451 except NoSuchElementException:
452 error_msg="Could not find the xpath: '%s'"
453 self.save_screenshot(error_msg % locator_string)
454 pytest.fail(error_msg % locator_string)
455
457 """
458 Get the webelement by CSS
459 @param locator_string: String used to locate element
460 @type locator_string: str
461 @param error_msg: Error string to show if element is not found
462 @type error_msg: str
463 """
464 try:
465 self.wait_for_element_to_be_visible(By.CSS_SELECTOR, locator_string)
466 return self.browser.find_element_by_css_selector(locator_string)
467 except NoSuchElementException:
468 error_msg="Could not find css: '%s'"
469 self.save_screenshot(error_msg % locator_string)
470 pytest.fail(error_msg %locator_string)
471
473 """
474 Get the webelement list by xpath
475 @param locator_string: String used to locate element
476 @type locator_string: str
477 @param error_msg: Error string to show if element is not found
478 @type error_msg: str
479 """
480 try:
481 return self.browser.find_elements_by_xpath(locator_string)
482 except NoSuchElementException:
483 error_msg="Could not find the link: '%s'"
484 self.save_screenshot(error_msg % locator_string)
485 pytest.fail(error_msg+ " '%s'" % locator_string)
486
488 """
489 Get the webelement list by CSS
490 @param locator_string: String used to locate element
491 @type locator_string: str
492 @param error_msg: Error string to show if element is not found
493 @type error_msg: str
494 """
495 try:
496 return self.browser.find_elements_by_css_selector(locator_string)
497 except NoSuchElementException:
498 error_msg="Could not find css: '%s'"
499 self.save_screenshot(error_msg % locator_string)
500 pytest.fail(error_msg % locator_string)
501