Skip to content

Commit c244cc6

Browse files
committed
TEST: linting fixes and test_add_device_with_generated_mac_ip rewrite
Signed-off-by: jokob-sk <jokob.sk@gmail.com>
1 parent 19f4d3e commit c244cc6

1 file changed

Lines changed: 42 additions & 152 deletions

File tree

test/ui/test_ui_devices.py

Lines changed: 42 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -79,180 +79,70 @@ def test_devices_totals_api(api_token):
7979
assert len(data) > 0, "Response should contain data"
8080

8181

82-
def test_add_device_with_random_data(driver, api_token):
83-
"""Test: Add new device with random MAC and IP via UI"""
82+
def test_add_device_with_generated_mac_ip(driver, api_token):
83+
"""Add a new device using the UI, always clicking Generate MAC/IP buttons"""
8484
import requests
85-
import random
85+
import time
8686

8787
driver.get(f"{BASE_URL}/devices.php")
8888
time.sleep(2)
8989

90-
# Find and click the "Add Device" button (common patterns)
90+
# --- Click "Add Device" ---
9191
add_buttons = driver.find_elements(By.CSS_SELECTOR, "button#btnAddDevice, button[onclick*='addDevice'], a[href*='deviceDetails.php?mac='], .btn-add-device")
92-
93-
if len(add_buttons) == 0:
94-
# Try finding by text
95-
add_buttons = driver.find_elements(By.XPATH, "//button[contains(text(), 'Add') or contains(text(), 'New')] | //a[contains(text(), 'Add') or contains(text(), 'New')]")
96-
97-
if len(add_buttons) == 0:
98-
# No add device button found - skip this test
99-
assert True, "Add device functionality not available on this page"
92+
if not add_buttons:
93+
add_buttons = driver.find_elements(By.XPATH, "//button[contains(text(),'Add') or contains(text(),'New')] | //a[contains(text(),'Add') or contains(text(),'New')]")
94+
if not add_buttons:
95+
assert True, "Add device button not found, skipping test"
10096
return
101-
102-
# Click the button
10397
add_buttons[0].click()
104-
time.sleep(3)
105-
106-
# Check current URL - might have navigated to deviceDetails page
107-
current_url = driver.current_url
108-
109-
# Look for MAC field with more flexible selectors
110-
mac_field = None
111-
mac_selectors = [
112-
"input#mac", "input#deviceMac", "input#txtMAC",
113-
"input[name='mac']", "input[name='deviceMac']",
114-
"input[placeholder*='MAC' i]", "input[placeholder*='Address' i]"
115-
]
116-
117-
for selector in mac_selectors:
118-
try:
119-
fields = driver.find_elements(By.CSS_SELECTOR, selector)
120-
if len(fields) > 0 and fields[0].is_displayed():
121-
mac_field = fields[0]
122-
break
123-
except Exception:
124-
continue
125-
126-
if mac_field is None:
127-
# Try finding any input that looks like it could be for MAC
128-
all_inputs = driver.find_elements(By.TAG_NAME, "input")
129-
for inp in all_inputs:
130-
input_id = inp.get_attribute("id") or ""
131-
input_name = inp.get_attribute("name") or ""
132-
input_placeholder = inp.get_attribute("placeholder") or ""
133-
if "mac" in input_id.lower() or "mac" in input_name.lower() or "mac" in input_placeholder.lower():
134-
if inp.is_displayed():
135-
mac_field = inp
136-
break
137-
138-
if mac_field is None:
139-
# UI doesn't have device add form - skip test
140-
assert True, "Device add form not found - functionality may not be available"
141-
return
142-
143-
# Generate random MAC
144-
random_mac = f"00:11:22:{random.randint(0,255):02X}:{random.randint(0,255):02X}:{random.randint(0,255):02X}"
145-
146-
# Find and click "Generate Random MAC" button if it exists
147-
random_mac_buttons = driver.find_elements(By.CSS_SELECTOR, "button[onclick*='randomMAC'], button[onclick*='generateMAC'], #btnRandomMAC, button[onclick*='Random']")
148-
if len(random_mac_buttons) > 0:
149-
try:
150-
driver.execute_script("arguments[0].click();", random_mac_buttons[0])
151-
time.sleep(1)
152-
# Re-get the MAC value after random generation
153-
test_mac = mac_field.get_attribute("value")
154-
except Exception:
155-
# Random button didn't work, enter manually
156-
mac_field.clear()
157-
mac_field.send_keys(random_mac)
158-
test_mac = random_mac
159-
else:
160-
# No random button, enter manually
161-
mac_field.clear()
162-
mac_field.send_keys(random_mac)
163-
test_mac = random_mac
164-
165-
assert len(test_mac) > 0, "MAC address should be filled"
166-
167-
# Look for IP field (optional)
168-
ip_field = None
169-
ip_selectors = ["input#ip", "input#deviceIP", "input#txtIP", "input[name='ip']", "input[placeholder*='IP' i]"]
170-
for selector in ip_selectors:
171-
try:
172-
fields = driver.find_elements(By.CSS_SELECTOR, selector)
173-
if len(fields) > 0 and fields[0].is_displayed():
174-
ip_field = fields[0]
175-
break
176-
except Exception:
177-
continue
178-
179-
if ip_field:
180-
# Find and click "Generate Random IP" button if it exists
181-
random_ip_buttons = driver.find_elements(By.CSS_SELECTOR, "button[onclick*='randomIP'], button[onclick*='generateIP'], #btnRandomIP")
182-
if len(random_ip_buttons) > 0:
183-
try:
184-
driver.execute_script("arguments[0].click();", random_ip_buttons[0])
185-
time.sleep(0.5)
186-
except:
187-
pass
188-
189-
# If IP is still empty, enter manually
190-
if not ip_field.get_attribute("value"):
191-
random_ip = f"192.168.1.{random.randint(100,250)}"
192-
ip_field.clear()
193-
ip_field.send_keys(random_ip)
194-
195-
# Fill in device name (optional)
196-
name_field = None
197-
name_selectors = ["input#name", "input#deviceName", "input#txtName", "input[name='name']", "input[placeholder*='Name' i]"]
198-
for selector in name_selectors:
199-
try:
200-
fields = driver.find_elements(By.CSS_SELECTOR, selector)
201-
if len(fields) > 0 and fields[0].is_displayed():
202-
name_field = fields[0]
203-
break
204-
except:
205-
continue
206-
207-
if name_field:
208-
name_field.clear()
209-
name_field.send_keys("Test Device Selenium")
98+
time.sleep(2)
21099

211-
# Find and click Save button
100+
# --- Helper to click generate button for a field ---
101+
def click_generate_button(field_id):
102+
btn = driver.find_element(By.CSS_SELECTOR, f"span[onclick*='generate_{field_id}']")
103+
driver.execute_script("arguments[0].click();", btn)
104+
time.sleep(0.5)
105+
# Return the new value
106+
inp = driver.find_element(By.ID, field_id)
107+
return inp.get_attribute("value")
108+
109+
# --- Generate MAC ---
110+
test_mac = click_generate_button("NEWDEV_devMac")
111+
assert test_mac, "MAC should be generated"
112+
113+
# --- Generate IP ---
114+
test_ip = click_generate_button("NEWDEV_devLastIP")
115+
assert test_ip, "IP should be generated"
116+
117+
# --- Fill Name ---
118+
name_field = driver.find_element(By.ID, "NEWDEV_devName")
119+
name_field.clear()
120+
name_field.send_keys("Test Device Selenium")
121+
122+
# --- Click Save ---
212123
save_buttons = driver.find_elements(By.CSS_SELECTOR, "button#btnSave, button#save, button[type='submit'], button.btn-primary, button[onclick*='save' i]")
213-
if len(save_buttons) == 0:
214-
save_buttons = driver.find_elements(By.XPATH, "//button[contains(translate(text(), 'SAVE', 'save'), 'save')]")
215-
216-
if len(save_buttons) == 0:
217-
# No save button found - skip test
218-
assert True, "Save button not found - test incomplete"
124+
if not save_buttons:
125+
save_buttons = driver.find_elements(By.XPATH, "//button[contains(translate(text(),'SAVE','save'),'save')]")
126+
if not save_buttons:
127+
assert True, "Save button not found, skipping test"
219128
return
220-
221-
# Click save
222129
driver.execute_script("arguments[0].click();", save_buttons[0])
223130
time.sleep(3)
224131

225-
# Verify device was saved via API
132+
# --- Verify device via API ---
226133
headers = {"Authorization": f"Bearer {api_token}"}
227-
verify_response = requests.get(
228-
f"{API_BASE_URL}/device/{test_mac}",
229-
headers=headers
230-
)
231-
134+
verify_response = requests.get(f"{API_BASE_URL}/device/{test_mac}", headers=headers)
232135
if verify_response.status_code == 200:
233-
# Device was created successfully
234136
device_data = verify_response.json()
235137
assert device_data is not None, "Device should exist in database"
236138

237-
# Cleanup: Delete the test device
238-
try:
239-
delete_response = requests.delete(
240-
f"{API_BASE_URL}/device/{test_mac}",
241-
headers=headers
242-
)
243-
except:
244-
pass # Delete might not be supported
245139
else:
246-
# Check if device appears in the UI
140+
# Fallback: check UI
247141
driver.get(f"{BASE_URL}/devices.php")
248142
time.sleep(2)
249-
250-
# If device is in page source, test passed even if API failed
251143
if test_mac in driver.page_source or "Test Device Selenium" in driver.page_source:
252144
assert True, "Device appears in UI"
253145
else:
254-
# Can't verify - just check that save didn't produce visible errors
255-
# Look for actual error messages (not JavaScript code)
256-
error_indicators = driver.find_elements(By.CSS_SELECTOR, ".alert-danger, .error-message, .callout-danger")
257-
has_error = any(elem.is_displayed() and len(elem.text) > 0 for elem in error_indicators)
258-
assert not has_error, "Save should not produce visible error messages"
146+
error_elements = driver.find_elements(By.CSS_SELECTOR, ".alert-danger, .error-message, .callout-danger")
147+
has_error = any(elem.is_displayed() and elem.text for elem in error_elements)
148+
assert not has_error, "Save should not produce visible errors"

0 commit comments

Comments
 (0)