|
1 | 1 | from selenium import webdriver |
2 | 2 | from selenium.common.exceptions import NoSuchElementException |
3 | 3 | from selenium.webdriver.remote import webelement |
| 4 | +from Library.variable import Var |
4 | 5 | import os |
| 6 | +import datetime |
| 7 | + |
5 | 8 | from Library.store import Store |
6 | 9 |
|
7 | 10 |
|
8 | 11 | class Driver: |
9 | | - driver = None |
10 | | - |
11 | | - def __init__(self, browser = "chrome") -> None: |
12 | | - if browser == "chrome": |
13 | | - options = webdriver.ChromeOptions() |
14 | | - options.add_argument("--no-sandbox") |
15 | | - options.add_argument("--foreground") |
16 | | - options.add_argument('disable-infobars') |
17 | | - options.add_argument("--disable-extensions") |
18 | | - # options.add_argument("--headless") |
19 | | - self.driver = webdriver.Chrome(options=options) |
20 | | - self.driver.implicitly_wait(20) |
21 | | - elif browser == "firefox": |
22 | | - self.driver = webdriver.Firefox() |
23 | | - elif browser == "safari": |
24 | | - self.driver = webdriver.Safari() |
25 | | - Store.push(self.driver) |
26 | | - |
27 | | - def get(self, url: str) -> None: |
28 | | - self.driver.get(url) |
29 | | - |
30 | | - def find_element(self, by, value) -> webelement.WebElement: |
31 | | - try: |
32 | | - return self.driver.find_element(by, value) |
33 | | - except NoSuchElementException: |
34 | | - print("Element not found \n\n" + by + "\n" + value) |
35 | | - except Exception as e: |
36 | | - print("Error in finding the element \n\n" + by + "\n" + value + "\nException: \n" + str(e)) |
37 | | - |
38 | | - def find_elements(self, by, value) -> list[webelement.WebElement]: |
39 | | - try: |
40 | | - return self.driver.find_elements(by, value) |
41 | | - except NoSuchElementException: |
42 | | - print("Element not found \n\n" + by + "\n" + value) |
43 | | - except Exception as e: |
44 | | - print("Error in finding the element \n\n" + by + "\n" + value + "\nException: \n" + str(e)) |
45 | | - |
46 | | - def refresh(self) -> None: |
47 | | - self.driver.refresh() |
48 | | - |
49 | | - def execute_script(self, script, locator) -> None: |
50 | | - self.driver.execute_script(self, script, locator) |
51 | | - |
52 | | - def current_url(self) -> str: |
53 | | - return self.driver.current_url |
54 | | - |
55 | | - def save_screenshot(self) -> bool: |
56 | | - ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) |
57 | | - CONFIG_PATH = os.path.join(ROOT_DIR, 'configuration.conf') |
58 | | - self.driver.save_screenshot(self, ) |
59 | | - |
60 | | - |
61 | | - def quit(self): |
62 | | - self.driver.quit() |
| 12 | + driver = None |
| 13 | + |
| 14 | + def __init__(self) -> None: |
| 15 | + if not Var.env("browser") == "None": |
| 16 | + browser = Var.env("browser") |
| 17 | + else: |
| 18 | + browser = Var.glob("browser") |
| 19 | + if browser == "chrome": |
| 20 | + options = webdriver.ChromeOptions() |
| 21 | + options.add_argument("--no-sandbox") |
| 22 | + options.add_argument("--foreground") |
| 23 | + options.add_argument('disable-infobars') |
| 24 | + options.add_argument("--disable-extensions") |
| 25 | + if str(Var.glob("headless")) == "1" or str(Var.env("headless")) == "1": |
| 26 | + options.add_argument("--headless") |
| 27 | + self.driver = webdriver.Chrome(options=options) |
| 28 | + self.driver.implicitly_wait(int(Var.glob("implicit_wait"))) |
| 29 | + self.driver.set_window_size(int(Var.glob("browser_horizontal_size")), int(Var.glob("browser_vertical_size"))) |
| 30 | + elif browser == "firefox": |
| 31 | + self.driver = webdriver.Firefox() |
| 32 | + elif browser == "safari": |
| 33 | + self.driver = webdriver.Safari() |
| 34 | + Store.push(self.driver) |
| 35 | + |
| 36 | + def get(self, url: str) -> None: |
| 37 | + self.driver.get(url) |
| 38 | + |
| 39 | + def find_element(self, by, value) -> webelement.WebElement: |
| 40 | + try: |
| 41 | + return self.driver.find_element(by, value) |
| 42 | + except NoSuchElementException: |
| 43 | + print("Element not found \n\n" + by + "\n" + value) |
| 44 | + except Exception as e: |
| 45 | + print("Error in finding the element \n\n" + by + "\n" + value + "\nException: \n" + str(e)) |
| 46 | + |
| 47 | + def find_elements(self, by, value): |
| 48 | + try: |
| 49 | + return self.driver.find_elements(by, value) |
| 50 | + except NoSuchElementException: |
| 51 | + print("Element not found \n\n" + by + "\n" + value) |
| 52 | + except Exception as e: |
| 53 | + print("Error in finding the element \n\n" + by + "\n" + value + "\nException: \n" + str(e)) |
| 54 | + |
| 55 | + def refresh(self) -> None: |
| 56 | + self.driver.refresh() |
| 57 | + |
| 58 | + def execute_script(self, script, locator) -> None: |
| 59 | + self.driver.execute_script(self, script, locator) |
| 60 | + |
| 61 | + def current_url(self) -> str: |
| 62 | + return self.driver.current_url |
| 63 | + |
| 64 | + def save_screenshot(self) -> str: |
| 65 | + root_dir = os.path.dirname(os.path.abspath(__file__)) |
| 66 | + config_path = os.path.join(root_dir, 'reports/screenshot/img%s.png' % datetime.datetime.now().strftime("%Y%m%d-%H%M%S")) |
| 67 | + self.driver.save_screenshot(config_path) |
| 68 | + return config_path |
| 69 | + |
| 70 | + def quit(self): |
| 71 | + self.driver.quit() |
| 72 | + |
0 commit comments