利用Python 來實現UI 自動化的功能.
適合需要反覆設定的測試.
例如設定WiFi Channel 的場合或是讓待測物反覆重開機.
1. Installation
1.1 Install
python3 on Windows 7
https://www.python.org/downloads/
1.2
1.3
If "api-ms-win-crt-runtime-l1-1-0.dll"
error occurs, install VC++2015
1.4 Install pip
https://jerrynest.io/windows-install-pip/
1.5 Install selenium via pip
pip install selenium
1.6 Install editor Thonny
http://thonny.org/
1.7
install selenium firefox web driver
http://selenium-python.readthedocs.io/installation.html
https://github.com/mozilla/geckodriver/releases
1.8
Install selenium IDE on firefox
https://addons.mozilla.org/zh-TW/firefox/addon/selenium-ide/
2. Script example
1. (Selenium module) Basic -
get UI with JS
#優點: like browser and
can record web via selenium IDE
#缺點: Slow and complex
from selenium import webdriver
import time
url="http://192.168.0.1//cgi-bin/luci/"
driver = webdriver.Firefox()
driver.get(url)
driver.find_element_by_xpath(u"//input[@type='button'
and @value='reboot']").click()
time.sleep(2)
driver.quit()
2. (Selenium module) Reboot device via UI 10000 times
# -*- coding: utf-8 -*-
# Function1: Login to UI
# Function2: Reboot device
# Function3: Print result output to file
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re, sys, os
class Test(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_ip = "192.168.2.3"
self.username = "admin"
self.password = "123"
self.base_url = "http://"+ self.base_ip
self.verificationErrors = []
self.accept_next_alert = True
def test_(self):
driver = self.driver
driver.get("http://"+self.username+":"+self.password+"@"+self.base_ip)
driver.switch_to_alert()
self.close_alert_and_get_its_text()
time.sleep(5)
i = 0
now = time.strftime("%c")
now1 = "Current date & time : " + now
self.wfile("\r\n")
self.wfile(now1)
self.wfile("reboot times is ")
while i <= 10000:
driver.get(self.base_url + "/cgi-bin/luci/content/system_settings/initialization")
driver.find_element_by_xpath(u"//input[@type='button' and @value='reboot']").click()
time.sleep(80)
i += 1
self.wfile(", ")
self.wfile(str(i))
def wfile(self,a):
wfile=os.path.splitext(__file__)[0]+".txt"
self.f = open(wfile, 'a', encoding = 'UTF-8')
f=self.f
with f as g:
print(a, end=" ")
print(a, end=" ", file=g)
f.close()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
if __name__ == "__main__":
unittest.main(exit=False)