-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomepage.py
More file actions
109 lines (86 loc) · 3.75 KB
/
homepage.py
File metadata and controls
109 lines (86 loc) · 3.75 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
# Class for Testing Login
class LoginTest:
# Test Data for Positive Login Test
username = "student"
password = "Password123"
# Test data for negative login test
username1 = "incorrectUser"
password1 = "incorrectPassword "
# Test Locators
username_locator = '//*[@id="username"]' # Locates username
password_locator = '//*[@id="password"]' # Locates password
submit_button_locator = '//*[@id="submit"]' # Locates submit button
logout_button_locator ='//*[@id="loop-container"]/div/article/div[2]/div/div/div/a' # Locates logout button
login_error_text_locator = '//*[@id="error"]' # Locates error message abot invalid user and invalid password
# Define the class constructor (__init__) method
def __init__(self, url):
# Store the given URL as an instance variable
self.url = url
# Initialize a Chrome WebDriver instance to interact with web pages
self.driver = webdriver.Chrome()
# Method for Login
def login(self):
self.driver.maximize_window() # Maximize window
self.driver.get(self.url) # Go to webpage
sleep(5)
# enter user name
self.driver.find_element(by=By.XPATH, value=self.username_locator).send_keys(self.username)
sleep(2)
# Enter password
self.driver.find_element(by=By.XPATH, value=self.password_locator).send_keys(self.password)
sleep(2)
# Click on submit
self.driver.find_element(by=By.XPATH, value=self.submit_button_locator).click()
sleep(5)
# Click on logout
self.driver.find_element(by=By.XPATH, value=self.logout_button_locator).click()
sleep(2)
if self.driver.current_url =="https://practicetestautomation.com/practice-test-login/":
print("SUCCESS : TEST CASE PASSED")
return True
else:
return False
# Method for login with wrong username
def NegativeUsername(self):
# enter user name
self.driver.find_element(by=By.XPATH, value=self.username_locator).send_keys(self.username1)
sleep(2)
# Enter password
self.driver.find_element(by=By.XPATH, value=self.password_locator).send_keys(self.password)
sleep(2)
# Click on submit
self.driver.find_element(by=By.XPATH, value=self.submit_button_locator).click()
sleep(3)
# to get the error text for assertion
invalid_username_text = self.driver.find_element(by=By.XPATH, value=self.login_error_text_locator).text
if invalid_username_text =="Your username is invalid!":
print("Your username is invalid!")
return True
else:
print("Failed, Unexpected Error!")
return False
# Method for login with wrong password
def NegativePassword(self):
# enter user name
self.driver.find_element(by=By.XPATH, value=self.username_locator).send_keys(self.username)
sleep(2)
# Enter password
self.driver.find_element(by=By.XPATH, value=self.password_locator).send_keys(self.password1)
sleep(2)
# Click on submit
self.driver.find_element(by=By.XPATH, value=self.submit_button_locator).click()
sleep(3)
invalid_password_text = self.driver.find_element(by=By.XPATH, value=self.login_error_text_locator).text
if invalid_password_text =="Your password is invalid!":
print("Your password is invalid!")
return True
else:
print("Failed, Unexpected Error!")
return False
# Shutdown Method
def shutdown(self):
self.driver.quit()
return None