-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoodle_methods.py
More file actions
184 lines (147 loc) · 7.18 KB
/
moodle_methods.py
File metadata and controls
184 lines (147 loc) · 7.18 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import datetime
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
import moodle_locators as locators
driver = webdriver.Chrome()
divider = '------------------------------'
def set_up():
# Make a full screen
driver.maximize_window()
# Navigating to the Moodle app website
driver.get(locators.moodle_url)
# Checking that we're on the correct URL address and we're seeing correct title
if driver.current_url == locators.moodle_url and driver.title == 'Software Quality Assurance Testing':
print(f'We\'re at Moodle homepage -- {driver.current_url}')
print(f'We\'re seeing title message -- "Software Quality Assurance Testing"')
else:
print(f'We\'re not at the Moodle homepage. Check your code!')
driver.close()
driver.quit()
def tear_down(scenario_num):
if driver is not None:
print(divider)
text = f'{scenario_num} completed at {datetime.datetime.now()}'
print(text)
open('moodle_log.log', 'a').write(text + '\n')
driver.close()
def log_in(username, password):
if driver.current_url == locators.moodle_url:
driver.find_element(By.LINK_TEXT, 'Log in').click()
if driver.current_url == locators.moodle_login_url:
driver.find_element(By.ID, 'username').send_keys(username)
sleep(0.25)
driver.find_element(By.ID, 'password').send_keys(password)
sleep(0.25)
driver.find_element(By.ID, 'loginbtn').click()
if driver.title == 'Dashboard' and driver.current_url == locators.moodle_dashboard_url:
assert driver.current_url == locators.moodle_dashboard_url
print(f'Log in successfully. Dashboard is present. \n'
f'We logged in with Username: {username} and Password: {password}')
else:
print(f'We\re not at the Dashboard. Try again')
def log_out():
driver.find_element(By.CLASS_NAME, 'userpicture').click()
sleep(0.25)
driver.find_element(By.XPATH, '//span[contains(., "Log out")]').click()
sleep(0.25)
if driver.current_url == locators.moodle_url:
print(f'Log out successfully at: {datetime.datetime.now()}')
def create_new_user():
driver.find_element(By.XPATH, '//span[contains(., "Site administration")]').click()
sleep(0.25)
assert driver.find_element(By.LINK_TEXT, 'Users').is_displayed()
driver.find_element(By.LINK_TEXT, 'Users').click()
sleep(0.25)
driver.find_element(By.LINK_TEXT, 'Add a new user').click()
sleep(0.25)
assert driver.find_element(By.LINK_TEXT, 'Add a new user').is_displayed()
sleep(0.25)
# Start fill the form
# username
driver.find_element(By.ID, 'id_username').send_keys(locators.new_username)
sleep(0.25)
# password
driver.find_element(By.LINK_TEXT, 'Click to enter text').click()
sleep(0.25)
driver.find_element(By.ID, 'id_newpassword').send_keys(locators.new_password)
sleep(0.25)
# First name
driver.find_element(By.ID, 'id_firstname').send_keys(locators.first_name)
sleep(0.25)
# Last name
driver.find_element(By.ID, 'id_lastname').send_keys(locators.last_name)
sleep(0.25)
# email
driver.find_element(By.ID, 'id_email').send_keys(locators.email)
# Select 'Allow everyone to see my email address'
Select(driver.find_element(By.ID, 'id_maildisplay')).select_by_visible_text('Allow everyone to see my email address')
sleep(0.25)
# Click by 'Create user' button
driver.find_element(By.ID, 'id_submitbutton').click()
sleep(0.25)
print(f'--- Test Scenario: Create a new user "{locators.new_username}" --- is passed')
# Take users details to .csv file
open('moodle_users_list.csv', 'a').write(f'{locators.new_username};{locators.new_password}; {locators.first_name};{locators.last_name};{locators.email};\n')
def filter_user():
sleep(1)
# Check that we are on the User's Main Page
if driver.current_url == locators.moodle_users_main_page:
assert driver.find_element(By.XPATH, "//h1[text() = 'Software Quality Assurance Testing']").is_displayed()
if driver.find_element(By.ID, 'fgroup_id_email_grp_label') and driver.find_element(By.NAME, 'email'):
sleep(0.25)
driver.find_element(By.CSS_SELECTOR, 'input#id_email').send_keys(locators.email)
sleep(0.25)
driver.find_element(By.CSS_SELECTOR, 'input#id_addfilter').click()
sleep(0.25)
if driver.find_element(By.XPATH, f'//td[contains(., "{locators.email}")]'):
print(f'--- User <{locators.email}> found on list')
def check_user_page():
sleep(1)
if driver.current_url == locators.moodle_dashboard_url:
# --- 1. Validate user full name on top right menu - beside logo
if driver.find_element(By.XPATH, f'//span[contains(., "{locators.full_name}")]').is_displayed():
print(f'--- User with the name {locators.full_name} is displayed. Test Passed ---')
# --- 2. Validate User's Dashboard. Must be only 3 options on left menu:
# Site Home, Calendar, Private files
users_options = ['Site home', 'Calendar', 'Private files']
for menu in users_options:
sleep(0.25)
if driver.find_element(By.XPATH, f'//span[contains(., "{menu}")]'):
driver.find_element(By.XPATH, f'//span[contains(., "{menu}")]').click()
else:
print(f'<{menu}> not found. Check')
sleep(0.25)
if menu == 'Site home':
assert driver.current_url == 'http://52.39.5.126/?redirect=0'
if menu == 'Calendar':
assert driver.current_url == 'http://52.39.5.126/calendar/view.php?view=month'
if menu == 'Private files':
assert driver.current_url == 'http://52.39.5.126/user/files.php'
print(f'--- Menu <{menu}> clicked and page validated')
# Validate that no administrative options on user page
admin_options = ['Content bank', 'Site administration']
for menu in admin_options:
try:
sleep(0.25)
if driver.find_element(By.XPATH, f'//span[contains(., "{menu}")]'):
print(f'Administrative menu <{menu}> displayed on user page. Check')
except:
print(f'Administrative menu <{menu}> not displayed on user page. Correct')
def delete_user():
sleep(1)
# Navigate to Users Page
driver.find_element(By.XPATH, '//span[contains(., "Site administration")]').click()
driver.find_element(By.LINK_TEXT, 'Users').click()
driver.find_element(By.LINK_TEXT, 'Browse list of users').click()
sleep(0.25)
# Filter user
filter_user()
# After filtering user, find user on table and delete
driver.find_element(By.XPATH, f'//td[contains(., "{locators.email}")]/following::*/descendant::i[@title = "Delete"]').click()
# Confirm
driver.find_element(By.XPATH, '//button[text() = "Delete"]').click()
print(f'--- User <{locators.email}> deleted successfully')
# -----------------------------------------------