-
Notifications
You must be signed in to change notification settings - Fork 0
/
DslLinkInfo.py
145 lines (116 loc) · 5.18 KB
/
DslLinkInfo.py
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
import os
import csv
import time
from pathlib import Path
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
class DslLinkInfo():
def __init__(self, headless=True, browser='firefox'):
if browser == 'chrome':
options = webdriver.chrome.options.Options()
else:
options = webdriver.firefox.options.Options()
if headless:
# Run Chrome driver without GUI
options.add_argument("--headless")
# Select Driver
if browser == 'chrome':
self.driver = webdriver.Chrome(options=options)
else:
self.driver = webdriver.Firefox(options=options)
def __del__(self):
self.driver.close()
def login(self, router_page, router_username, router_password):
self.driver.get(router_page)
# Locate Form Input fields
form_username = self.driver.find_element_by_id('Frm_Username')
form_password = self.driver.find_element_by_id('Frm_Password')
# Clear any pre-populated text in the input field
form_username.clear()
form_password.clear()
# Enter username
form_username.send_keys(router_username)
# Enter password
form_password.send_keys(router_password)
form_password.send_keys(Keys.RETURN)
def internet_page(self, internet_page, timeout=10):
# Test if "Another User is configuring the device" page is shown
# Wait for page to load (until Apply Button is loaded)
try:
btn_apply = WebDriverWait(self.driver, timeout/2).until(
EC.element_to_be_clickable((By.ID, 'Btn_apply'))
)
# Click Internet tab
btn_apply.click()
except TimeoutException:
pass
# Wait for page to load (until Internet tab is loaded)
try:
nav_internet = WebDriverWait(self.driver, timeout).until(
EC.element_to_be_clickable((By.ID, internet_page))
)
# Click Internet tab
nav_internet.click()
except TimeoutException:
raise TimeoutException('Timed out waiting for page to load.')
def get_dsl_link_info(self, dsl_link_info_bar, timeout=10):
# Wait for page to load (until DSL Link Info bar is loaded)
try:
WebDriverWait(self.driver, timeout).until(
EC.element_to_be_clickable((By.ID, dsl_link_info_bar))
)
# Get DSL Errors
errors_crc = self.driver.find_element_by_id('ccrc:0')
errors_fec = self.driver.find_element_by_id('cfec:0')
showtime_start = self.driver.find_element_by_id('cststart:0')
# Get DSL Errors Value
errors_crc_value = errors_crc.get_attribute('title')
errors_fec_value = errors_fec.get_attribute('title')
showtime_start_value = showtime_start.get_attribute('title')
# Get Up and Down Errors
errors_crc_up = errors_crc_value.split('/')[0]
errors_crc_down = errors_crc_value.split('/')[1]
errors_fec_up = errors_fec_value.split('/')[0]
errors_fec_down = errors_fec_value.split('/')[1]
showtime_start_value = showtime_start_value.replace(' ', '')
# Get Current Date
current_date = time.strftime('%Y-%m-%d %H:%M:%S')
# Return as a Dict
self.dsl_link_info_dict = {
'errors_crc_up': errors_crc_up,
'errors_crc_down': errors_crc_down,
'errors_fec_up': errors_fec_up,
'errors_fec_down': errors_fec_down,
'showtime_start_value': showtime_start_value,
'current_date': current_date
}
return self.dsl_link_info_dict
except TimeoutException:
raise TimeoutException('Timed out waiting for page to load.')
def save_dsl_link_info(self, csv_file, data=None):
if data == None:
data = self.dsl_link_info_dict
# Get dictionary keys
csv_columns = list(data.keys())
# Save results
with open(csv_file, mode='a') as csv_fd:
# Create a writer object from csv module
csv_writer = csv.DictWriter(csv_fd, fieldnames=csv_columns)
# Add contents as last row in the csv file
csv_writer.writerow(data)
# Save results headers
# Create the headers file from name of CSV file
csv_headers_file_name = csv_file.stem + '_headers' + csv_file.suffix
# Get CSV's path without the filename
csv_dir_path = Path(*csv_file.parts[:-1])
# Join CSV's path with new filename
csv_headers_file = csv_dir_path.joinpath(csv_headers_file_name)
with open(csv_headers_file, mode='w') as csv_fd:
# Create a writer object from csv module
csv_writer = csv.writer(csv_fd)
# Add contents as last row in the csv file
csv_writer.writerow(csv_columns)