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 self.tlib_logger.debug("Waiting for element '%s' to be visible with locator strategy: %s" %
360 (locator_string, locator_strategy))
361 element = WebDriverWait(self._driver, timeout).\
362 until(expected_conditions.visibility_of_element_located((locator_strategy, locator_string)))
363
364 self.tlib_logger.debug("Waiting until element stops moving")
365
366 old_location = {'x': 0, 'y': 0}
367 while old_location != element.location:
368 self.tlib_logger.debug("Pop-up is still moving. Previous position: %s, current position: %s" %
369 (old_location, element.location))
370 old_location = element.location
371 sleep(0.1)
372 element = self._driver.find_element(locator_strategy, locator_string)
373
374 return element
375 except TimeoutException:
376 if error_msg is None:
377 error_msg = "Timeout while waiting for element '%s' to be visible" % locator_string
378 self.save_screenshot("[ERROR] %s" % error_msg)
379 pytest.fail(error_msg)
380
381 - def wait_for_text_to_be_present_in_element(self, locator_strategy, locator_string, text,
382 error_msg=None, timeout=10):
383 """
384 Wait for an element that contains specified text
385 @param locator_strategy: Location strategy to use
386 @type locator_strategy: By
387 @param locator_string: String used to locate element
388 @type locator_string: str
389 @param error_msg: Error string to show if element is not found
390 @type error_msg: str
391 @param timeout: Maximum time in seconds to wait
392 @type timeout: int
393 """
394 try:
395 WebDriverWait(self._driver, timeout).\
396 until(expected_conditions.text_to_be_present_in_element((locator_strategy, locator_string), text))
397 except TimeoutException:
398 if error_msg is None:
399 error_msg = "Timeout while waiting for text %(text)s to be present in element '%(element)s'" % \
400 {"text": text, "element": locator_string}
401 self.save_screenshot("[ERROR] %s" % error_msg)
402 pytest.fail(error_msg)
403
404 - def wait_for_text_to_be_present_in_element_value(self, locator_strategy, locator_string, text,
405 error_msg=None, timeout=10):
406 """
407 Wait for an element's value to contain some test
408 @param locator_strategy: Location strategy to use
409 @type locator_strategy: By
410 @param locator_string: String used to locate element
411 @type locator_string: str
412 @param error_msg: Error string to show if element is not found
413 @type error_msg: str
414 @param timeout: Maximum time in seconds to wait
415 @type timeout: int
416 """
417 try:
418 WebDriverWait(self._driver, timeout).\
419 until(expected_conditions.text_to_be_present_in_element_value((locator_strategy, locator_string), text))
420 except TimeoutException:
421 if error_msg is None:
422 error_msg = "Timeout while waiting for text %(text)s to be present " \
423 "in the value of element '%(element)s'" % {"text": text, "element": locator_string}
424 self.save_screenshot("[ERROR] %s" % error_msg)
425 pytest.fail(error_msg)
426
427
428 - def get_webelement_by_link_text(self, locator_string):
429 """
430 Get the webelement by link text
431 @param locator_string: String used to locate element
432 @type locator_string: str
433 @param error_msg: Error string to show if element is not found
434 @type error_msg: str
435 """
436 try:
437 return self.browser.find_element_by_link_text(locator_string)
438 except NoSuchElementException:
439 error_msg="Could not find the link: '%s'"
440 self.save_screenshot(error_msg % locator_string)
441 pytest.fail(error_msg % locator_string)
442
444 """
445 Get the webelement by xpath
446 @param locator_string: String used to locate element
447 @type locator_string: str
448 @param error_msg: Error string to show if element is not found
449 @type error_msg: str
450 """
451 try:
452 self.wait_for_element_to_be_visible(By.XPATH, locator_string)
453 return self.browser.find_element_by_xpath(locator_string)
454 except NoSuchElementException:
455 error_msg="Could not find the xpath: '%s'"
456 self.save_screenshot(error_msg % locator_string)
457 pytest.fail(error_msg % locator_string)
458
460 """
461 Get the webelement by CSS
462 @param locator_string: String used to locate element
463 @type locator_string: str
464 @param error_msg: Error string to show if element is not found
465 @type error_msg: str
466 """
467 try:
468 self.wait_for_element_to_be_visible(By.CSS_SELECTOR, locator_string)
469 return self.browser.find_element_by_css_selector(locator_string)
470 except NoSuchElementException:
471 error_msg="Could not find css: '%s'"
472 self.save_screenshot(error_msg % locator_string)
473 pytest.fail(error_msg %locator_string)
474
476 """
477 Get the webelement list by xpath
478 @param locator_string: String used to locate element
479 @type locator_string: str
480 @param error_msg: Error string to show if element is not found
481 @type error_msg: str
482 """
483 try:
484 return self.browser.find_elements_by_xpath(locator_string)
485 except NoSuchElementException:
486 error_msg="Could not find the link: '%s'"
487 self.save_screenshot(error_msg % locator_string)
488 pytest.fail(error_msg+ " '%s'" % locator_string)
489
491 """
492 Get the webelement list by CSS
493 @param locator_string: String used to locate element
494 @type locator_string: str
495 @param error_msg: Error string to show if element is not found
496 @type error_msg: str
497 """
498 try:
499 return self.browser.find_elements_by_css_selector(locator_string)
500 except NoSuchElementException:
501 error_msg="Could not find css: '%s'"
502 self.save_screenshot(error_msg % locator_string)
503 pytest.fail(error_msg % locator_string)
504