Skip to content

Commit 4804ae4

Browse files
Refactor the folder structure and env & local variable changes
1 parent b8da069 commit 4804ae4

File tree

12 files changed

+136
-78
lines changed

12 files changed

+136
-78
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ __pycache__/
1111
# python identifiers
1212
.python-version
1313

14-
# pytest report
14+
# pytest reports
1515
html_report.html
16-
report/
16+
1717

1818
# browser trash
1919
geckodriver.log

Data/GlobalData/global_data.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
browser: chrome
2+
implicit_wait: 10
3+
browser_horizontal_size: 1200
4+
browser_vertical_size: 800
5+
headless: 0
File renamed without changes.

Data/TestData/google.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
url: https://www.google.com
2+
search_text: hello

Library/driver.py

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,72 @@
11
from selenium import webdriver
22
from selenium.common.exceptions import NoSuchElementException
33
from selenium.webdriver.remote import webelement
4+
from Library.variable import Var
45
import os
6+
import datetime
7+
58
from Library.store import Store
69

710

811
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+

Library/variable.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import yaml
3+
4+
5+
class Var:
6+
file_name = None
7+
local_variable = None
8+
9+
def __init__(self, file_name):
10+
self.file_name = file_name
11+
try:
12+
root_dir = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "")
13+
with open(root_dir + '/Data/TestData/' + file_name) as file:
14+
self.local_variable = yaml.load(file, Loader=yaml.FullLoader)
15+
except Exception as e:
16+
print(e)
17+
18+
@staticmethod
19+
def env(string):
20+
try:
21+
return os.environ[string]
22+
except Exception as e:
23+
print(e)
24+
return "None"
25+
26+
@staticmethod
27+
def glob(string):
28+
try:
29+
root_dir = os.path.dirname(os.path.abspath(__file__)).replace("/Library", "")
30+
with open(root_dir + '/Data/GlobalData/global_data.yml') as file:
31+
global_data = yaml.load(file, Loader=yaml.FullLoader)
32+
return global_data[string]
33+
except Exception as e:
34+
print(e)
35+
return "None"
36+
37+
def loc(self, string):
38+
try:
39+
return self.local_variable[string]
40+
except Exception as e:
41+
print(e)
42+
return ""

Tests/conftest.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33

44
@pytest.fixture(autouse=True)
5-
def before_all():
6-
print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ INITIALIZATION')
5+
def before_each():
6+
print('*-* Before each INITIALIZATION')
77
yield
8-
print('######################################### END')
8+
print('*-* After each END')
9+
10+
11+
@pytest.fixture(scope='module', autouse=True)
12+
def before_module():
13+
print('*-* Before module INITIALIZATION')
14+
yield
15+
print('*-* After module END')
916

1017

1118
def pytest_configure(config):

Tests/google.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,23 @@
33
import os
44
from Library.driver import Driver
55
from Pages.google import GooglePage
6+
from Library.variable import Var
67

78

89
@allure.feature("Google Search")
910
@allure.step('Enter and search')
1011
@allure.severity('Critical')
1112
def test_google_search():
13+
with allure.step("Set the test data file needed for this test run"):
14+
variable = Var("google.yml")
15+
1216
with allure.step("first step"):
13-
print(os.environ['BROWSER'])
14-
d = Driver("chrome")
15-
d.get("https://www.google.com")
17+
d = Driver()
18+
d.get(variable.loc("url"))
1619
print("landed in google home page")
1720

1821
with allure.step("second step"):
19-
GooglePage.enter_search_text("hello")
22+
GooglePage.enter_search_text(variable.loc("search_text"))
2023
d.quit()
2124

2225

23-
@allure.feature("Google Search 2")
24-
@allure.step('Enter and search 2')
25-
@allure.severity('Critical')
26-
def test_google_search2():
27-
with allure.step("first step 2"):
28-
d = Driver("chrome")
29-
d.get("https://www.google.com")
30-
print("landed in google home page")
31-
32-
with allure.step("second step 2"):
33-
GooglePage.enter_search_text("hello")
34-
d.quit()

pytest.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ addopts = -rsxX
88
-q
99
-v
1010
--self-contained-html
11-
--html=html_report.html
11+
--html=reports/html_report.html
1212
--cov=Tests
13-
--alluredir report
13+
--alluredir reports/allure
1414
--clean-alluredir

reports/allure/.ignore

Whitespace-only changes.

0 commit comments

Comments
 (0)