|
| 1 | +from selenium import webdriver |
| 2 | +from selenium.webdriver.chrome.options import Options |
| 3 | +from selenium.webdriver.common.by import By |
| 4 | +from selenium.webdriver.support.ui import WebDriverWait |
| 5 | +from selenium.webdriver.support.wait import TimeoutException |
| 6 | +from selenium.webdriver.support import expected_conditions as EC |
| 7 | + |
| 8 | +import chromedriver_binary |
| 9 | +import logging as log |
| 10 | +import os |
| 11 | + |
| 12 | +logging = log.getLogger(__name__) |
| 13 | + |
| 14 | +WAIT = 10 |
| 15 | + |
| 16 | +def getBrowser(headless=True): |
| 17 | + if os.environ.get("DEBUG") is not None: |
| 18 | + logging.info("Headless mode deactivated") |
| 19 | + headless = False |
| 20 | + |
| 21 | + options = Options() |
| 22 | + if headless is True: |
| 23 | + options.add_argument('--headless') |
| 24 | + options.add_argument('--no-sandbox') |
| 25 | + options.add_argument('--disable-gpu') |
| 26 | + |
| 27 | + driver = webdriver.Chrome(options=options) |
| 28 | + driver.set_page_load_timeout(WAIT * 2) |
| 29 | + return driver |
| 30 | + |
| 31 | +def waitElementByCondition(browser, condition, searchMethod, searchString, errorMessage, time=WAIT): |
| 32 | + try: |
| 33 | + element = WebDriverWait(browser, time).until( |
| 34 | + condition((searchMethod, searchString)) |
| 35 | + ) |
| 36 | + except TimeoutException as e: |
| 37 | + logging.error(f"Element {searchString} {errorMessage}") |
| 38 | + return False |
| 39 | + return True |
| 40 | + |
| 41 | + |
| 42 | +def waitElementIsVisibleById(browser, elementId, time=WAIT): |
| 43 | + return waitElementByCondition(browser, |
| 44 | + EC.visibility_of_element_located, |
| 45 | + By.ID, |
| 46 | + elementId, |
| 47 | + "is not visibile", |
| 48 | + time) |
| 49 | + |
| 50 | +def waitElementIsVisibleByXpath(browser, xpath): |
| 51 | + return waitElementByCondition(browser, |
| 52 | + EC.visibility_of_element_located, |
| 53 | + By.XPATH, |
| 54 | + xpath, |
| 55 | + "is not visibile") |
| 56 | + |
| 57 | +def waitElementIsClickableById(browser, elementId): |
| 58 | + return waitElementByCondition(browser, |
| 59 | + EC.element_to_be_clickable, |
| 60 | + By.ID, |
| 61 | + elementId, |
| 62 | + "is not clickable") |
| 63 | + |
| 64 | +def waitElementIsClickableByXpath(browser, xpath): |
| 65 | + return waitElementByCondition(browser, |
| 66 | + EC.element_to_be_clickable, |
| 67 | + By.XPATH, |
| 68 | + xpath, |
| 69 | + "is not clickable") |
| 70 | + |
| 71 | +def clickIfElementIsVisibleByXpath(browser, xpath): |
| 72 | + try: |
| 73 | + assert(waitElementIsVisibleByXpath(browser, xpath)) |
| 74 | + assert(waitElementIsClickableByXpath(browser, xpath)) |
| 75 | + browser.find_element_by_xpath(xpath).click() |
| 76 | + |
| 77 | + except Exception as e: |
| 78 | + logging.error(f"Cannot click on element {xpath}: {e}") |
| 79 | + return False |
| 80 | + |
| 81 | + return True |
| 82 | + |
| 83 | +def clickIfElementIsVisibleById(browser, elementId): |
| 84 | + try: |
| 85 | + assert(waitElementIsVisibleById(browser, elementId)) |
| 86 | + assert(waitElementIsClickableById(browser, elementId)) |
| 87 | + browser.find_element_by_id(elementId).click() |
| 88 | + |
| 89 | + except Exception as e: |
| 90 | + logging.error(f"Cannot click on element {elementId}: {e}") |
| 91 | + return False |
| 92 | + |
| 93 | + return True |
| 94 | + |
| 95 | +def fillTextIfElementIsVisibleById(browser, elementId, text): |
| 96 | + try: |
| 97 | + assert(waitElementIsVisibleById(browser, elementId)) |
| 98 | + browser.find_element_by_id(elementId).send_keys(text) |
| 99 | + |
| 100 | + except Exception as e: |
| 101 | + logging.error(f"Cannot type {text} on element {elementId}: {e}") |
| 102 | + return False |
| 103 | + |
| 104 | + return True |
| 105 | + |
0 commit comments