-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_search.py
More file actions
85 lines (70 loc) · 3.33 KB
/
google_search.py
File metadata and controls
85 lines (70 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# google_search.py
import os
import time
import logging
import pandas as pd
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from sidebar_test import test_sidebar
# Logging config
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def save_screenshot(driver, test_case_id, step):
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
folder = os.path.expanduser("~/Pictures/SmartCookie_Screenshots")
os.makedirs(folder, exist_ok=True)
filename = f"{folder}/{test_case_id}_{step}_{timestamp}.png"
driver.save_screenshot(filename)
logging.info(f"Screenshot saved: {filename}")
def run_test_case(test_case):
logging.info(f"Running Test Case: {test_case['id']}")
options = Options()
options.add_argument("--disable-blink-features=AutomationControlled")
service = Service()
driver = webdriver.Chrome(service=service, options=options)
driver.maximize_window()
driver.get("https://smartcookie.in/")
save_screenshot(driver, test_case["id"], "homepage")
try:
driver.find_element(By.XPATH, "//button[contains(text(),'Login As')]").click()
save_screenshot(driver, test_case["id"], "login_as")
driver.find_element(By.XPATH, "//a[contains(text(),'Student')]").click()
save_screenshot(driver, test_case["id"], "student_click")
time.sleep(2)
driver.find_element(By.ID, "email").send_keys(test_case["username"])
driver.find_element(By.ID, "password").send_keys(test_case["password"])
save_screenshot(driver, test_case["id"], "credentials_entered")
driver.find_element(By.ID, "btnLogin").click()
time.sleep(5)
current_url = driver.current_url
if test_case["expected"].lower() == "fail":
if "login" in current_url or "members" in current_url:
logging.info(f"{test_case['id']}: Login failed as expected.")
else:
logging.warning(f"{test_case['id']}: Unexpected success. URL: {current_url}")
save_screenshot(driver, test_case["id"], "unexpected_result")
else:
if "dashboard" in current_url or test_case["expected"].lower() == "success":
logging.info(f"{test_case['id']}: Login successful. Running sidebar test.")
test_sidebar(driver, test_case["id"], save_screenshot)
else:
logging.warning(f"{test_case['id']}: Unexpected result. URL: {current_url}")
save_screenshot(driver, test_case["id"], "unexpected_result")
except Exception as e:
logging.error(f"{test_case['id']}: Exception occurred - {e}")
save_screenshot(driver, test_case["id"], "exception")
driver.quit()
logging.info(f"{test_case['id']}: Browser closed.\n")
if __name__ == "__main__":
# Read Excel file
df = pd.read_excel("login_test_cases.xlsx")
for _, row in df.iterrows():
test_case = {
"id": row["TestCaseID"],
"username": row["Email"],
"password": row["Password"],
"expected": row["ExpectedResult"]
}
run_test_case(test_case)