python测试例子
#-*-coding:utf-8-*- from __future__ import with_statement import os import unittest import time from splinter import Browser from random import randint class DbackupTestCase(unittest.TestCase): @classmethod def setUpClass(cls): cls.browser = Browser('firefox') @classmethod def setDownClass(cls): cls.browser.quit() def login(self, username, password): if self.browser.find_by_id('lhgfrm_lhgdgId'): with self.browser.get_iframe('lhgfrm_lhgdgId') as frame: frame.find_by_id('trialRadio').click() frame.find_by_id('continue').click() self.browser.fill('username',username) self.browser.fill('password',password) self.browser.find_by_name('Submit').click() time.sleep(0.5) assert self.browser.is_element_present_by_tag('body') @unittest.skip('skip register') def test_register(self): self.browser.visit('http://192.168.88.245/dbackup') self.login(username='admin', password= 'admin') time.sleep(1) #because iframe has no id,so can't get it #iframe = self.browser.find_by_tag('iframe') #iframe.find_link_by_href('../Supermanage/LocalCfgManage.php').click() #self.browser.find_by_id('Navigate1').click() #self.browser.find_link_by_href('Register.php').click() navigate = self.browser.find_by_css('.NavigateMBG') for i in navigate: print i navigate[2].click() navigate[2].find_by_tag('a')[2].click() time.sleep(0.5) file = open('username.txt') lines = file.readlines() for line in lines: data = [ line.strip().split(':') for word in line] user = data[0] self.browser.find_by_id('username').fill(data[0][0]) self.browser.fill('password',data[0][1]) self.browser.find_by_id('confirmpassword').fill(data[0][1]) self.browser.find_by_id('email').fill(data[0][2]) self.browser.find_by_id('telephone').fill(data[0][3]) self.browser.find_by_name('Submit').click() time.sleep(1) alert = self.browser.get_alert() alert.accept() #navigate[2].click() #navigate[2].find_by_tag('a')[2].click() self.browser.find_by_id('Navigate1').click() self.browser.find_link_by_href('Register.php').click() file.close() @unittest.skip('skip user land') def test_User(self): self.browser.visit('http://192.168.88.245/dbackup') file = open('username.txt') lines = file.readlines() for line in lines: data = [ line.strip().split(':') for word in line ] user = data[0] self.login(username = user[0], password = user[1]) time.sleep(1) iframe = self.browser.find_by_tag('iframe') iframe[0].find_by_tag('a')[3].click() self.browser.visit('http://192.168.88.245/dbackup') @unittest.skip('del user') def test_delUser(self): self.browser.visit('http://192.168.88.245/dbackup') self.login(username ='admin', password ='admin') time.sleep(0.5) navigate = self.browser.find_by_css('.NavigateMBG') navigate[2].click() navigate[2].find_by_tag('a')[1].click() table=self.browser.find_by_id('mytable') tr=table.find_by_tag('tr') file = open('deluser.txt') delname = file.readline() file.close() i=1 while i< len(tr): if tr[i].find_by_tag('td')[1].value == delname.strip(): tr[i].find_by_tag('input').click() self.browser.find_by_id('nDeleteAccountID').click() time.sleep(1) alert = self.browser.get_alert() alert.accept() alert = self.browser.get_alert() alert.accept() time.sleep(1) break else: i+=1 def test_addFTP(self): self.browser.visit('http://192.168.88.245/dbackup') self.login(username = 'admin', password = 'admin') time.sleep(0.5) navigate = self.browser.find_by_css('.NavigateMBG') navigate[0].click() navigate[0].find_by_tag('a')[2].click() file = open('ftp.txt') lines = file.readlines() for line in lines: data = [ line.strip().split(':') for word in line ] ftp = data[0] self.browser.find_by_id('strFTPNameID').fill(ftp[0]) ip = [ ftp[1].strip().split('.') for ipnum in ftp[1] ] self.browser.find_by_id('IP1').fill(ip[0][0]) self.browser.find_by_id('IP2').fill(ip[0][1]) self.browser.find_by_id('IP3').fill(ip[0][2]) self.browser.find_by_id('IP4').fill(ip[0][3]) self.browser.find_by_id('strPortID').fill(ftp[2]) self.browser.find_by_id('strFTPLoginNameID').fill(ftp[3]) self.browser.fill('strFTPLoginPW',ftp[4]) self.browser.find_by_id('ConfirmPWID').fill(ftp[4]) self.browser.find_by_id('nPathID').fill(ftp[5]) self.browser.find_by_id('Submit').click() alert = self.browser.get_alert() alert.accept() time.sleep(1) self.browser.find_by_id('Navigate5').click() self.browser.find_link_by_href('FtpCfg.php').click() unittest.main()
username.txt
hdingjia:dingjia:hzhida95@gmail.com:13423413413
huangzd:dingjia:hzhida95@gmail.com:13143143144
mricurm:200012334:hzhida95@gmail.com:1314314141
Jane:jane:hzhida95@gmail.com:34313124314
ftp.txt
ftpunbuntu:192.168.88.162:21:hzhida:dingjia:FTPFile/backup
Live together,or Die alone!