def __init__(self): print("Get parameters from Configuration file") parameters = PubReadConfig("") global caseName caseName = sys.argv[0] caseName = caseName[:-3] global prjName prjName = parameters.GetParameter("DUT1", "PRODUCT") global brserType brserType = parameters.GetParameter("PC", "BRWSRTYPE") global DUTIP DUTIP = parameters.GetParameter("DUT1", "IP") self.switch_type = prjName self.brower_type = brserType self.URL = DUTIP global netcard netcard = parameters.GetParameter("PC", "MGMTNIC") global netcardip netcardip = parameters.GetParameter("PC", "MGMTIP") self.conn = PubModuleCase(self.URL) self.st1 = self.conn.ConnectionServer() if self.st1 != 200: print "connect to server failed...%d" % self.st1 sys.exit() self.driver = self.conn.LoginToWeb() self.mac = self.conn.GetDutMac() print "the mac of dut is %s" % self.mac
def __init__(self): print ("Get parameters from Configuration file") parameters=PubReadConfig("") global caseName caseName = sys.argv[0] caseName = caseName[:-3] global prjName prjName =parameters.GetParameter("DUT1","PRODUCT") global brserType brserType=parameters.GetParameter("PC","BRWSRTYPE") global DUTIP DUTIP = parameters.GetParameter("DUT1","IP") self.switch_type = prjName self.brower_type = brserType self.URL = DUTIP global netcard netcard = parameters.GetParameter("PC","MGMTNIC") global netcardip netcardip = parameters.GetParameter("PC","MGMTIP") self.conn = PubModuleCase(self.URL) self.st1 = self.conn.ConnectionServer() if self.st1 != 200: print "connect to server failed...%d" %self.st1 sys.exit() self.driver = self.conn.LoginToWeb() self.mac = self.conn.GetDutMac() print "the mac of dut is %s"%self.mac
def __init__(self, brserType, DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d." % st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle
def __init__(self, prjName, brsertype, DUTIP): self.prjName = prjName self.browserType = brsertype self.DUTIP = DUTIP self.pubmodul = PubModuleCase(DUTIP) st = self.pubmodul.ConnectionServer() print "Connection status %d." % st if st != 200: print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.pubmodul.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle
class AttackCases: def __init__(self, DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) def ChkDutSts(self): st = self.PubModuleEle.ConnectionServer() print "Connection status %d." % st if st != 200: print "Attack result in the Target DUT doesn't work" sys.exit() else: print "The target DUT can work,start to the next steps" def fgarp(self, macsrc, ipsrc, ipdst, nic): send(ARP(op=2, hwsrc=macsrc, psrc=ipsrc, pdst=ipdst), iface=nic)
def __init__(self,brserType,DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle
def __init__(self,prjName,brsertype,DUTIP): self.prjName=prjName self.browserType=brsertype self.DUTIP=DUTIP self.pubmodul=PubModuleCase(DUTIP) st=self.pubmodul.ConnectionServer() print "Connection status %d."%st if st != 200 : print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome" : print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox" : print "Starting firefox browser..." self.driver = webdriver.Firefox() self.pubmodul.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle
class Ports: def __init__(self, prjName, brsertype, DUTIP): self.prjName = prjName self.browserType = brsertype self.DUTIP = DUTIP self.pubmodul = PubModuleCase(DUTIP) st = self.pubmodul.ConnectionServer() print "Connection status %d." % st if st != 200: print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.pubmodul.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle def StartPortsWeb(self): print "========StartPortsWeb========" domain = "http://admin:@%s" % (self.DUTIP) self.driver.get(domain) assert (self.driver.title == self.prjName) print "start web successfully!" self.pubmodul.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Configuration")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Ports")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("ports.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("ports.htm")).click() def Web_Config(self): self.pubmodul.location("/html/frameset/frameset/frame[2]") config = {} for i in range(1, 9): config[i] = {} if i <= 4: cf = random.choice([ '0A0A0A0A0', '1A1A0A0A0', '1A0A1A0A0', '1A0A1A1A0', '1A0A2A0A0', '1A0A2A1A0', '1A0A3A1A0' ]) else: cf = random.choice( ['0A0A0A0A0', '1A1A0A0A0', '1A0A2A1A0', '1A0A3A1A0']) config[i]['configured'] = cf speed = str("speed_%d" % i) select1 = Select(self.driver.find_element_by_id(speed)) select1.select_by_value(cf) time.sleep(1) flow = str("flow_%d" % i) self.driver.find_element_by_id(flow).click() framesize = random.randint(1518, 9600) framesize = str(framesize) config[i]['max'] = framesize max = str("max_%d" % i) self.driver.find_element_by_id(max).clear() self.driver.find_element_by_id(max).send_keys(framesize) if i <= 4: exc = str("exc_%d" % i) exc_value = random.choice(['0', '1']) config[i]['exc'] = "exc_value" select2 = Select(self.driver.find_element_by_id(exc)) select2.select_by_value(exc_value) time.sleep(1) pwr = str("pwr_%d" % i) pwr_value = random.choice(['0', '1', '2', '3']) config[i]['pwr'] = pwr_value select3 = Select(self.driver.find_element_by_id(pwr)) select3.select_by_value(pwr_value) time.sleep(1) desc = string.join( random.sample('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^*', 31)).replace(" ", "") config[i]['desc'] = desc name = str("name_%d" % i) self.driver.find_element_by_id(name).send_keys(desc) time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(1) return config def DelConf(self): self.pubmodul.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Maintenance").click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text( "Factory Defaults")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Factory Defaults")).click() self.pubmodul.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_name("factory")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_name("factory")).click() temp = 1 while True: try: elem = self.driver.find_element_by_xpath("/html/body/h1") if elem.text == "Configuration Factory Reset Done": print "=======Factory Default successfully======\n" break raise error except: time.sleep(3) if temp != 10: temp = temp + 1 else: print "there is an error while factory default" break def Reboot(self): print "ready to reboot" time.sleep(5) self.pubmodul.location("/html/frameset/frameset/frame[1]") re = self.pubmodul.DutReboot() return re def CheckConfig(self, config): self.pubmodul.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Configuration")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Ports")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("ports.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("ports.htm")).click() self.pubmodul.location("/html/frameset/frameset/frame[2]") for i in range(1, 9): speed = str("speed_%d" % i) select1 = Select(self.driver.find_element_by_id(speed)) speed = select1.first_selected_option.text if cmp(speed, config[i]['configured']): print "the configuration changed in port %d,configured" % i sys.exit() max = str("max_%d" % i) max = self.driver.find_element_by_id(max).get_attribute('value') if cmp(max, config[i]['max']): print "the configuration changed in port %d,max frame" % i sys.exit() if i <= 4: exc = str("exc_%d" % i) select2 = Select(self.driver.find_element_by_id(exc)) exc = select2.first_selected_option.text if cmp(exc, config[i]['exc']): print "the configuration changed in port %d,excessive" % i sys.exit() pwr = str("pwr_%d" % i) select3 = Select(self.driver.find_element_by_id(pwr)) pwr = select3.first_selected_option.text if cmp(pwr, config[i]['pwr']): print "the configuration changed in port %d,power control" % i sys.exit() name = str("name_%d" % i) name = self.driver.find_element_by_id(name).get_attribute('value') if cmp(name, config[i]['desc']): print "the configuration changed in port %d,Description" % i sys.exit() print "all configuration is correct !!" print "Pass\n" def CloseSession(self): self.driver.close()
class LimitControl: def __init__(self, prjName, brsertype, DUTIP, managenic, datanic): self.managenic = managenic self.datanic = datanic self.prjName = prjName.lower() self.browserType = brsertype.lower() self.DUTIP = DUTIP self.pubmodul = PubModuleCase(DUTIP) st = self.pubmodul.ConnectionServer() print "Connection status %d." % st if st != 200: print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.pubmodul.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle def StartWeb(self): print "========StartPortsWeb========" domain = "http://admin:@%s" % (self.DUTIP) self.driver.get(domain) # assert (self.driver.title == self.prjName) print "start web successfully!" self.pubmodul.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Configuration")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Security")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver. find_element_by_link_text("Security")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Network")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver. find_element_by_link_text("Network")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Limit Control")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Limit Control")).click() def WebConfig(self): self.pubmodul.location("/html/frameset/frameset/frame[2]") select = Select(self.driver.find_element_by_id("glbl_ena")) select.select_by_visible_text("Enabled") age = str(random.randint(10, 10000000)) self.driver.find_element_by_id("aging_enabled").click() self.driver.find_element_by_id("aging_period").clear() self.driver.find_element_by_id("aging_period").send_keys(age) config = {} config['age'] = age if self.prjName == 's3280': j = 9 if self.prjName == 's4140': j = 5 for i in range(1, j): config[i] = {} ena = str("ena_%d" % i) select1 = Select(self.driver.find_element_by_id(ena)) select1.select_by_value("1") limit_value = str(random.randint(1, 1024)) limit = str("limit_%d" % i) config[i]['limit'] = limit_value self.driver.find_element_by_id(limit).clear() self.driver.find_element_by_id(limit).send_keys(limit_value) action = str('action_%d' % i) action_value = random.choice(['0', '1', '2', '3']) config[i]['action'] = action_value select2 = Select(self.driver.find_element_by_id(action)) select2.select_by_value(action_value) time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(2) return config def FactoryDefault(self): self.pubmodul.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Maintenance").click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text( "Factory Defaults")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Factory Defaults")).click() self.pubmodul.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_name("factory")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_name("factory")).click() temp = 1 while True: try: elem = self.driver.find_element_by_xpath("/html/body/h1") if elem.text == "Configuration Factory Reset Done": print "=======Factory Default successfully======\n" break raise error except: time.sleep(3) if temp != 10: temp = temp + 1 else: print "there is an error while factory default" break def CheckConfig(self, config): self.StartWeb() self.pubmodul.location("/html/frameset/frameset/frame[2]") value = self.driver.find_element_by_id("aging_period").get_attribute( "value") if config['age'] != value: print "configuration changed" print "failed" sys.exit() if self.prjName == 's3280': j = 9 if self.prjName == 's4140': j = 5 for i in range(1, j): limit = str("limit_%d" % i) value = self.driver.find_element_by_id(limit).get_attribute( "value") if config[i]['limit'] != value: print "configuration changed" print "failed" sys.exit() action = str('action_%d' % i) value = self.driver.find_element_by_id(action).get_attribute( "value") if config[i]['action'] != value: print "configuration changed" print "failed" sys.exit() print "configuration not changed" print "successfully" def Restart(self): time.sleep(1) re = self.pubmodul.DutReboot() if re == 0: print "dut reboot failed,please check the dut" sys.exit() else: print "dut reboot successfully" def CloseSession(self): self.driver.close() def Getlimit(self): return self.limit def FunctionCheck(self, testport): portnum = testport[-1] self.pubmodul.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Limit Control").click() time.sleep(1) self.pubmodul.location("/html/frameset/frameset/frame[2]") if os.geteuid() != 0: print "Please run as root." sys.exit() print "the test port is set to port%s!!!!" % portnum limit_index = "limit_%s" % portnum self.limit = self.driver.find_element_by_id(limit_index).get_attribute( "value") print "the max num of mac address in port%s is %s" % (portnum, self.limit) print "set the action of port%s as Shutdown" % portnum action_index = "action_%s" % portnum select = Select(self.driver.find_element_by_id(action_index)) select.select_by_visible_text("Shutdown") time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(1) print "now we send the over limit-num of mac address" self.limit = int(self.limit) + 1 self.mac = self.pubmodul.MacIncrease('00:00:00:00:00:01', self.limit, 2) #sendp((Ether(src=self.mac,dst='ff:ff:ff:ff:ff:ff')),iface='eth2',inter=0.1) sendp((Ether(src=self.mac, dst='ff:ff:ff:ff:ff:ff')), iface='eth1', inter=0.1) time.sleep(1) self.driver.find_element_by_xpath("html/body/div/form/input").click() time.sleep(1) openindex = "/config/psec_limit_reopen?port=%s" % portnum elem = self.driver.find_element_by_name(openindex) if elem.is_enabled() == True: print "port%s's limit action of Shutdown works well" % portnum else: print "port%s did not Shutdown while the mac address exceed the limit" % portnum print "failed" sys.exit() elem.click() time.sleep(1) print "set the action of port%s as trap" % portnum select = Select(self.driver.find_element_by_id(action_index)) select.select_by_visible_text("Trap") self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(1) self.TrapTest(self.managenic, self.datanic, portnum) timeout = self.limit / 10 + 11 time.sleep(timeout) print "stat to test the ShutdownandTrap" select = Select(self.driver.find_element_by_id(action_index)) select.select_by_visible_text("Trap & Shutdown") time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(1) self.TrapTest(self.managenic, self.datanic, portnum) time.sleep(timeout) self.driver.find_element_by_xpath("html/body/div/form/input").click() time.sleep(1) try: self.driver.find_element_by_name(openindex).click() print "port%s's limit action of Shutdown works well" % portnum except: print "port%s did not Shutdown while the mac address exceed the limit" % portnum print "failed" sys.exit() print "pass" def SendMac(self, iface='eth2'): print "now we send the over limit-num of mac address" sendp((Ether(src=self.mac, dst='ff:ff:ff:ff:ff:ff')), iface=iface, inter=0.1) time.sleep(1) def CapTrap(self, iface='eth1', port='1'): timeout = self.limit / 10 + 10 filter = "udp and port 162 and src %s" % self.DUTIP pkg = sniff(iface=iface, filter=filter, timeout=timeout) num = len(pkg) if num == 0: print "can not capture the trap" print "failed0" else: try: #the last number of the oid is the index of port #if the test port is port1,then the oid is 1.X.X.X.1 #if the test port is port 2 ,then the oid is 1.X.X.2 port = port oid = '1.3.6.1.4.1.868.2.5.3.1.4.4.1.4.%s' % port print oid if pkg[0][SNMP][5].oid.val == oid: print "capture the trap for the macreached" print "Pass" else: print "can not capture the trap" print "failed1" except: print "can not capture the trap" print "failed2" sys.exit() def TrapTest(self, managenic, datanic, port): managenic = managenic datanic = datanic port = port t1 = threading.Thread(target=self.CapTrap, args=(managenic, port)) t1.start() t2 = threading.Thread(target=self.SendMac, args=(datanic, )) t2.start() def SnmpConfig(self): parameters = PubReadConfig("") pc_ip = parameters.GetParameter("PC", "MGMTIP") print "pc_ip:" + pc_ip self.pubmodul.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Switch")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("Switch")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("SNMP")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("SNMP")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("snmp.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("snmp.htm")).click() self.pubmodul.location("/html/frameset/frameset/frame[2]") select1 = Select(self.driver.find_element_by_id("trap_mode")) select1.select_by_visible_text("Enabled") select2 = Select(self.driver.find_element_by_id("trap_version")) select2.select_by_visible_text("SNMP v2c") des = self.driver.find_element_by_id("trap_dip") des.clear() des.send_keys(pc_ip) select3 = Select(self.driver.find_element_by_id("trap_inform_mode")) select3.select_by_visible_text("Disabled") self.driver.find_element_by_xpath("/html/body/form/p/input").click()
class WebMonitorDMIClassCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self,prjName,brserType,DUTIP): self.prjName = prjName.lower() self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200: print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebMonitorDMI(self,prjName): print "=====startWebMonitorDMI=====" domain = "http://admin:@%s"%(self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" def ClickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath("//input[@value='Save']").send_keys(Keys.RETURN) if self.browserType == "chrome": self.driver.find_element_by_xpath("//input[@value='Save']").click() def EngineWebMonitorDMI(self): print "=====engineWebMonitorDMI====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(1) #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Monitor")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Monitor")).click() #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Ports")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(" stat_dmi_detailed.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(" stat_dmi_detailed.htm")).click() def ReadMonitorDMI(self): print "========readMonitorDMI======" time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") if self.prjName == 's3280': index=5 elif self.prjName == 's4140': index=1 for i in range (1,5): value=str(index) select=Select(self.driver.find_element_by_id("portselect")) select.select_by_value(value) time.sleep(1) threshlod=self.driver.find_element_by_xpath("/html/body/table[2]/tbody/tr[4]/td[2]/label").text print "Rx Power Intrusion Threshold of port%s is %s"%(value,threshlod) index=index+1 self.driver.close() def ConfigDMI(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Configuration").click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Ports")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("DMI")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("DMI")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") if self.prjName == 's3280': index=5 elif self.prjName == 's4140': index=1 self.config=[] for i in range (1,5): time.sleep(1) ID = str("rpit_%d"%index) value=random.randint(0,65535) value=str(value) self.config.append(value) self.driver.find_element_by_id(ID).clear() self.driver.find_element_by_id(ID).send_keys(value) index=index+1 self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(1) def DelConf(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Maintenance").click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Factory Defaults")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Factory Defaults")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_name("factory")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_name("factory")).click() temp = 1 while True: try: elem = self.driver.find_element_by_xpath("/html/body/h1") if elem.text == "Configuration Factory Reset Done": print "=======Factory Default successfully======\n" break raise error except: time.sleep(3) if temp != 10: temp = temp+1 else: print "there is an error while factory default" break def Reboot(self): print "ready to reboot" time.sleep(5) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") try: re=self.PubModuleEle.DutReboot() except: print "there is an error while reboot dut" print "failed" sys.exit() if not re: print "DUT reboot failed,please check it" sys.exit() def CheckConfig(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Configuration").click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Ports")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("DMI")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("DMI")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") if self.prjName == 's3280': index=5 elif self.prjName == 's4140': index=1 for i in range (0,4): ID = str("rpit_%d"%index) value=self.driver.find_element_by_id(ID).get_attribute('value') if self.config[i] != value: print "config changed after reboot,please check the dut" sys.exit() index=index+1 print "configuration not changed " print "Pass"
class Acess_manage(): def __init__(self): print ("Get parameters from Configuration file") parameters=PubReadConfig("") global caseName caseName = sys.argv[0] caseName = caseName[:-3] global prjName prjName =parameters.GetParameter("DUT1","PRODUCT") global brserType brserType=parameters.GetParameter("PC","BRWSRTYPE") global DUTIP DUTIP = parameters.GetParameter("DUT1","IP") self.switch_type = prjName self.brower_type = brserType self.URL = DUTIP global netcard netcard = parameters.GetParameter("PC","MGMTNIC") global netcardip netcardip = parameters.GetParameter("PC","MGMTIP") self.conn = PubModuleCase(self.URL) self.st1 = self.conn.ConnectionServer() if self.st1 != 200: print "connect to server failed...%d" %self.st1 sys.exit() self.driver = self.conn.LoginToWeb() self.mac = self.conn.GetDutMac() print "the mac of dut is %s"%self.mac def into_manage_web(self): self.conn.location("/html/frameset/frameset/frame[1]") print self.brower_type if (self.switch_type=="S3280"): self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/a").click() time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[4]/a").click() time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[4]/div/ul/li/a").click() time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[4]/div/ul/li/div/ul/li[6]/a").click() elif (self.switch_type=="S4140"): self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/a").click() time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[3]/a").click() time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[3]/div/ul/li/a").click() time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[3]/div/ul/li/div/ul/li[6]/a").click() self.conn.location("/html/frameset/frameset/frame[2]") def clicksavebutton(self): self.conn.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("/html/body/form/p[3]/input")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_xpath("/html/body/form/p[3]/input")).click() def clickaddbutton(self): self.conn.location("/html/frameset/frameset/frame[2]") self.driver.find_element_by_xpath("/html/body/form/p[2]/input").click() def check_entry_num(self): time.sleep(5) entry_list1 = self.driver.find_elements_by_class_name("config_even") entry_list2 = self.driver.find_elements_by_class_name("config_odd") i = 0 for entry in entry_list1: i = i+1 for entry in entry_list2: i = i+1 return i def add_client(self): self.conn.location("/html/frameset/frameset/frame[2]") entrynum = self.check_entry_num() print "==========the number of access management entry is %d==========" %entrynum while entrynum: self.conn.ScreenshotSele("/html/frameset/frameset/frame[2]","acess_manage",self.switch_type) print "==========please factory default the access_manage entry==========" sys.exit() self.select = Select(self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr/td[2]/select")) self.select.select_by_value("1") self.clickaddbutton() local_ip = netcardip ip_num = socket.ntohl(struct.unpack("I",socket.inet_aton(local_ip))[0]) print "your local address is %s"%local_ip self.driver.find_element_by_xpath("/html/body/form/table[2]/tbody/tr[2]/td[2]/input").clear() self.driver.find_element_by_xpath("/html/body/form/table[2]/tbody/tr[2]/td[2]/input").send_keys(local_ip) ip_num = ip_num+1 end_ip = socket.inet_ntoa(struct.pack('I',socket.htonl(ip_num))) self.driver.find_element_by_xpath("/html/body/form/table[2]/tbody/tr[2]/td[3]/input").clear() self.driver.find_element_by_xpath("/html/body/form/table[2]/tbody/tr[2]/td[3]/input").send_keys(end_ip) self.driver.find_element_by_xpath("/html/body/form/table[2]/tbody/tr[2]/td[4]/input").click() self.driver.find_element_by_xpath("/html/body/form/table[2]/tbody/tr[2]/td[5]/input").click() self.driver.find_element_by_xpath("/html/body/form/table[2]/tbody/tr[2]/td[6]/input").click() self.clicksavebutton() print "============================================" print " creat the entry successfully " print " use legal ip http server " print "============================================" st = self.conn.ConnectionServer() if st==200: print "pass!" else: print "the ip cannot http server" sys.exit() #time.sleep(2) time.sleep(2) print "============================================" print " use legal ip https server " print "============================================" st = self.conn.ConnectionServer(httpmode="https") if st==200: print "pass!" else: print "the ip cannot https server" sys.exit() #time.sleep(2) time.sleep(2) print "============================================" print " use legal ip snmp to server " print "============================================" result = self.conn.SnmpToServer() if result == 0: sys.exit() #time.sleep(2) time.sleep(2) print "============================================" print " use legal ip telnet to server " print "============================================" self.conn.TelnetToSserver() #time.sleep(1) time.sleep(2) print "============================================" print " use legal ip ssh to server " print "============================================" self.conn.SshToServer() #time.sleep(1) time.sleep(2) print "============================================" print " use illegal ip estabilish tcp to server " print "============================================" re = self.conn.TcpHandShake(portdest=23,nickname=netcard) if re == 0: print "illegal ip could not estabilish telnet tcp connect" print "pass\n" re = self.conn.TcpHandShake(portdest=22,nickname=netcard) if re == 0: print "illegal ip could not estabilish ssh tcp connect" print "pass\n" #### time.sleep(2) print "============================================" print " remove the the snmp in the web and test " print "============================================" self.conn.location("/html/frameset/frameset/frame[2]") self.driver.find_element_by_xpath("/html/body/form/table[2]/tbody/tr[2]/td[5]/input").click() self.clicksavebutton() time.sleep(2) result2 = self.conn.SnmpToServer() if result2 == 0: print "illegal ip can not snmp to server,successfully" else: print "illegal ip can snmp to server,failed" sys.exit() print "=====del the entry for access_manage========" print " " self.driver.find_element_by_xpath("/html/body/form/table[2]/tbody/tr[2]/td/input").click() self.select = Select(self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr/td[2]/select")) self.select.select_by_value("0") time.sleep(1) self.clicksavebutton() print "Function test is over,pass" def add_to_full(self): print "now add the entry to the max num\n" self.driver.find_element_by_xpath("/html/body/form/p[2]/input").click() ip_first = "1.1.1.1" i = 1 j = 2 ip_first_num = socket.ntohl(struct.unpack("I",socket.inet_aton(ip_first))[0]) while True: m = r"/html/body/form/table[2]/tbody/tr[%d]/td[2]/input"%j try: alert = self.driver.switch_to_alert() alert.accept() global max_num max_num = i-1 print "the max num of the entry is %d\n"%max_num break except: #ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath(m)).perform() #WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_xpath(m)).clear() while True: try: ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath(m)).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_xpath(m)).clear() break except: time.sleep(7) self.driver.find_element_by_xpath(m).send_keys(ip_first) ip_sec_num = ip_first_num+1 ip_sec = socket.inet_ntoa(struct.pack('I',socket.htonl(ip_sec_num))) a = r"/html/body/form/table[2]/tbody/tr[%d]/td[3]/input"%j time.sleep(1) self.driver.find_element_by_xpath(a).clear() self.driver.find_element_by_xpath(a).send_keys(ip_sec) b = r"/html/body/form/table[2]/tbody/tr[%d]/td[4]/input"%j self.driver.find_element_by_xpath(b).click() c = r"/html/body/form/table[2]/tbody/tr[%d]/td[5]/input"%j self.driver.find_element_by_xpath(c).click() d = r"/html/body/form/table[2]/tbody/tr[%d]/td[6]/input"%j self.driver.find_element_by_xpath(d).click() self.driver.find_element_by_xpath("/html/body/form/p[3]/input").click() time.sleep(1) ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("/html/body/form/p[2]/input")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_xpath("/html/body/form/p[2]/input")).click() time.sleep(1) j = j+1 i = i+1 ip_first_num = ip_sec_num+1 ip_first = socket.inet_ntoa(struct.pack('I',socket.htonl(ip_first_num))) def del_all(self): print "now clear all the entry:\n" loop = max_num+1 for i in range (1 , loop): self.driver.find_element_by_xpath("/html/body/form/table[2]/tbody/tr[2]/td/input").click() self.clicksavebutton() time.sleep(1) print "all entry has been delete" def reb_check(self): re=self.conn.DutReboot() if re == 0: sys.exit() self.into_manage_web() num = self.check_entry_num() if (num==max_num): print "check Element successfully" else: print "check Element failed" sys.exit() def factory_default(self): self.conn.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Maintenance")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_link_text("Maintenance")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Factory Defaults")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_link_text("Factory Defaults")).click() self.conn.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_name("factory")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_name("factory")).click() temp = 1 while True: try: elem = self.driver.find_element_by_xpath("/html/body/h1") if elem.text == "Configuration Factory Reset Done": print "=======Factory Default successfully======\n" break raise error except: time.sleep(3) if temp != 10: temp = temp+1 else: print "there is an error while factory default" break def CloseSession(self): self.driver.close()
class ConfigCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self, brserType, DUTIP): self.pubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.pubModuleEle.ConnectionServer() print "Connection status %d." % st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.pubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def startWebConfig(self, prjName): print "\n*******************StartWebMac*******************" domain = "http://admin:@%s" % (self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "Start web successfully!" def clickBackupButton(self): print "\nBackup in Processing...\n" if self.browserType == "firefox": self.driver.find_element_by_xpath("/html/body/form/input").submit() if self.browserType == "chrome": self.driver.find_element_by_xpath("/html/body/form/input").click() def engineConfigBackup(self): print "\n************EngineConfigurationBackup************\n" self.pubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(1) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li[4]/a").click( ) #find "Maintenance" time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li[4]/div/ul/li[4]/a" ).click() #find "Configuration" time.sleep(2) #self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[4]/div/ul/li[4]/div/ul/li/a").click() #find "Backup Binary" #time.sleep(2) #self.pubModuleEle.location("/html/frameset/frameset/frame[2]") #time.sleep(1) #self.clickBackupButton() print "Technical Difficulties: can't close the popup windows...\n" time.sleep(5) print "***********ConfigurationBackupFinish*************" def engineConfigRestore(self): print "\n************EngineConfigurationRestore***********" #self.pubModuleEle.location("/html/frameset/frameset/frame[1]") #time.sleep(1) #self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[4]/a").click() #find "Maintenance" #time.sleep(1) #self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[4]/div/ul/li[4]/a").click() #find "Configuration" #time.sleep(1) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li[4]/div/ul/li[4]/div/ul/li[2]/a" ).click() #find "Restore Binary" time.sleep(1) self.clickRestoreButton() time.sleep(1) print "***********ConfigurationRestoreFinish************\n" def clickRestoreButton(self): print "\nRestore in Processing..." self.pubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(1) #self.driver.find_element_by_xpath("/html/body/form/input").send_keys("D:\FirefoxDownload\S3280_v1.6.0-May21-bugfix_conf.bin") # for windows OS self.driver.find_element_by_xpath("/html/body/form/input").send_keys( "/home/olivia/Downloads/S3280_v1.5.4_conf.bin") #(for linux OS) time.sleep(1) self.pubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/input[2]").click() print "\nPlease wait for 90 seconds ...\n" time.sleep(90)
class WebUserCase: #Initialize module, will include "caseName","ProjectName","browser type","ip address","driver object" def __init__(self,brserType,DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebUser(self,prjName): print "=======startWebUser=======" domain = "http://admin:@%s"%(self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" #Finding the "users" table in the web def EngineUser(self): print "========engineUser========" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Security")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Security")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Switch")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Switch")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Users")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Users")).click() #Because the operation way of the "submit" button is different in the different broswer, so we need distinguish to use. def ClickButtonType(self): if self.browserType == "firefox": WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("addNewEntry")).send_keys(Keys.RETURN) if self.browserType == "chrome": WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("addNewEntry")).click() def ClickAddUserSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath("//input[@value='Save']").send_keys(Keys.RETURN) if self.browserType == "chrome": self.driver.find_element_by_xpath("//input[@value='Save']").click() #add the new users. def AddNewUser(self,userNum): time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") print '=========addNewUser=======' for i in range(1,userNum+1): time.sleep(2) prefix = 'user_' usrname="%s%d"%(prefix,i) print usrname self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.ClickButtonType() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("username")).perform() WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("username")).send_keys(usrname) ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("password1")).perform() WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("password1")).send_keys("123456") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("password2")).perform() WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("password2")).send_keys("123456") self.PubModuleEle.SetSelectElement() self.ClickAddUserSaveButton() time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("addNewEntry")) #check the default user is "admin" and the level is 15, or report errors and screenshots def CheckUser(self,caseName,prjName): print "=========checkUser========" time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") x = self.driver.execute_script('var tluser=document.getElementById("userConfigs");return tluser.rows.length;') userNumerr="Existing %d users in the system!"%(x) print userNumerr if x != 1: print "ERROR:Existing more than one user in the system,Please delete all except for admin!" self.PubModuleEle.ScreenshotSele("/html/frameset/frameset/frame[2]",caseName,prjName) re=self.PubModuleEle.DutReboot(mode='fd') if not re: print "factory default failed" sys.exit() self.StartWebUser(prjName) self.EngineUser() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") if x == 1: adminUser = self.driver.execute_script('var tluser=document.getElementById("userConfigs");return tluser.childNodes[0].childNodes[0].childNodes[0].text;') print adminUser privi = self.driver.execute_script('var tluser=document.getElementById("userConfigs");return tluser.childNodes[0].childNodes[1].innerHTML;') print privi if adminUser != "admin" or privi != "15": print "The user is not admin, or the priviledge is not 15!" #tStr = str(time.strftime("%Y%m%d%H%M%S",time.localtime())) #strT = "F:\\selenium\\"+tStr+".png" #print strT self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.PubModuleEle.screenshotSele("/html/frameset/frameset/frame[2]",caseName,prjName) #self.driver.get_screenshot_as_file(strT) self.driver.quit() print "===please factory default Device====" re=self.PubModuleEle.DutReboot(mode='fd') if not re: print "factory default failed" sys.exit() self.StartWebUser(prjName) self.EngineUser() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") print "=start to execute user cases=" def AddUserHandle(self,userNum,caseName,prjName): print "=======addUserHandle======" self.driver.implicitly_wait(5) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.CheckUser(caseName,prjName) #Judge Legal self.AddNewUser(userNum) #Add new user #delete the users that have been added. def DeleteUser(self,userNum): print '========deleteUser========' time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") for pos in range(1,userNum+1): time.sleep(2) prefix = 'user_' usrname="%s%d"%(prefix,pos) userInfo = "The No.%d is "%pos+usrname print userInfo #jerry modified WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_xpath("/html/body/table/tbody/tr[2]/td/a")).click() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text(usrname)).click() time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("//input[@value='Delete User']")).perform() WebDriverWait(self.driver, 30).until(lambda driver:driver.find_element_by_xpath("//input[@value='Delete User']")).click() time.sleep(1) self.driver.switch_to_alert().accept() #jerry modified time.sleep(1) #self.PubModuleEle.location("/html/frameset/frameset/frame[2]") #check the users added is existence after rebooting or not. def CheckElem(self,userNum): time.sleep(5) print "========checkElem========" st = self.PubModuleEle.ConnectionServer() print "Connection status %d."%(st) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") self.EngineUser() time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") n = 0 print "Start to check the element...\n" n = self.driver.execute_script('var tluser=document.getElementById("userConfigs");return tluser.rows.length;') n = n-1 if n == userNum: time.sleep(2) print "Add users number is %d."%(n) print "The element checked is existence, check successfully!" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.driver.close() else: time.sleep(2) print "n=%d"%(n) print "The element checked is not existence, check failed!" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.driver.close() #Because the operation way of the "submit" button is different in the different broswer, so we need distinguish to use. def ClickDeleteUserButton(self): if self.browserType == "firefox": WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_name("warm")).send_keys(Keys.RETURN) if self.browserType == "chrome": WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_name("warm")).click() #Find the "Restart Device" table and reboot the system, and check when the server is normal connection. def CheckRebootElement(self,userNum): print "======checkRebootElement=====" time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Switch")).click() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Security")).click() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[1]") WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Maintenance")).click() time.sleep(1) WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Restart Device")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.ClickDeleteUserButton() n = 0 time.sleep(2) while True: n = n + 1 st = self.PubModuleEle.ConnectionServer() if st == 0: print "Connection status %d. Failure... [%d]"%(st,n) if st == 200: print "Connection status %d. Success! [%d]"%(st,n) break time.sleep(5) if n==10: print "No connection,exit after 5 seconds!" time.sleep(5) self.driver.close() print "OK,start to check element!" self.CheckElem(userNum)
class Acess_manage(): def __init__(self): print("Get parameters from Configuration file") parameters = PubReadConfig("") global caseName caseName = sys.argv[0] caseName = caseName[:-3] global prjName prjName = parameters.GetParameter("DUT1", "PRODUCT") global brserType brserType = parameters.GetParameter("PC", "BRWSRTYPE") global DUTIP DUTIP = parameters.GetParameter("DUT1", "IP") self.switch_type = prjName self.brower_type = brserType self.URL = DUTIP global netcard netcard = parameters.GetParameter("PC", "MGMTNIC") global netcardip netcardip = parameters.GetParameter("PC", "MGMTIP") self.conn = PubModuleCase(self.URL) self.st1 = self.conn.ConnectionServer() if self.st1 != 200: print "connect to server failed...%d" % self.st1 sys.exit() self.driver = self.conn.LoginToWeb() self.mac = self.conn.GetDutMac() print "the mac of dut is %s" % self.mac def into_manage_web(self): self.conn.location("/html/frameset/frameset/frame[1]") print self.brower_type if (self.switch_type == "S3280"): self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/a").click() time.sleep(1) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[4]/a" ).click() time.sleep(1) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[4]/div/ul/li/a" ).click() time.sleep(1) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[4]/div/ul/li/div/ul/li[6]/a" ).click() elif (self.switch_type == "S4140"): self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/a").click() time.sleep(1) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[3]/a" ).click() time.sleep(1) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[3]/div/ul/li/a" ).click() time.sleep(1) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li[3]/div/ul/li/div/ul/li[6]/a" ).click() self.conn.location("/html/frameset/frameset/frame[2]") def clicksavebutton(self): self.conn.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_xpath( "/html/body/form/p[3]/input")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_xpath( "/html/body/form/p[3]/input")).click() def clickaddbutton(self): self.conn.location("/html/frameset/frameset/frame[2]") self.driver.find_element_by_xpath("/html/body/form/p[2]/input").click() def check_entry_num(self): time.sleep(5) entry_list1 = self.driver.find_elements_by_class_name("config_even") entry_list2 = self.driver.find_elements_by_class_name("config_odd") i = 0 for entry in entry_list1: i = i + 1 for entry in entry_list2: i = i + 1 return i def add_client(self): self.conn.location("/html/frameset/frameset/frame[2]") entrynum = self.check_entry_num() print "==========the number of access management entry is %d==========" % entrynum while entrynum: self.conn.ScreenshotSele("/html/frameset/frameset/frame[2]", "acess_manage", self.switch_type) print "==========please factory default the access_manage entry==========" sys.exit() self.select = Select( self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr/td[2]/select")) self.select.select_by_value("1") self.clickaddbutton() local_ip = netcardip ip_num = socket.ntohl( struct.unpack("I", socket.inet_aton(local_ip))[0]) print "your local address is %s" % local_ip self.driver.find_element_by_xpath( "/html/body/form/table[2]/tbody/tr[2]/td[2]/input").clear() self.driver.find_element_by_xpath( "/html/body/form/table[2]/tbody/tr[2]/td[2]/input").send_keys( local_ip) ip_num = ip_num + 1 end_ip = socket.inet_ntoa(struct.pack('I', socket.htonl(ip_num))) self.driver.find_element_by_xpath( "/html/body/form/table[2]/tbody/tr[2]/td[3]/input").clear() self.driver.find_element_by_xpath( "/html/body/form/table[2]/tbody/tr[2]/td[3]/input").send_keys( end_ip) self.driver.find_element_by_xpath( "/html/body/form/table[2]/tbody/tr[2]/td[4]/input").click() self.driver.find_element_by_xpath( "/html/body/form/table[2]/tbody/tr[2]/td[5]/input").click() self.driver.find_element_by_xpath( "/html/body/form/table[2]/tbody/tr[2]/td[6]/input").click() self.clicksavebutton() print "============================================" print " creat the entry successfully " print " use legal ip http server " print "============================================" st = self.conn.ConnectionServer() if st == 200: print "pass!" else: print "the ip cannot http server" sys.exit() #time.sleep(2) time.sleep(2) print "============================================" print " use legal ip https server " print "============================================" st = self.conn.ConnectionServer(httpmode="https") if st == 200: print "pass!" else: print "the ip cannot https server" sys.exit() #time.sleep(2) time.sleep(2) print "============================================" print " use legal ip snmp to server " print "============================================" result = self.conn.SnmpToServer() if result == 0: sys.exit() #time.sleep(2) time.sleep(2) print "============================================" print " use legal ip telnet to server " print "============================================" self.conn.TelnetToSserver() #time.sleep(1) time.sleep(2) print "============================================" print " use legal ip ssh to server " print "============================================" self.conn.SshToServer() #time.sleep(1) time.sleep(2) print "============================================" print " use illegal ip estabilish tcp to server " print "============================================" re = self.conn.TcpHandShake(portdest=23, nickname=netcard) if re == 0: print "illegal ip could not estabilish telnet tcp connect" print "pass\n" re = self.conn.TcpHandShake(portdest=22, nickname=netcard) if re == 0: print "illegal ip could not estabilish ssh tcp connect" print "pass\n" #### time.sleep(2) print "============================================" print " remove the the snmp in the web and test " print "============================================" self.conn.location("/html/frameset/frameset/frame[2]") self.driver.find_element_by_xpath( "/html/body/form/table[2]/tbody/tr[2]/td[5]/input").click() self.clicksavebutton() time.sleep(2) result2 = self.conn.SnmpToServer() if result2 == 0: print "illegal ip can not snmp to server,successfully" else: print "illegal ip can snmp to server,failed" sys.exit() print "=====del the entry for access_manage========" print " " self.driver.find_element_by_xpath( "/html/body/form/table[2]/tbody/tr[2]/td/input").click() self.select = Select( self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr/td[2]/select")) self.select.select_by_value("0") time.sleep(1) self.clicksavebutton() print "Function test is over,pass" def add_to_full(self): print "now add the entry to the max num\n" self.driver.find_element_by_xpath("/html/body/form/p[2]/input").click() ip_first = "1.1.1.1" i = 1 j = 2 ip_first_num = socket.ntohl( struct.unpack("I", socket.inet_aton(ip_first))[0]) while True: m = r"/html/body/form/table[2]/tbody/tr[%d]/td[2]/input" % j try: alert = self.driver.switch_to_alert() alert.accept() global max_num max_num = i - 1 print "the max num of the entry is %d\n" % max_num break except: #ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath(m)).perform() #WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_xpath(m)).clear() while True: try: ActionChains(self.driver).move_to_element( self.driver.find_element_by_xpath(m)).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver. find_element_by_xpath(m)).clear() break except: time.sleep(7) self.driver.find_element_by_xpath(m).send_keys(ip_first) ip_sec_num = ip_first_num + 1 ip_sec = socket.inet_ntoa( struct.pack('I', socket.htonl(ip_sec_num))) a = r"/html/body/form/table[2]/tbody/tr[%d]/td[3]/input" % j time.sleep(1) self.driver.find_element_by_xpath(a).clear() self.driver.find_element_by_xpath(a).send_keys(ip_sec) b = r"/html/body/form/table[2]/tbody/tr[%d]/td[4]/input" % j self.driver.find_element_by_xpath(b).click() c = r"/html/body/form/table[2]/tbody/tr[%d]/td[5]/input" % j self.driver.find_element_by_xpath(c).click() d = r"/html/body/form/table[2]/tbody/tr[%d]/td[6]/input" % j self.driver.find_element_by_xpath(d).click() self.driver.find_element_by_xpath( "/html/body/form/p[3]/input").click() time.sleep(1) ActionChains(self.driver).move_to_element( self.driver.find_element_by_xpath( "/html/body/form/p[2]/input")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_xpath( "/html/body/form/p[2]/input")).click() time.sleep(1) j = j + 1 i = i + 1 ip_first_num = ip_sec_num + 1 ip_first = socket.inet_ntoa( struct.pack('I', socket.htonl(ip_first_num))) def del_all(self): print "now clear all the entry:\n" loop = max_num + 1 for i in range(1, loop): self.driver.find_element_by_xpath( "/html/body/form/table[2]/tbody/tr[2]/td/input").click() self.clicksavebutton() time.sleep(1) print "all entry has been delete" def reb_check(self): re = self.conn.DutReboot() if re == 0: sys.exit() self.into_manage_web() num = self.check_entry_num() if (num == max_num): print "check Element successfully" else: print "check Element failed" sys.exit() def factory_default(self): self.conn.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Maintenance")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Maintenance")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text( "Factory Defaults")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Factory Defaults")).click() self.conn.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_name("factory")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_name("factory")).click() temp = 1 while True: try: elem = self.driver.find_element_by_xpath("/html/body/h1") if elem.text == "Configuration Factory Reset Done": print "=======Factory Default successfully======\n" break raise error except: time.sleep(3) if temp != 10: temp = temp + 1 else: print "there is an error while factory default" break def CloseSession(self): self.driver.close()
class LimitControl: def __init__(self,prjName,brsertype,DUTIP,managenic,datanic): self.managenic=managenic self.datanic=datanic self.prjName=prjName.lower() self.browserType=brsertype.lower() self.DUTIP=DUTIP self.pubmodul=PubModuleCase(DUTIP) st=self.pubmodul.ConnectionServer() print "Connection status %d."%st if st != 200 : print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome" : print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox" : print "Starting firefox browser..." self.driver = webdriver.Firefox() self.pubmodul.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle def StartWeb(self): print "========StartPortsWeb========" domain = "http://admin:@%s"%(self.DUTIP) self.driver.get(domain) # assert (self.driver.title == self.prjName) print "start web successfully!" self.pubmodul.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Security")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Security")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Network")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Network")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Limit Control")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Limit Control")).click() def WebConfig(self): self.pubmodul.location("/html/frameset/frameset/frame[2]") select=Select(self.driver.find_element_by_id("glbl_ena")) select.select_by_visible_text("Enabled") age=str(random.randint(10,10000000)) self.driver.find_element_by_id("aging_enabled").click() self.driver.find_element_by_id("aging_period").clear() self.driver.find_element_by_id("aging_period").send_keys(age) config = {} config['age']=age if self.prjName == 's3280': j=9 if self.prjName == 's4140': j=5 for i in range (1,j): config[i]={} ena=str("ena_%d"%i) select1=Select(self.driver.find_element_by_id(ena)) select1.select_by_value("1") limit_value=str(random.randint(1,1024)) limit=str("limit_%d"%i) config[i]['limit']=limit_value self.driver.find_element_by_id(limit).clear() self.driver.find_element_by_id(limit).send_keys(limit_value) action=str('action_%d'%i) action_value=random.choice(['0','1','2','3']) config[i]['action']=action_value select2=Select(self.driver.find_element_by_id(action)) select2.select_by_value(action_value) time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(2) return config def FactoryDefault(self): self.pubmodul.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Maintenance").click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Factory Defaults")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Factory Defaults")).click() self.pubmodul.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_name("factory")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_name("factory")).click() temp = 1 while True: try: elem = self.driver.find_element_by_xpath("/html/body/h1") if elem.text == "Configuration Factory Reset Done": print "=======Factory Default successfully======\n" break raise error except: time.sleep(3) if temp != 10: temp = temp+1 else: print "there is an error while factory default" break def CheckConfig(self,config): self.StartWeb() self.pubmodul.location("/html/frameset/frameset/frame[2]") value=self.driver.find_element_by_id("aging_period").get_attribute("value") if config['age'] != value: print "configuration changed" print "failed" sys.exit() if self.prjName == 's3280': j=9 if self.prjName == 's4140': j=5 for i in range (1,j): limit=str("limit_%d"%i) value=self.driver.find_element_by_id(limit).get_attribute("value") if config[i]['limit'] != value: print "configuration changed" print "failed" sys.exit() action=str('action_%d'%i) value=self.driver.find_element_by_id(action).get_attribute("value") if config[i]['action'] != value: print "configuration changed" print "failed" sys.exit() print "configuration not changed" print "successfully" def Restart(self): time.sleep(1) re=self.pubmodul.DutReboot() if re == 0: print "dut reboot failed,please check the dut" sys.exit() else: print "dut reboot successfully" def CloseSession(self): self.driver.close() def Getlimit(self): return self.limit def FunctionCheck(self,testport): portnum = testport[-1] self.pubmodul.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Limit Control").click() time.sleep(1) self.pubmodul.location("/html/frameset/frameset/frame[2]") if os.geteuid() != 0: print "Please run as root." sys.exit() print "the test port is set to port%s!!!!"%portnum limit_index="limit_%s"%portnum self.limit=self.driver.find_element_by_id(limit_index).get_attribute("value") print "the max num of mac address in port%s is %s"%(portnum,self.limit) print "set the action of port%s as Shutdown"%portnum action_index="action_%s"%portnum select=Select(self.driver.find_element_by_id(action_index)) select.select_by_visible_text("Shutdown") time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(1) print "now we send the over limit-num of mac address" self.limit=int(self.limit)+1 self.mac=self.pubmodul.MacIncrease('00:00:00:00:00:01',self.limit,2) #sendp((Ether(src=self.mac,dst='ff:ff:ff:ff:ff:ff')),iface='eth2',inter=0.1) sendp((Ether(src=self.mac,dst='ff:ff:ff:ff:ff:ff')),iface='eth1',inter=0.1) time.sleep(1) self.driver.find_element_by_xpath("html/body/div/form/input").click() time.sleep(1) openindex="/config/psec_limit_reopen?port=%s"%portnum elem=self.driver.find_element_by_name(openindex) if elem.is_enabled() == True: print "port%s's limit action of Shutdown works well"%portnum else: print "port%s did not Shutdown while the mac address exceed the limit"%portnum print "failed" sys.exit() elem.click() time.sleep(1) print "set the action of port%s as trap"%portnum select=Select(self.driver.find_element_by_id(action_index)) select.select_by_visible_text("Trap") self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(1) self.TrapTest(self.managenic,self.datanic,portnum) timeout=self.limit/10+11 time.sleep(timeout) print "stat to test the ShutdownandTrap" select=Select(self.driver.find_element_by_id(action_index)) select.select_by_visible_text("Trap & Shutdown") time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(1) self.TrapTest(self.managenic,self.datanic,portnum) time.sleep(timeout) self.driver.find_element_by_xpath("html/body/div/form/input").click() time.sleep(1) try: self.driver.find_element_by_name(openindex).click() print "port%s's limit action of Shutdown works well"%portnum except: print "port%s did not Shutdown while the mac address exceed the limit"%portnum print "failed" sys.exit() print "pass" def SendMac(self,iface='eth2'): print "now we send the over limit-num of mac address" sendp((Ether(src=self.mac,dst='ff:ff:ff:ff:ff:ff')),iface=iface,inter=0.1) time.sleep(1) def CapTrap(self,iface='eth1',port='1'): timeout=self.limit/10+10 filter="udp and port 162 and src %s"%self.DUTIP pkg=sniff(iface=iface, filter=filter,timeout=timeout) num=len(pkg) if num == 0: print "can not capture the trap" print "failed0" else: try: #the last number of the oid is the index of port #if the test port is port1,then the oid is 1.X.X.X.1 #if the test port is port 2 ,then the oid is 1.X.X.2 port=port oid = '1.3.6.1.4.1.868.2.5.3.1.4.4.1.4.%s'%port print oid if pkg[0][SNMP][5].oid.val == oid: print "capture the trap for the macreached" print "Pass" else: print "can not capture the trap" print "failed1" except: print "can not capture the trap" print "failed2" sys.exit() def TrapTest(self,managenic,datanic,port): managenic=managenic datanic=datanic port=port t1=threading.Thread(target=self.CapTrap,args=(managenic,port)) t1.start() t2=threading.Thread(target=self.SendMac,args=(datanic,)) t2.start() def SnmpConfig(self): parameters = PubReadConfig("") pc_ip = parameters.GetParameter("PC","MGMTIP") print "pc_ip:" + pc_ip self.pubmodul.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Switch")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Switch")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("SNMP")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("SNMP")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("snmp.htm")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_id("snmp.htm")).click() self.pubmodul.location("/html/frameset/frameset/frame[2]") select1=Select(self.driver.find_element_by_id("trap_mode")) select1.select_by_visible_text("Enabled") select2=Select(self.driver.find_element_by_id("trap_version")) select2.select_by_visible_text("SNMP v2c") des=self.driver.find_element_by_id("trap_dip") des.clear() des.send_keys(pc_ip) select3=Select(self.driver.find_element_by_id("trap_inform_mode")) select3.select_by_visible_text("Disabled") self.driver.find_element_by_xpath("/html/body/form/p/input").click()
class SshCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self, brserType, DUTIP): self.pubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.pubModuleEle.ConnectionServer() print "Connection status %d." % st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.pubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def startWebConfig(self, prjName): print "\n*******************StartWebMac*******************" domain = "http://admin:@%s" % (self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "Start web successfully!" def enabledSSH(self): print "\n************EnabledConfigurationSSH**************\n" self.pubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Configuration")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Security")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver. find_element_by_link_text("Security")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Switch")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("Switch")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("SSH")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("SSH")).click() self.pubModuleEle.location("/html/frameset/frameset/frame[2]") WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("ssh_mode")) select = Select(self.driver.find_element_by_name("ssh_mode")) select.select_by_value("1") self.clickSaveButton() print "\n*************SetSSHenabledFinish*****************\n" print "SSH try to connect to server...\n" def clickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath( "/html/body/form/p/input").click() if self.browserType == "chrome": self.driver.find_element_by_xpath( "/html/body/form/p/input").click() def disabledSSH(self): print "\n***********DisabledConfigurationSSH**************\n" self.pubModuleEle.location("/html/frameset/frameset/frame[2]") WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("ssh_mode")) select = Select(self.driver.find_element_by_name("ssh_mode")) select.select_by_value("0") self.clickSaveButton() print "\n**************SetSSHdisabledFinish***************\n" def checkSSHConfig(self): print "\n*************CheckSSHConfiguration***************" self.pubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Security")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Security")).click() #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Switch")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Switch")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("SSH")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("SSH")).click() self.pubModuleEle.location("/html/frameset/frameset/frame[2]") WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("ssh_mode")) getsel = Select(self.driver.find_element_by_name("ssh_mode")) getval = getsel.first_selected_option.text print "\nCurrent SSH is %s" % getval if getval == "Disabled": print "SSH configuration has no change after reboot...\n" else: getval == "Enabled" print "SSH configuration changed after reboot...\n" print "*************CheckConfigurationFinish************\n"
class ConfigCase: #Initialize module, will include "caseName","ProjectName","browser type","ip address","driver object" def __init__(self,brserType,DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIP = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebConfig(self,prjName): print "\n=======startWebUser=======" domain = "http://admin:@%s"%(self.DUTIP) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" def ClickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath("//input[@value='Save']").send_keys(Keys.RETURN) if self.browserType == "chrome": self.driver.find_element_by_xpath("//input[@value='Save']").click() def engineConfigLog(self,srvaddr): print "\n========engineConfig========" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/a").click()# locate Configuration time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/a").click()#locate System time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/div/ul/li[6]/a").click()# locate log time.sleep(2) print "***********ConfigurationFinish*************" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("server_mode")) select = Select(self.driver.find_element_by_name("server_mode")) select.select_by_value("1") elem=self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td[2]/input") elem.clear() self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td[2]/input").send_keys(srvaddr) s1=self.driver.find_element_by_name("server_addr").get_attribute("value") print "the serveraddr is %s"%s1 WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("syslog_level")) select = Select(self.driver.find_element_by_name("syslog_level")) select.select_by_value("0") self.driver.find_element_by_xpath( "/html/body/form/p/input").click() def CheckConfig(self,pkg): print "\n====CheckConfig====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/a").click() #locate monitor system time.sleep(5) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/div/ul/li[3]/a").click() #locate log self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(8) i = 0 while 1: i = i+1 if i == 1: Levelxpath = "/html/body/table[2]/tbody/tr/td/a" Timexpath = "/html/body/table[2]/tbody/tr/td[3]" Messagexpath = "/html/body/table[2]/tbody/tr/td[4]" else: Levelxpath = "/html/body/table[2]/tbody/tr[%d]/td/a"%i Timexpath = "/html/body/table[2]/tbody/tr[%d]/td[3]"%i Messagexpath = "/html/body/table[2]/tbody/tr[%d]/td[4]"%i try: Levelvalue = self.driver.find_element_by_xpath(Levelxpath).text Timevalue = self.driver.find_element_by_xpath(Timexpath).text Messagevalue = self.driver.find_element_by_xpath(Messagexpath).text Levelvalue = "ID"+Levelvalue except: print "finish read the Syslog table" break if pkg[i-1].find(Levelvalue) != -1 and pkg[i-1].find(Timevalue) != -1 and pkg[i-1].find(Messagevalue) != -1: pass else: print "can't find the sniffered packet in syslog table" raise print "CheckConfig successfully\n" #----------------------------ManualSyslog--------------------------------------- def SendSyslog(self,choice): print "\n====SendSyslog====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Ports")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("ports.htm")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_id("ports.htm")).click() #ports configuration time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") WebDriverWait(self.driver,30).until(lambda driver:driver.find_element_by_id("speed_4")) if choice == 1: Select(self.driver.find_element_by_id("speed_4")).select_by_value("0A0A0A0A0") elif choice == 2: Select(self.driver.find_element_by_id("speed_4")).select_by_value("1A1A0A0A0") time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() #click save time.sleep(10) self.driver.find_element_by_xpath("/html/body/div/form/input").click() # refresh print"send manul Info" def SyslogTest(self,choice): print "\n====SyslogTest====" t1=threading.Thread(target=self.CapSyslog,args=(choice,)) t1.start() t2=threading.Thread(target=self.SendSyslog,args=(choice,)) t2.start() t1.join() t2.join() def CapSyslog(self,choice=0,iface="eth0"): package = [] print "\n====CapSyslog====" if choice == 0: timeout = 70 else: timeout = 30 filter1="udp and port 514 and src 192.168.3.59" pkg=sniff(iface=iface,filter=filter1,timeout=timeout) num=len(pkg) if num==0: print "can not capture the Syslog" raise else: for i in xrange(num): try: string = pkg[i][3].load except: continue if string.find("syslog - ID") != -1: package.append(pkg[i][3].load) print "CapSyslog successfully" print package if choice == 0: self.CheckConfig(pkg = package) if choice == 1: self.ManuallyCheckConfig(pkg = package,choice = 1) elif choice == 2: self.ManuallyCheckConfig(pkg = package,choice = 2) def ManuallyCheckConfig(self,pkg,choice): print "\n====ManuallyCheckConfig====" if choice == 1: if pkg[0].find("Link down on port 4") != -1: pass else: print "the sniffered packet is incorrect" raise if choice == 2: if pkg[0].find("Link up on port 4") != -1: pass else: print "the sniffered packet is incorrect" raise self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/a").click() #locate monitor system time.sleep(5) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/div/ul/li[3]/a").click() #locate log self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(8) i = 0 while 1: i = i+1 if i == 1: Levelxpath = "/html/body/table[2]/tbody/tr/td/a" Timexpath = "/html/body/table[2]/tbody/tr/td[3]" Messagexpath = "/html/body/table[2]/tbody/tr/td[4]" else: Levelxpath = "/html/body/table[2]/tbody/tr[%d]/td/a"%i Timexpath = "/html/body/table[2]/tbody/tr[%d]/td[3]"%i Messagexpath = "/html/body/table[2]/tbody/tr[%d]/td[4]"%i try: Levelvalue = self.driver.find_element_by_xpath(Levelxpath).text Timevalue = self.driver.find_element_by_xpath(Timexpath).text Messagevalue = self.driver.find_element_by_xpath(Messagexpath).text Levelvalue = "ID"+Levelvalue except: print "finish read the table" i = 0 break if pkg[0].find(Timevalue) != -1 and pkg[0].find(Messagevalue) != -1: break if i == 0: print "can't find the sniffered packet in syslog table" raise print "ManuallyCheckConfig successfully\n"
class Ports: def __init__(self,prjName,brsertype,DUTIP): self.prjName=prjName self.browserType=brsertype self.DUTIP=DUTIP self.pubmodul=PubModuleCase(DUTIP) st=self.pubmodul.ConnectionServer() print "Connection status %d."%st if st != 200 : print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome" : print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox" : print "Starting firefox browser..." self.driver = webdriver.Firefox() self.pubmodul.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle def StartPortsWeb(self): print "========StartPortsWeb========" domain = "http://admin:@%s"%(self.DUTIP) self.driver.get(domain) assert (self.driver.title == self.prjName) print "start web successfully!" self.pubmodul.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Ports")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("ports.htm")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_id("ports.htm")).click() def Web_Config(self): self.pubmodul.location("/html/frameset/frameset/frame[2]") config={} for i in range (1,9): config[i]={} if i<=4: cf=random.choice(['0A0A0A0A0','1A1A0A0A0','1A0A1A0A0','1A0A1A1A0','1A0A2A0A0','1A0A2A1A0','1A0A3A1A0']) else: cf=random.choice(['0A0A0A0A0','1A1A0A0A0','1A0A2A1A0','1A0A3A1A0']) config[i]['configured']=cf speed=str("speed_%d"%i) select1=Select(self.driver.find_element_by_id(speed)) select1.select_by_value(cf) time.sleep(1) flow=str("flow_%d"%i) self.driver.find_element_by_id(flow).click() framesize=random.randint(1518,9600) framesize=str(framesize) config[i]['max']=framesize max=str("max_%d"%i) self.driver.find_element_by_id(max).clear() self.driver.find_element_by_id(max).send_keys(framesize) if i <= 4: exc=str("exc_%d"%i) exc_value=random.choice(['0','1']) config[i]['exc']="exc_value" select2=Select(self.driver.find_element_by_id(exc)) select2.select_by_value(exc_value) time.sleep(1) pwr=str("pwr_%d"%i) pwr_value=random.choice(['0','1','2','3']) config[i]['pwr']=pwr_value select3=Select(self.driver.find_element_by_id(pwr)) select3.select_by_value(pwr_value) time.sleep(1) desc=string.join(random.sample('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^*',31)).replace(" ","") config[i]['desc']=desc name=str("name_%d"%i) self.driver.find_element_by_id(name).send_keys(desc) time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(1) return config def DelConf(self): self.pubmodul.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Maintenance").click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Factory Defaults")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Factory Defaults")).click() self.pubmodul.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_name("factory")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_name("factory")).click() temp = 1 while True: try: elem = self.driver.find_element_by_xpath("/html/body/h1") if elem.text == "Configuration Factory Reset Done": print "=======Factory Default successfully======\n" break raise error except: time.sleep(3) if temp != 10: temp = temp+1 else: print "there is an error while factory default" break def Reboot(self): print "ready to reboot" time.sleep(5) self.pubmodul.location("/html/frameset/frameset/frame[1]") re=self.pubmodul.DutReboot() return re def CheckConfig(self,config): self.pubmodul.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Ports")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("ports.htm")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_id("ports.htm")).click() self.pubmodul.location("/html/frameset/frameset/frame[2]") for i in range (1,9): speed=str("speed_%d"%i) select1=Select(self.driver.find_element_by_id(speed)) speed=select1.first_selected_option.text if cmp(speed,config[i]['configured']): print "the configuration changed in port %d,configured"%i sys.exit() max=str("max_%d"%i) max=self.driver.find_element_by_id(max).get_attribute('value') if cmp(max,config[i]['max']): print "the configuration changed in port %d,max frame"%i sys.exit() if i <= 4: exc=str("exc_%d"%i) select2=Select(self.driver.find_element_by_id(exc)) exc=select2.first_selected_option.text if cmp(exc,config[i]['exc']): print "the configuration changed in port %d,excessive"%i sys.exit() pwr=str("pwr_%d"%i) select3=Select(self.driver.find_element_by_id(pwr)) pwr=select3.first_selected_option.text if cmp(pwr,config[i]['pwr']): print "the configuration changed in port %d,power control"%i sys.exit() name=str("name_%d"%i) name=self.driver.find_element_by_id(name).get_attribute('value') if cmp(name,config[i]['desc']): print "the configuration changed in port %d,Description"%i sys.exit() print "all configuration is correct !!" print "Pass\n" def CloseSession(self): self.driver.close()
class SoftUploadCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self,brserType,DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200: print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebSoftUpload(self,prjName): print "========startWebMac========" domain = "http://admin:@%s"%(self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" def ClickUploadButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath("//input[@value='Upload']").submit() if self.browserType == "chrome": self.driver.find_element_by_xpath("//input[@value='Upload']").click() def EngineSoftUpload(self): print "=======engineSoftUpload====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(1) ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Maintenance")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Maintenance")).click() time.sleep(1) ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Software")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Software")).click() time.sleep(1) ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("upload.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("upload.htm")).click() def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to_alert() if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert.text finally: self.accept_next_alert = True def SetSoftUpload(self,prjName): print "======setSoftUpload========" time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("activate_now")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("activate_now")).click() self.driver.find_element_by_name("firmware").send_keys("./project/prjName/firmware/S3280-v1.5.3.dat") self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(1) self.ClickUploadButton() time.sleep(1) self.driver.switch_to_alert().accept() def SearchSoftUpload(self): print "=======searchSoftUpload====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(1) ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("upload.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("upload.htm")).click() def SoftwareUpload(self,prjName): print "=======softwareUpload======" self.SetSoftUpload(prjName) time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") m = 1 global progress progress = "Initial!" while True: self.PubModuleEle.location("/html/frameset/frameset/frame[2]") progress = self.driver.execute_script('return document.getElementById("ticker");') print "Loop %d."%m judge = not progress if judge != True: break m=m+1 if m>3: break self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.SearchSoftUpload() self.SetSoftUpload(prjName) time.sleep(2) return m
def __init__(self, DUTIP): self.PubModuleEle = PubModuleCase(DUTIP)
class ConfigCase: #Initialize module, will include "caseName","ProjectName","browser type","ip address","driver object" def __init__(self, brserType, DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d." % st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebConfig(self, prjName): print "=======startWebUser=======" domain = "http://admin:@%s" % (self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" def ClickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath( "//input[@value='Save']").send_keys(Keys.RETURN) if self.browserType == "chrome": self.driver.find_element_by_xpath("//input[@value='Save']").click() def engineConfigNtp(self, ntpsrv1, ntpsrv2, ntpsrv3, ntpsrv4, ntpsrv5): print "========engineConfig========" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/a").click( ) # locate Configuration time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/a").click( ) #locate System time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/div/ul/li[4]/a" ).click() time.sleep(2) print "***********ConfigurationFinish*************" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("ntp_mode")) select = Select(self.driver.find_element_by_name("ntp_mode")) select.select_by_value("1") print "***************NTPConfiguration***************************" elem = self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[3]/td[2]/input") elem.clear() self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[3]/td[2]/input").send_keys(ntpsrv1) s1 = self.driver.find_element_by_name("ntp_server1").get_attribute( "value") print "the server1 is %s" % s1 elem = self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[4]/td[2]/input") elem.clear() self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[4]/td[2]/input").send_keys(ntpsrv2) s2 = self.driver.find_element_by_name("ntp_server2").get_attribute( "value") elem = self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[5]/td[2]/input") elem.clear() self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[5]/td[2]/input").send_keys(ntpsrv3) s3 = self.driver.find_element_by_name("ntp_server3").get_attribute( "value") elem = self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[6]/td[2]/input") elem.clear() self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[6]/td[2]/input").send_keys(ntpsrv4) s4 = self.driver.find_element_by_name("ntp_server4").get_attribute( "value") elem = self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[7]/td[2]/input") elem.clear() self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[7]/td[2]/input").send_keys(ntpsrv5) s5 = self.driver.find_element_by_name("ntp_server5").get_attribute( "value") self.driver.find_element_by_xpath("/html/body/form/p/input").click() global s s = [s1, s2, s3, s4, s5] def GetRandomNumber(self, m, n): return random.randint(m, n) def GetRandomString(self, list): return random.choice(list) def engineConfigTime(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/div/ul/li[5]/a" ).click() # locate Time time.sleep(2) def engineRange(self): for i in range(1, 4): self.engineConfigZone() self.engineConfigStart() def engineConfigZone(self): self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) timezone = self.GetRandomString( ['-3600', '1', '4804', '3000', '4200', '6000', '7200']) WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("time_zone")) select = Select(self.driver.find_element_by_name("time_zone")) select.select_by_value(timezone) getval = select.first_selected_option.text print "the timezone is %s" % getval elem = self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[3]/td[2]/input") elem.clear() self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[3]/td[2]/input").send_keys("Cas") WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("mode")) select = Select(self.driver.find_element_by_name("mode")) select.select_by_value("1") time.sleep(2) def engineConfigStart(self): self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) num = self.GetRandomNumber(1, 5) num = str(num) print "The start week random number is %s\n" % num WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("week_s")) select = Select(self.driver.find_element_by_name("week_s")) select.select_by_visible_text(num) string = self.GetRandomString( ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']) WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("day_s")) select = Select(self.driver.find_element_by_name("day_s")) select.select_by_visible_text(string) string = self.GetRandomString([ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]) WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("month_s")) select = Select(self.driver.find_element_by_name("month_s")) select.select_by_visible_text(string) num = self.GetRandomNumber(0, 23) num = str(num) WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("hours_s")) select = Select(self.driver.find_element_by_name("hours_s")) select.select_by_visible_text(num) num = self.GetRandomNumber(0, 59) num = str(num) WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("minutes_s")) select = Select(self.driver.find_element_by_name("minutes_s")) select.select_by_visible_text(num) # config End time settings num = self.GetRandomNumber(1, 5) num = str(num) WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("week_e")) select = Select(self.driver.find_element_by_name("week_e")) select.select_by_visible_text(num) print "the end week random number is %s\n" % num string = self.GetRandomString( ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']) WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("day_e")) select = Select(self.driver.find_element_by_name("day_e")) select.select_by_visible_text(string) string = self.GetRandomString([ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]) WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("month_e")) select = Select(self.driver.find_element_by_name("month_e")) select.select_by_visible_text(string) num = self.GetRandomNumber(0, 23) num = str(num) WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("hours_e")) select = Select(self.driver.find_element_by_name("hours_e")) select.select_by_visible_text(num) num = self.GetRandomNumber(0, 59) num = str(num) WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_name("minutes_e")) select = Select(self.driver.find_element_by_name("minutes_e")) select.select_by_visible_text(num) #Config offset settings elem = self.driver.find_element_by_xpath( "/html/body/form/table[3]/tbody/tr[18]/td[2]/input") elem.clear() self.driver.find_element_by_xpath( "/html/body/form/table[3]/tbody/tr[18]/td[2]/input").send_keys("1") self.driver.find_element_by_xpath("/html/body/form/p/input").click() def engineConfigInfo(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/a").click( ) # locate Configuration time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/a").click( ) #locate System time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/div/ul/li[4]/a" ).click() time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) c1 = self.driver.find_element_by_name("ntp_server1").get_attribute( "value") print "the server1 is %s\n" % c1 c2 = self.driver.find_element_by_name("ntp_server2").get_attribute( "value") c3 = self.driver.find_element_by_name("ntp_server3").get_attribute( "value") c4 = self.driver.find_element_by_name("ntp_server4").get_attribute( "value") c5 = self.driver.find_element_by_name("ntp_server5").get_attribute( "value") c = [c1, c2, c3, c4, c5] i = 0 while i < len(c): t = i + 1 if s[i] == c[i]: print "server %d configuration is correct" % t else: print "the Configuration changed after the dut Rboot" time.sleep(1) i = i + 1 print "check all configuration" def systime(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/a").click( ) time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/div/ul/li/a" ).click() # locate information time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(300) self.driver.find_element_by_xpath( "/html/body/div/form/input[2]").click() time.sleep(15) sysdate = self.driver.find_element_by_id("sys_date").text print "the date is %s" % sysdate systime = self.driver.find_element_by_id("sys_uptime").text print "the time is %s" % systime
class ConfigCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self,brserType,DUTIP): self.pubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.pubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.pubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def startWebConfig(self,prjName): print "\n*******************StartWebMac*******************" domain = "http://admin:@%s"%(self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "Start web successfully!" def clickBackupButton(self): print "\nBackup in Processing...\n" if self.browserType == "firefox": self.driver.find_element_by_xpath("/html/body/form/input").submit() if self.browserType == "chrome": self.driver.find_element_by_xpath("/html/body/form/input").click() def engineConfigBackup(self): print "\n************EngineConfigurationBackup************\n" self.pubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[4]/a").click() #find "Maintenance" time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[4]/div/ul/li[4]/a").click() #find "Configuration" time.sleep(2) #self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[4]/div/ul/li[4]/div/ul/li/a").click() #find "Backup Binary" #time.sleep(2) #self.pubModuleEle.location("/html/frameset/frameset/frame[2]") #time.sleep(1) #self.clickBackupButton() print "Technical Difficulties: can't close the popup windows...\n" time.sleep(5) print "***********ConfigurationBackupFinish*************" def engineConfigRestore(self): print "\n************EngineConfigurationRestore***********" #self.pubModuleEle.location("/html/frameset/frameset/frame[1]") #time.sleep(1) #self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[4]/a").click() #find "Maintenance" #time.sleep(1) #self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[4]/div/ul/li[4]/a").click() #find "Configuration" #time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[4]/div/ul/li[4]/div/ul/li[2]/a").click() #find "Restore Binary" time.sleep(1) self.clickRestoreButton() time.sleep(1) print "***********ConfigurationRestoreFinish************\n" def clickRestoreButton(self): print "\nRestore in Processing..." self.pubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(1) #self.driver.find_element_by_xpath("/html/body/form/input").send_keys("D:\FirefoxDownload\S3280_v1.6.0-May21-bugfix_conf.bin") # for windows OS self.driver.find_element_by_xpath("/html/body/form/input").send_keys("/home/olivia/Downloads/S3280_v1.5.4_conf.bin") #(for linux OS) time.sleep(1) self.pubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(1) self.driver.find_element_by_xpath("/html/body/form/input[2]").click() print "\nPlease wait for 90 seconds ...\n" time.sleep(90)
class WebMacCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self,brserType,DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200: print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebMac(self,prjName): print "\n========startWebMac========" domain = "http://admin:@%s"%(self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" def ClickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath("//input[@value='Save']").send_keys(Keys.RETURN) if self.browserType == "chrome": self.driver.find_element_by_xpath("//input[@value='Save']").click() def EngineMac(self): print "\n=========engineMac=========" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(3) ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("mac.htm")).click() def SetMacLearning(self): print "\n======setMacLearning=====" time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") global randomList randomList = [] for i in range(0,8): randomList.append(random.randint(1,3)) for j in range(0,8): autoId = "Learn_Auto_%d"%(j+1) disableId = "Learn_Disable_%d"%(j+1) secureId = "Learn_Secure_%d"%(j+1) if randomList[j] == 1: ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(autoId)).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(autoId)).click() if randomList[j] == 2: ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(disableId)).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(disableId)).click() if randomList[j] == 3: ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(secureId)).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(secureId)).click() time.sleep(3) self.ClickSaveButton() print "SetMacLearning finished" def CheckMacLearning(self): print "\n======CheckMacLearning=====" self.PubModuleEle.DutReboot() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("mac.htm")).click() time.sleep(5) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(5) PortList = [] for i in range(0,8): for j in range(1,4): Portxpath = "/html/body/form/table[2]/tbody/tr[%d]/td[%d]/input"%((2+j),(2+i)) Portstatus = str(self.driver.find_element_by_xpath(Portxpath).get_attribute("checked")) if Portstatus == "true": PortList.append(j) break for i in range(0,8): if PortList[i] != randomList[i]: print i raise print "CheckMacLearning finished" def FactoryDefault(self): print "\n=====FactoryDefault=====" self.PubModuleEle.DutReboot(mode='fd') print "FactoryDefault finished" def AgingConfig(self, disable = "0", Agingtime = "300"): global agingtime global disable_value print "\n=====AgingConfig=====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("mac.htm")).click() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(3) disable_value=str(self.driver.find_element_by_id("DisableAgeing").get_attribute("checked")) agingtime = string.atoi(self.driver.find_element_by_id("agebox").get_attribute("value")) time.sleep(3) if disable_value == "true": self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("DisableAgeing")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("DisableAgeing")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("agebox")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("agebox")).clear() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("agebox")).send_keys("20") ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("/html/body/form/p[2]/input[2]")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_xpath("/html/body/form/p[2]/input[2]")).click() time.sleep(3) if disable == "0": agingtime = string.atoi(Agingtime) disable_value = "None" if 10 <= agingtime <= 1000000: print "\ndisable = " + disable + " Agingtime = " + Agingtime self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("agebox")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("agebox")).clear() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("agebox")).send_keys(Agingtime) ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("/html/body/form/p[2]/input[2]")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_xpath("/html/body/form/p[2]/input[2]")).click() time.sleep(3) else: print "out of range" if disable == "1": disable_value = "true" print "\ndisable = " + disable self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("DisableAgeing")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("DisableAgeing")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("/html/body/form/p[2]/input[2]")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_xpath("/html/body/form/p[2]/input[2]")).click() time.sleep(3) print "AgingConfig finished" time.sleep(3) def CheckMAC_Port(self,dst): global check_web_port_xpath global agingtime global disable_value print "\n=========CheckMAC_Port========" #----------------send an ARP packet----------------------- random_src = "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x" % (0, 0, 0, random.uniform(0,15), random.uniform(0,15), random.uniform(0,15),\ random.uniform(0,15), random.uniform(0,15), random.uniform(0,15), random.uniform(0,15), random.uniform(0,15),\ random.uniform(0,15)) eth = Ether(src = random_src,type=0x0806) #arp = ARP(hwtype = 0x0001,ptype = 0x0800,op = 0x0001,hwsrc = random_src,psrc = '192.168.1.32',pdst = dst) arp = ARP(hwtype = 0x0001,ptype = 0x0800,op = 0x0001,hwsrc = random_src,pdst = dst) a = eth/arp success = 1 success = sendp(a,iface="eth1") if success == None: print "send an ARP Packet successfully" else: print "fail in sending an ARP Packet" raise #----------------check mac---------------------------------- self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(3) #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Monitor")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Monitor")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("dyna_mac.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("dyna_mac.htm")).click() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(3) time.sleep(agingtime) ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("/html/body/div/form/input[2]")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_xpath("/html/body/div/form/input[2]")).click() time.sleep(3) i = 3 check_web_mac_xpath = "/html/body/table/tbody/tr[3]/td[3]" check_web_port_xpath = "/html/body/table/tbody/tr[3]/td[5]/img" MACaddress = self.driver.find_element_by_xpath(check_web_mac_xpath).text mac_flag = 0 while MACaddress: if MACaddress.lower() == random_src[0:2] + "-" + random_src[3:5] + "-" + random_src[6:8] + "-" + random_src[9:11] + "-" +\ random_src[12:14] + "-" + random_src[15:17]: print "get the right MACaddress" mac_flag = 1 break else: mac_flag = 0 i +=1 check_web_mac_xpath="/html/body/table/tbody/tr[%d]/td[3]"%i check_web_port_xpath="/html/body/table/tbody/tr[%d]/td[5]/img"%i try: MACaddress = self.driver.find_element_by_xpath(check_web_mac_xpath).text except: print "searching all the table" break if disable_value == "None": if mac_flag: print "Shouldn't get the right MACaddress" raise else : print "Haven't gottn the right MACaddress" else : if mac_flag: print "Have gottn the right MACaddress" else : print "Should get the right MACaddress" raise print "finished checking MAC" #----------------check the right port---------------------------------- if mac_flag: try : self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.driver.find_element_by_xpath(check_web_port_xpath) print "get the right port" port_flag = 1; except : print "can't get the right port" port_flag = 0 if not port_flag: print "Haven't gotten the right port" raise else : print "Have gotten the right port" print "finished checking Port" print "----CheckMAC_Port finished---" time.sleep(5) def Check_MAC_Config(self): print "\n=========Check_MAC_Port_Config========" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("mac.htm")).click() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") check_disable_value = str(self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr/td[2]/input").get_attribute("checked")) self.PubModuleEle.DutReboot() time.sleep(5) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("mac.htm")).click() time.sleep(5) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(5) check_disable_value = str(self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr/td[2]/input").get_attribute("checked")) if(check_disable_value != disable_value): raise else: print "right disable_value" if(check_disable_value == "None"): check_agingtime = str(self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td[2]/input").get_attribute("value")) if check_agingtime != str(agingtime): raise else: print "right agingtime" print "----Check_MAC_Config finished---" time.sleep(3) def Check_MacTableConfig(self): print "\n=========Check_MacConfig========" global PortMem global MacID time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("mac.htm")).click() time.sleep(5) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(5) Check_WebVlan = [] Check_WebMac = [] for i in range(64): Check_WebVlanxpath="/html/body/form/table[3]/tbody/tr[%d]/td[2]"%(i+3) Check_WebMacxpath="/html/body/form/table[3]/tbody/tr[%d]/td[3]"%(i+3) Check_WebPortID = "Dest_%d_"%(i+1) Check_WebPort = [] for j in range (1,9): select_Check_WebPortID = Check_WebPortID + str(j) ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(select_Check_WebPortID)).perform() status = WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id\ (select_Check_WebPortID)).get_attribute("checked") Check_WebPort.append(str(status)) if Check_WebPort[j-1] == "true": Check_WebPort[j-1] = 1 else: Check_WebPort[j-1] = 0 if PortMem[i][j-1] != Check_WebPort[j-1]: raise time.sleep(3) Check_WebMac.append(str(self.driver.find_element_by_xpath(Check_WebMacxpath).text)) if MacID[i] != Check_WebMac[i]: raise Check_WebVlan.append(str(self.driver.find_element_by_xpath(Check_WebVlanxpath).text)) if Check_WebVlan[i] != "1": raise time.sleep(3) print "----Check_MacTableConfig finished---" time.sleep(3) def AddMacTable(self, process): print "\n=========AddMacTable========" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") global selectVID global MacID global PortMem PortMem = [] selectVID = range(64) if process == 1: VlanID = range(1,4094) selectVID = random.sample(VlanID,64) elif process == 2: for i in range(64): selectVID[i] = 1 MacID = range(100,164) for i in range(64): MacID[i] = "00-00-00-00-00-%X"%MacID[i] for i in range(0,64): WebVlanID = "VID_%d"%(i+1) WebMacID = "MAC_%d"%(i+1) WebPortID = "Dest_%d_"%(i+1) PortMem.append([0,0,0,0,0,0,0,0]) PortNum = random.randint(1,255) k = 0 while PortNum: PortNum,rem = divmod(PortNum,2) PortMem[i][k] = rem k = k+1 time.sleep(3) self.driver.find_element_by_xpath("/html/body/form/p/input").click() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(WebVlanID)).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(WebVlanID)).clear() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(WebVlanID)).send_keys(str(selectVID[i])) ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(WebMacID)).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(WebMacID)).clear() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(WebMacID)).send_keys(str(MacID[i])) for j in range (1,9): selectWebPortID = WebPortID + str(j) ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(selectWebPortID)).perform() if (PortMem[i][j-1]): WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(selectWebPortID)).click() time.sleep(3) self.driver.find_element_by_xpath("/html/body/form/p[2]/input[2]").click() time.sleep(10) print "----AddMacTable finished---" time.sleep(10) def DeleteMacTable(self): print "\n=========DeleteMacTable========" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") for i in range(0,64): DeleteID = "Delete_%d"%(i+1) self.driver.find_element_by_id(DeleteID).click() self.driver.find_element_by_xpath("/html/body/form/p[2]/input[2]").click() time.sleep(3) print "----DeleteMacTable finished---" time.sleep(5) def SendMACs(self,dst): print"\n=========SendMACs========" global Send_MAC src_mac = [] Send_MAC = [] for i in xrange(64): a = i + 1 j = '%s' % ('%x' % a) if len(j) == 1: x = "0" + j y = "00" elif len(j) == 2: x = j y = "00" elif len(j) == 3: x = j[1:] y = "0" + j[0] else: x = j[2:] y = j[:2] src_mac.append("00:01:00:00:" + y + ":" + x) Send_MAC.append("00-01-00-00-" + y + "-" + x) eth = Ether(src = src_mac[i],type=0x0806) #arp = ARP(hwtype = 0x0001,ptype = 0x0800,op = 0x0001,hwsrc = src_mac[i],psrc = '192.168.1.32',pdst = dst) arp = ARP(hwtype = 0x0001,ptype = 0x0800,op = 0x0001,hwsrc = src_mac[i],pdst = dst) a = eth/arp sendp(a,iface='eth1') time.sleep(0.1) print "----SendMACs finished---" time.sleep(3) def Check_MACs(self): print"\n=========Check_Macs========" global Send_MAC self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("dyna_mac.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("dyna_mac.htm")).click() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(3) ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("StartMacAddr")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("StartMacAddr")).clear() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("StartMacAddr")).send_keys("00-01-00-00-00-01") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("NumberOfEntries")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("NumberOfEntries")).clear() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("NumberOfEntries")).send_keys("100") self.driver.find_element_by_xpath("/html/body/div/form/input[2]").click() time.sleep(5) for i in xrange(64): for j in xrange(100): Check_Mac_xpath = "/html/body/table/tbody/tr[%d]/td[3]"%(j+3) Check_Mac = self.driver.find_element_by_xpath(Check_Mac_xpath).text if Send_MAC[i].upper() == Check_Mac: Check_Type_xpath = "/html/body/table/tbody/tr[%d]/td"%(j+3) Check_VlanID_xpath = "/html/body/table/tbody/tr[%d]/td[2]"%(j+3) Check_Port_xpath = "/html/body/table/tbody/tr[%d]/td[5]/img"%(j+3) Check_Type = self.driver.find_element_by_xpath(Check_Type_xpath).text Check_VlanID = self.driver.find_element_by_xpath(Check_VlanID_xpath).text break elif j == 100: raise if Check_Type == "Dynamic" and Check_VlanID == "1": self.driver.find_element_by_xpath(Check_Port_xpath) else: raise print "----Check_MACs finished---" time.sleep(3) def DutReboot(self,**args): self.PubModuleEle.rthttpmode="http" ##self.PubModuleEle.location("/html/frameset/frameset/frame[1]") #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Maintenance")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Maintenance")).click() ##ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("wreset.htm")).perform() ##WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("wreset.htm")).click() print "DUT will reboot soon ..." time.sleep(3) ##self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ##time.sleep(5) ##self.driver.find_element_by_xpath("//input[@value='Yes']").click() ##time.sleep(5) self.PubModuleEle.i = 1 time.sleep(120) ###wait for reboot accomplishment while True: self.st = self.PubModuleEle.ConnectionServer(httpmode=self.PubModuleEle.rthttpmode) if self.st != 200: print "Waiting for DUT bootup ...%d"%self.PubModuleEle.i else: time.sleep(5) print "DUT bootup successfully!" time.sleep(10) return 1 break self.PubModuleEle.i += 1 if (self.PubModuleEle.i == 12): print "DUT do not bootup again\n DUT bootup FAIL!" time.sleep(10) return 0 else: time.sleep(5)
class SoftUploadCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self, brserType, DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d." % st if st != 200: print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebSoftUpload(self, prjName): print "========startWebMac========" domain = "http://admin:@%s" % (self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" def ClickUploadButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath( "//input[@value='Upload']").submit() if self.browserType == "chrome": self.driver.find_element_by_xpath( "//input[@value='Upload']").click() def EngineSoftUpload(self): print "=======engineSoftUpload====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(1) ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Maintenance")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Maintenance")).click() time.sleep(1) ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Software")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver. find_element_by_link_text("Software")).click() time.sleep(1) ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("upload.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("upload.htm")).click() def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to_alert() if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert.text finally: self.accept_next_alert = True def SetSoftUpload(self, prjName): print "======setSoftUpload========" time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("activate_now")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("activate_now")).click() self.driver.find_element_by_name("firmware").send_keys( "./project/prjName/firmware/S3280-v1.5.3.dat") self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(1) self.ClickUploadButton() time.sleep(1) self.driver.switch_to_alert().accept() def SearchSoftUpload(self): print "=======searchSoftUpload====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(1) ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("upload.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("upload.htm")).click() def SoftwareUpload(self, prjName): print "=======softwareUpload======" self.SetSoftUpload(prjName) time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") m = 1 global progress progress = "Initial!" while True: self.PubModuleEle.location("/html/frameset/frameset/frame[2]") progress = self.driver.execute_script( 'return document.getElementById("ticker");') print "Loop %d." % m judge = not progress if judge != True: break m = m + 1 if m > 3: break self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.SearchSoftUpload() self.SetSoftUpload(prjName) time.sleep(2) return m
class WebMirrorCase: def __init__(self, brserType="firefox", DUTIP="192.168.3.74"): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d." % st if st != 200: print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle def StartWebMirror(self, prjName): print "========startWebMirror========" domain = "http://admin:@%s" % (self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Configuration")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("mirror.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("mirror.htm")).click() def into_mirror_web(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("mirror.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("mirror.htm")).click() def ConfigMirror(self): self.PubModuleEle.location("/html/frameset/frameset/frame[2]") select1 = Select(self.driver.find_element_by_id("portselect")) index1 = random.randint(0, 8) index1 = str(index1) select1.select_by_value(index1) mirror_conf = {'port_to_mirror': index1} mirror_port = { '0': 'Disabled', '1': 'Rx only', '2': 'Tx only', '3': 'Enabled' } for i in range(1, 9): i = str(i) select_path = "mode_%s" % i select2 = Select(self.driver.find_element_by_id(select_path)) if i == index1: index2 = random.randint(0, 1) else: index2 = random.randint(0, 3) index2 = str(index2) time.sleep(1) select2.select_by_value(index2) mirror_conf[i] = mirror_port[index2] self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() return mirror_conf def DeleteMirror(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Maintenance").click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text( "Factory Defaults")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Factory Defaults")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_name("factory")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_name("factory")).click() temp = 1 while True: try: elem = self.driver.find_element_by_xpath("/html/body/h1") if elem.text == "Configuration Factory Reset Done": print "=======Factory Default successfully======\n" break raise error except: time.sleep(3) if temp != 10: temp = temp + 1 else: print "there is an error while factory default" break def CheckMirrorConfig(self, conf): print "=====DUT will reboot soon=======" re = self.PubModuleEle.DutReboot() if re == 0: sys.exit() time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Configuration")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("mirror.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("mirror.htm")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") port_select = Select(self.driver.find_element_by_id("portselect")) if port_select.first_selected_option.text != conf["port_to_mirror"]: print "the Configuration changed after the dut reboot" sys.exit() else: for i in range(1, 9): i = str(i) select_path = "mode_%s" % i select = Select(self.driver.find_element_by_id(select_path)) if select.first_selected_option.text != conf[i]: print "the Configuration changed after the dut Rboot" sys.exit() print "===all Configuration is correct=====" def CloseSession(self): self.driver.close()
class WebMonitorDMIClassCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self, prjName, brserType, DUTIP): self.prjName = prjName.lower() self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d." % st if st != 200: print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebMonitorDMI(self, prjName): print "=====startWebMonitorDMI=====" domain = "http://admin:@%s" % (self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" def ClickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath( "//input[@value='Save']").send_keys(Keys.RETURN) if self.browserType == "chrome": self.driver.find_element_by_xpath("//input[@value='Save']").click() def EngineWebMonitorDMI(self): print "=====engineWebMonitorDMI====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(1) #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Monitor")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Monitor")).click() #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Ports")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id( " stat_dmi_detailed.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_id( " stat_dmi_detailed.htm")).click() def ReadMonitorDMI(self): print "========readMonitorDMI======" time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") if self.prjName == 's3280': index = 5 elif self.prjName == 's4140': index = 1 for i in range(1, 5): value = str(index) select = Select(self.driver.find_element_by_id("portselect")) select.select_by_value(value) time.sleep(1) threshlod = self.driver.find_element_by_xpath( "/html/body/table[2]/tbody/tr[4]/td[2]/label").text print "Rx Power Intrusion Threshold of port%s is %s" % (value, threshlod) index = index + 1 self.driver.close() def ConfigDMI(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Configuration").click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Ports")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("DMI")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("DMI")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") if self.prjName == 's3280': index = 5 elif self.prjName == 's4140': index = 1 self.config = [] for i in range(1, 5): time.sleep(1) ID = str("rpit_%d" % index) value = random.randint(0, 65535) value = str(value) self.config.append(value) self.driver.find_element_by_id(ID).clear() self.driver.find_element_by_id(ID).send_keys(value) index = index + 1 self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() time.sleep(1) def DelConf(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Maintenance").click() time.sleep(1) ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text( "Factory Defaults")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Factory Defaults")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_name("factory")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_name("factory")).click() temp = 1 while True: try: elem = self.driver.find_element_by_xpath("/html/body/h1") if elem.text == "Configuration Factory Reset Done": print "=======Factory Default successfully======\n" break raise error except: time.sleep(3) if temp != 10: temp = temp + 1 else: print "there is an error while factory default" break def Reboot(self): print "ready to reboot" time.sleep(5) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") try: re = self.PubModuleEle.DutReboot() except: print "there is an error while reboot dut" print "failed" sys.exit() if not re: print "DUT reboot failed,please check it" sys.exit() def CheckConfig(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Configuration").click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Ports")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("DMI")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("DMI")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") if self.prjName == 's3280': index = 5 elif self.prjName == 's4140': index = 1 for i in range(0, 4): ID = str("rpit_%d" % index) value = self.driver.find_element_by_id(ID).get_attribute('value') if self.config[i] != value: print "config changed after reboot,please check the dut" sys.exit() index = index + 1 print "configuration not changed " print "Pass"
class SshCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self,brserType,DUTIP): self.pubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.pubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.pubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def startWebConfig(self,prjName): print "\n*******************StartWebMac*******************" domain = "http://admin:@%s"%(self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "Start web successfully!" def enabledSSH(self): print "\n************EnabledConfigurationSSH**************\n" self.pubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Security")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Security")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Switch")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Switch")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("SSH")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("SSH")).click() self.pubModuleEle.location("/html/frameset/frameset/frame[2]") WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("ssh_mode")) select = Select(self.driver.find_element_by_name("ssh_mode")) select.select_by_value("1") self.clickSaveButton() print "\n*************SetSSHenabledFinish*****************\n" print "SSH try to connect to server...\n" def clickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath("/html/body/form/p/input").click() if self.browserType == "chrome": self.driver.find_element_by_xpath("/html/body/form/p/input").click() def disabledSSH(self): print "\n***********DisabledConfigurationSSH**************\n" self.pubModuleEle.location("/html/frameset/frameset/frame[2]") WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("ssh_mode")) select = Select(self.driver.find_element_by_name("ssh_mode")) select.select_by_value("0") self.clickSaveButton() print "\n**************SetSSHdisabledFinish***************\n" def checkSSHConfig(self): print "\n*************CheckSSHConfiguration***************" self.pubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Security")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Security")).click() #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Switch")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Switch")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("SSH")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("SSH")).click() self.pubModuleEle.location("/html/frameset/frameset/frame[2]") WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("ssh_mode")) getsel = Select(self.driver.find_element_by_name("ssh_mode")) getval = getsel.first_selected_option.text print "\nCurrent SSH is %s" %getval if getval == "Disabled": print "SSH configuration has no change after reboot...\n" else: getval == "Enabled" print "SSH configuration changed after reboot...\n" print "*************CheckConfigurationFinish************\n"
class WebMacCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self, brserType, DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d." % st if st != 200: print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebMac(self, prjName): print "\n========startWebMac========" domain = "http://admin:@%s" % (self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" def ClickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath( "//input[@value='Save']").send_keys(Keys.RETURN) if self.browserType == "chrome": self.driver.find_element_by_xpath("//input[@value='Save']").click() def EngineMac(self): print "\n=========engineMac=========" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(3) ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Configuration")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("mac.htm")).click() def SetMacLearning(self): print "\n======setMacLearning=====" time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") global randomList randomList = [] for i in range(0, 8): randomList.append(random.randint(1, 3)) for j in range(0, 8): autoId = "Learn_Auto_%d" % (j + 1) disableId = "Learn_Disable_%d" % (j + 1) secureId = "Learn_Secure_%d" % (j + 1) if randomList[j] == 1: ActionChains(self.driver).move_to_element( self.driver.find_element_by_id(autoId)).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id(autoId)).click() if randomList[j] == 2: ActionChains(self.driver).move_to_element( self.driver.find_element_by_id(disableId)).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver. find_element_by_id(disableId)).click() if randomList[j] == 3: ActionChains(self.driver).move_to_element( self.driver.find_element_by_id(secureId)).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver. find_element_by_id(secureId)).click() time.sleep(3) self.ClickSaveButton() print "SetMacLearning finished" def CheckMacLearning(self): print "\n======CheckMacLearning=====" self.PubModuleEle.DutReboot() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Configuration")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("mac.htm")).click() time.sleep(5) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(5) PortList = [] for i in range(0, 8): for j in range(1, 4): Portxpath = "/html/body/form/table[2]/tbody/tr[%d]/td[%d]/input" % ( (2 + j), (2 + i)) Portstatus = str( self.driver.find_element_by_xpath(Portxpath).get_attribute( "checked")) if Portstatus == "true": PortList.append(j) break for i in range(0, 8): if PortList[i] != randomList[i]: print i raise print "CheckMacLearning finished" def FactoryDefault(self): print "\n=====FactoryDefault=====" self.PubModuleEle.DutReboot(mode='fd') print "FactoryDefault finished" def AgingConfig(self, disable="0", Agingtime="300"): global agingtime global disable_value print "\n=====AgingConfig=====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("mac.htm")).click() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(3) disable_value = str( self.driver.find_element_by_id("DisableAgeing").get_attribute( "checked")) agingtime = string.atoi( self.driver.find_element_by_id("agebox").get_attribute("value")) time.sleep(3) if disable_value == "true": self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("DisableAgeing")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_id( "DisableAgeing")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("agebox")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("agebox")).clear() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_id( "agebox")).send_keys("20") ActionChains(self.driver).move_to_element( self.driver.find_element_by_xpath( "/html/body/form/p[2]/input[2]")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_xpath( "/html/body/form/p[2]/input[2]")).click() time.sleep(3) if disable == "0": agingtime = string.atoi(Agingtime) disable_value = "None" if 10 <= agingtime <= 1000000: print "\ndisable = " + disable + " Agingtime = " + Agingtime self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("agebox")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver. find_element_by_id("agebox")).clear() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_id( "agebox")).send_keys(Agingtime) ActionChains(self.driver).move_to_element( self.driver.find_element_by_xpath( "/html/body/form/p[2]/input[2]")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_xpath( "/html/body/form/p[2]/input[2]")).click() time.sleep(3) else: print "out of range" if disable == "1": disable_value = "true" print "\ndisable = " + disable self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("DisableAgeing")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_id( "DisableAgeing")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_xpath( "/html/body/form/p[2]/input[2]")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_xpath( "/html/body/form/p[2]/input[2]")).click() time.sleep(3) print "AgingConfig finished" time.sleep(3) def CheckMAC_Port(self, dst): global check_web_port_xpath global agingtime global disable_value print "\n=========CheckMAC_Port========" #----------------send an ARP packet----------------------- random_src = "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x" % (0, 0, 0, random.uniform(0,15), random.uniform(0,15), random.uniform(0,15),\ random.uniform(0,15), random.uniform(0,15), random.uniform(0,15), random.uniform(0,15), random.uniform(0,15),\ random.uniform(0,15)) eth = Ether(src=random_src, type=0x0806) #arp = ARP(hwtype = 0x0001,ptype = 0x0800,op = 0x0001,hwsrc = random_src,psrc = '192.168.1.32',pdst = dst) arp = ARP(hwtype=0x0001, ptype=0x0800, op=0x0001, hwsrc=random_src, pdst=dst) a = eth / arp success = 1 success = sendp(a, iface="eth1") if success == None: print "send an ARP Packet successfully" else: print "fail in sending an ARP Packet" raise #----------------check mac---------------------------------- self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(3) #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Monitor")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Monitor")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("dyna_mac.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("dyna_mac.htm")).click() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(3) time.sleep(agingtime) ActionChains(self.driver).move_to_element( self.driver.find_element_by_xpath( "/html/body/div/form/input[2]")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_xpath( "/html/body/div/form/input[2]")).click() time.sleep(3) i = 3 check_web_mac_xpath = "/html/body/table/tbody/tr[3]/td[3]" check_web_port_xpath = "/html/body/table/tbody/tr[3]/td[5]/img" MACaddress = self.driver.find_element_by_xpath( check_web_mac_xpath).text mac_flag = 0 while MACaddress: if MACaddress.lower() == random_src[0:2] + "-" + random_src[3:5] + "-" + random_src[6:8] + "-" + random_src[9:11] + "-" +\ random_src[12:14] + "-" + random_src[15:17]: print "get the right MACaddress" mac_flag = 1 break else: mac_flag = 0 i += 1 check_web_mac_xpath = "/html/body/table/tbody/tr[%d]/td[3]" % i check_web_port_xpath = "/html/body/table/tbody/tr[%d]/td[5]/img" % i try: MACaddress = self.driver.find_element_by_xpath( check_web_mac_xpath).text except: print "searching all the table" break if disable_value == "None": if mac_flag: print "Shouldn't get the right MACaddress" raise else: print "Haven't gottn the right MACaddress" else: if mac_flag: print "Have gottn the right MACaddress" else: print "Should get the right MACaddress" raise print "finished checking MAC" #----------------check the right port---------------------------------- if mac_flag: try: self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.driver.find_element_by_xpath(check_web_port_xpath) print "get the right port" port_flag = 1 except: print "can't get the right port" port_flag = 0 if not port_flag: print "Haven't gotten the right port" raise else: print "Have gotten the right port" print "finished checking Port" print "----CheckMAC_Port finished---" time.sleep(5) def Check_MAC_Config(self): print "\n=========Check_MAC_Port_Config========" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("mac.htm")).click() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") check_disable_value = str( self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr/td[2]/input").get_attribute( "checked")) self.PubModuleEle.DutReboot() time.sleep(5) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Configuration")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("mac.htm")).click() time.sleep(5) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(5) check_disable_value = str( self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr/td[2]/input").get_attribute( "checked")) if (check_disable_value != disable_value): raise else: print "right disable_value" if (check_disable_value == "None"): check_agingtime = str( self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td[2]/input"). get_attribute("value")) if check_agingtime != str(agingtime): raise else: print "right agingtime" print "----Check_MAC_Config finished---" time.sleep(3) def Check_MacTableConfig(self): print "\n=========Check_MacConfig========" global PortMem global MacID time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_link_text( "Configuration")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("mac.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("mac.htm")).click() time.sleep(5) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(5) Check_WebVlan = [] Check_WebMac = [] for i in range(64): Check_WebVlanxpath = "/html/body/form/table[3]/tbody/tr[%d]/td[2]" % ( i + 3) Check_WebMacxpath = "/html/body/form/table[3]/tbody/tr[%d]/td[3]" % ( i + 3) Check_WebPortID = "Dest_%d_" % (i + 1) Check_WebPort = [] for j in range(1, 9): select_Check_WebPortID = Check_WebPortID + str(j) ActionChains(self.driver).move_to_element( self.driver.find_element_by_id( select_Check_WebPortID)).perform() status = WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id\ (select_Check_WebPortID)).get_attribute("checked") Check_WebPort.append(str(status)) if Check_WebPort[j - 1] == "true": Check_WebPort[j - 1] = 1 else: Check_WebPort[j - 1] = 0 if PortMem[i][j - 1] != Check_WebPort[j - 1]: raise time.sleep(3) Check_WebMac.append( str(self.driver.find_element_by_xpath(Check_WebMacxpath).text)) if MacID[i] != Check_WebMac[i]: raise Check_WebVlan.append( str( self.driver.find_element_by_xpath( Check_WebVlanxpath).text)) if Check_WebVlan[i] != "1": raise time.sleep(3) print "----Check_MacTableConfig finished---" time.sleep(3) def AddMacTable(self, process): print "\n=========AddMacTable========" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") global selectVID global MacID global PortMem PortMem = [] selectVID = range(64) if process == 1: VlanID = range(1, 4094) selectVID = random.sample(VlanID, 64) elif process == 2: for i in range(64): selectVID[i] = 1 MacID = range(100, 164) for i in range(64): MacID[i] = "00-00-00-00-00-%X" % MacID[i] for i in range(0, 64): WebVlanID = "VID_%d" % (i + 1) WebMacID = "MAC_%d" % (i + 1) WebPortID = "Dest_%d_" % (i + 1) PortMem.append([0, 0, 0, 0, 0, 0, 0, 0]) PortNum = random.randint(1, 255) k = 0 while PortNum: PortNum, rem = divmod(PortNum, 2) PortMem[i][k] = rem k = k + 1 time.sleep(3) self.driver.find_element_by_xpath( "/html/body/form/p/input").click() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_id(WebVlanID)).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id(WebVlanID)).clear() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id(WebVlanID)).send_keys( str(selectVID[i])) ActionChains(self.driver).move_to_element( self.driver.find_element_by_id(WebMacID)).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id(WebMacID)).clear() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id(WebMacID)).send_keys( str(MacID[i])) for j in range(1, 9): selectWebPortID = WebPortID + str(j) ActionChains(self.driver).move_to_element( self.driver.find_element_by_id(selectWebPortID)).perform() if (PortMem[i][j - 1]): WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_id( selectWebPortID)).click() time.sleep(3) self.driver.find_element_by_xpath( "/html/body/form/p[2]/input[2]").click() time.sleep(10) print "----AddMacTable finished---" time.sleep(10) def DeleteMacTable(self): print "\n=========DeleteMacTable========" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") for i in range(0, 64): DeleteID = "Delete_%d" % (i + 1) self.driver.find_element_by_id(DeleteID).click() self.driver.find_element_by_xpath( "/html/body/form/p[2]/input[2]").click() time.sleep(3) print "----DeleteMacTable finished---" time.sleep(5) def SendMACs(self, dst): print "\n=========SendMACs========" global Send_MAC src_mac = [] Send_MAC = [] for i in xrange(64): a = i + 1 j = '%s' % ('%x' % a) if len(j) == 1: x = "0" + j y = "00" elif len(j) == 2: x = j y = "00" elif len(j) == 3: x = j[1:] y = "0" + j[0] else: x = j[2:] y = j[:2] src_mac.append("00:01:00:00:" + y + ":" + x) Send_MAC.append("00-01-00-00-" + y + "-" + x) eth = Ether(src=src_mac[i], type=0x0806) #arp = ARP(hwtype = 0x0001,ptype = 0x0800,op = 0x0001,hwsrc = src_mac[i],psrc = '192.168.1.32',pdst = dst) arp = ARP(hwtype=0x0001, ptype=0x0800, op=0x0001, hwsrc=src_mac[i], pdst=dst) a = eth / arp sendp(a, iface='eth1') time.sleep(0.1) print "----SendMACs finished---" time.sleep(3) def Check_MACs(self): print "\n=========Check_Macs========" global Send_MAC self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("dyna_mac.htm")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("dyna_mac.htm")).click() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(3) ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("StartMacAddr")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_id("StartMacAddr")).clear() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_id( "StartMacAddr")).send_keys("00-01-00-00-00-01") ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("NumberOfEntries")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_id( "NumberOfEntries")).clear() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_id( "NumberOfEntries")).send_keys("100") self.driver.find_element_by_xpath( "/html/body/div/form/input[2]").click() time.sleep(5) for i in xrange(64): for j in xrange(100): Check_Mac_xpath = "/html/body/table/tbody/tr[%d]/td[3]" % (j + 3) Check_Mac = self.driver.find_element_by_xpath( Check_Mac_xpath).text if Send_MAC[i].upper() == Check_Mac: Check_Type_xpath = "/html/body/table/tbody/tr[%d]/td" % ( j + 3) Check_VlanID_xpath = "/html/body/table/tbody/tr[%d]/td[2]" % ( j + 3) Check_Port_xpath = "/html/body/table/tbody/tr[%d]/td[5]/img" % ( j + 3) Check_Type = self.driver.find_element_by_xpath( Check_Type_xpath).text Check_VlanID = self.driver.find_element_by_xpath( Check_VlanID_xpath).text break elif j == 100: raise if Check_Type == "Dynamic" and Check_VlanID == "1": self.driver.find_element_by_xpath(Check_Port_xpath) else: raise print "----Check_MACs finished---" time.sleep(3) def DutReboot(self, **args): self.PubModuleEle.rthttpmode = "http" ##self.PubModuleEle.location("/html/frameset/frameset/frame[1]") #ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Maintenance")).perform() #WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Maintenance")).click() ##ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("wreset.htm")).perform() ##WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("wreset.htm")).click() print "DUT will reboot soon ..." time.sleep(3) ##self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ##time.sleep(5) ##self.driver.find_element_by_xpath("//input[@value='Yes']").click() ##time.sleep(5) self.PubModuleEle.i = 1 time.sleep(120) ###wait for reboot accomplishment while True: self.st = self.PubModuleEle.ConnectionServer( httpmode=self.PubModuleEle.rthttpmode) if self.st != 200: print "Waiting for DUT bootup ...%d" % self.PubModuleEle.i else: time.sleep(5) print "DUT bootup successfully!" time.sleep(10) return 1 break self.PubModuleEle.i += 1 if (self.PubModuleEle.i == 12): print "DUT do not bootup again\n DUT bootup FAIL!" time.sleep(10) return 0 else: time.sleep(5)
class ConfigCase: #Initialize module, will include "caseName","ProjectName","browser type","ip address","driver object" def __init__(self,brserType,DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebConfig(self,prjName): print "=======startWebUser=======" domain = "http://admin:@%s"%(self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" def ClickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath("//input[@value='Save']").send_keys(Keys.RETURN) if self.browserType == "chrome": self.driver.find_element_by_xpath("//input[@value='Save']").click() def engineConfigNtp(self,ntpsrv1,ntpsrv2,ntpsrv3,ntpsrv4,ntpsrv5): print "========engineConfig========" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/a").click()# locate Configuration time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/a").click()#locate System time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/div/ul/li[4]/a").click() time.sleep(2) print "***********ConfigurationFinish*************" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("ntp_mode")) select = Select(self.driver.find_element_by_name("ntp_mode")) select.select_by_value("1") print "***************NTPConfiguration***************************" elem=self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[3]/td[2]/input") elem.clear() self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[3]/td[2]/input").send_keys(ntpsrv1) s1=self.driver.find_element_by_name("ntp_server1").get_attribute("value") print "the server1 is %s"%s1 elem=self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[4]/td[2]/input") elem.clear() self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[4]/td[2]/input").send_keys(ntpsrv2) s2=self.driver.find_element_by_name("ntp_server2").get_attribute("value") elem=self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[5]/td[2]/input") elem.clear() self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[5]/td[2]/input").send_keys(ntpsrv3) s3=self.driver.find_element_by_name("ntp_server3").get_attribute("value") elem=self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[6]/td[2]/input") elem.clear() self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[6]/td[2]/input").send_keys(ntpsrv4) s4=self.driver.find_element_by_name("ntp_server4").get_attribute("value") elem=self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[7]/td[2]/input") elem.clear() self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[7]/td[2]/input").send_keys(ntpsrv5) s5=self.driver.find_element_by_name("ntp_server5").get_attribute("value") self.driver.find_element_by_xpath( "/html/body/form/p/input").click() global s s=[s1,s2,s3,s4 ,s5] def GetRandomNumber(self,m,n): return random.randint(m,n) def GetRandomString(self,list): return random.choice(list) def engineConfigTime(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/div/ul/li[5]/a").click()# locate Time time.sleep(2) def engineRange(self): for i in range(1,4): self.engineConfigZone() self.engineConfigStart() def engineConfigZone(self): self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) timezone=self.GetRandomString(['-3600','1','4804','3000','4200','6000','7200']) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("time_zone")) select = Select(self.driver.find_element_by_name("time_zone")) select.select_by_value(timezone) getval = select.first_selected_option.text print "the timezone is %s"%getval elem=self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[3]/td[2]/input") elem.clear() self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[3]/td[2]/input").send_keys("Cas") WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("mode")) select = Select(self.driver.find_element_by_name("mode")) select.select_by_value("1") time.sleep(2) def engineConfigStart(self): self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) num = self.GetRandomNumber(1,5) num=str(num) print "The start week random number is %s\n"%num WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("week_s")) select = Select(self.driver.find_element_by_name("week_s")) select.select_by_visible_text(num) string=self.GetRandomString(['Sun','Mon','Tue','Wed','Thu','Fri','Sat']) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("day_s")) select = Select(self.driver.find_element_by_name("day_s")) select.select_by_visible_text(string) string=self.GetRandomString(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("month_s")) select = Select(self.driver.find_element_by_name("month_s")) select.select_by_visible_text(string) num=self.GetRandomNumber(0,23) num=str(num) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("hours_s")) select = Select(self.driver.find_element_by_name("hours_s")) select.select_by_visible_text(num) num=self.GetRandomNumber(0,59) num=str(num) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("minutes_s")) select = Select(self.driver.find_element_by_name("minutes_s")) select.select_by_visible_text(num) # config End time settings num=self.GetRandomNumber(1,5) num=str(num) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("week_e")) select = Select(self.driver.find_element_by_name("week_e")) select.select_by_visible_text(num) print "the end week random number is %s\n"%num string=self.GetRandomString(['Sun','Mon','Tue','Wed','Thu','Fri','Sat']) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("day_e")) select = Select(self.driver.find_element_by_name("day_e")) select.select_by_visible_text(string) string=self.GetRandomString(['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("month_e")) select = Select(self.driver.find_element_by_name("month_e")) select.select_by_visible_text(string) num=self.GetRandomNumber(0,23) num=str(num) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("hours_e")) select = Select(self.driver.find_element_by_name("hours_e")) select.select_by_visible_text(num) num=self.GetRandomNumber(0,59) num=str(num) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_name("minutes_e")) select = Select(self.driver.find_element_by_name("minutes_e")) select.select_by_visible_text(num) #Config offset settings elem=self.driver.find_element_by_xpath("/html/body/form/table[3]/tbody/tr[18]/td[2]/input") elem.clear() self.driver.find_element_by_xpath("/html/body/form/table[3]/tbody/tr[18]/td[2]/input").send_keys("1") self.driver.find_element_by_xpath( "/html/body/form/p/input").click() def engineConfigInfo(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/a").click()# locate Configuration time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/a").click()#locate System time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/div/ul/li[4]/a").click() time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) c1=self.driver.find_element_by_name("ntp_server1").get_attribute("value") print"the server1 is %s\n"%c1 c2=self.driver.find_element_by_name("ntp_server2").get_attribute("value") c3=self.driver.find_element_by_name("ntp_server3").get_attribute("value") c4=self.driver.find_element_by_name("ntp_server4").get_attribute("value") c5=self.driver.find_element_by_name("ntp_server5").get_attribute("value") c=[c1,c2,c3,c4,c5] i=0 while i<len(c): t=i+1 if s[i]==c[i]: print "server %d configuration is correct" %t else : print "the Configuration changed after the dut Rboot" time.sleep(1) i=i+1 print "check all configuration" def systime(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/a").click() time.sleep(2) self.driver.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/div/ul/li/a").click()# locate information time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(300) self.driver.find_element_by_xpath( "/html/body/div/form/input[2]").click() time.sleep(15) sysdate=self.driver.find_element_by_id("sys_date").text print "the date is %s"%sysdate systime=self.driver.find_element_by_id("sys_uptime").text print "the time is %s"%systime
class ConfigCase: #Initialize module, will include "caseName","ProjectName","browser type","ip address","driver object" def __init__(self, brserType, DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIP = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d." % st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebConfig(self, prjName): print "\n=======startWebUser=======" domain = "http://admin:@%s" % (self.DUTIP) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" def ClickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath( "//input[@value='Save']").send_keys(Keys.RETURN) if self.browserType == "chrome": self.driver.find_element_by_xpath("//input[@value='Save']").click() def engineConfigLog(self, srvaddr): print "\n========engineConfig========" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/a").click( ) # locate Configuration time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/a").click( ) #locate System time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li/div/ul/li/div/ul/li[6]/a" ).click() # locate log time.sleep(2) print "***********ConfigurationFinish*************" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_name("server_mode")) select = Select(self.driver.find_element_by_name("server_mode")) select.select_by_value("1") elem = self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td[2]/input") elem.clear() self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td[2]/input").send_keys(srvaddr) s1 = self.driver.find_element_by_name("server_addr").get_attribute( "value") print "the serveraddr is %s" % s1 WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_name("syslog_level")) select = Select(self.driver.find_element_by_name("syslog_level")) select.select_by_value("0") self.driver.find_element_by_xpath("/html/body/form/p/input").click() def CheckConfig(self, pkg): print "\n====CheckConfig====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/a").click( ) #locate monitor system time.sleep(5) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/div/ul/li[3]/a" ).click() #locate log self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(8) i = 0 while 1: i = i + 1 if i == 1: Levelxpath = "/html/body/table[2]/tbody/tr/td/a" Timexpath = "/html/body/table[2]/tbody/tr/td[3]" Messagexpath = "/html/body/table[2]/tbody/tr/td[4]" else: Levelxpath = "/html/body/table[2]/tbody/tr[%d]/td/a" % i Timexpath = "/html/body/table[2]/tbody/tr[%d]/td[3]" % i Messagexpath = "/html/body/table[2]/tbody/tr[%d]/td[4]" % i try: Levelvalue = self.driver.find_element_by_xpath(Levelxpath).text Timevalue = self.driver.find_element_by_xpath(Timexpath).text Messagevalue = self.driver.find_element_by_xpath( Messagexpath).text Levelvalue = "ID" + Levelvalue except: print "finish read the Syslog table" break if pkg[i - 1].find(Levelvalue) != -1 and pkg[i - 1].find( Timevalue) != -1 and pkg[i - 1].find(Messagevalue) != -1: pass else: print "can't find the sniffered packet in syslog table" raise print "CheckConfig successfully\n" #----------------------------ManualSyslog--------------------------------------- def SendSyslog(self, choice): print "\n====SendSyslog====" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) ActionChains(self.driver).move_to_element( self.driver.find_element_by_link_text("Ports")).perform() WebDriverWait(self.driver, 30).until( lambda driver: driver.find_element_by_link_text("Ports")).click() ActionChains(self.driver).move_to_element( self.driver.find_element_by_id("ports.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element_by_id( "ports.htm")).click() #ports configuration time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") WebDriverWait( self.driver, 30).until(lambda driver: driver.find_element_by_id("speed_4")) if choice == 1: Select(self.driver.find_element_by_id("speed_4")).select_by_value( "0A0A0A0A0") elif choice == 2: Select(self.driver.find_element_by_id("speed_4")).select_by_value( "1A1A0A0A0") time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.driver.find_element_by_xpath( "/html/body/form/p/input[2]").click() #click save time.sleep(10) self.driver.find_element_by_xpath( "/html/body/div/form/input").click() # refresh print "send manul Info" def SyslogTest(self, choice): print "\n====SyslogTest====" t1 = threading.Thread(target=self.CapSyslog, args=(choice, )) t1.start() t2 = threading.Thread(target=self.SendSyslog, args=(choice, )) t2.start() t1.join() t2.join() def CapSyslog(self, choice=0, iface="eth0"): package = [] print "\n====CapSyslog====" if choice == 0: timeout = 70 else: timeout = 30 filter1 = "udp and port 514 and src 192.168.3.59" pkg = sniff(iface=iface, filter=filter1, timeout=timeout) num = len(pkg) if num == 0: print "can not capture the Syslog" raise else: for i in xrange(num): try: string = pkg[i][3].load except: continue if string.find("syslog - ID") != -1: package.append(pkg[i][3].load) print "CapSyslog successfully" print package if choice == 0: self.CheckConfig(pkg=package) if choice == 1: self.ManuallyCheckConfig(pkg=package, choice=1) elif choice == 2: self.ManuallyCheckConfig(pkg=package, choice=2) def ManuallyCheckConfig(self, pkg, choice): print "\n====ManuallyCheckConfig====" if choice == 1: if pkg[0].find("Link down on port 4") != -1: pass else: print "the sniffered packet is incorrect" raise if choice == 2: if pkg[0].find("Link up on port 4") != -1: pass else: print "the sniffered packet is incorrect" raise self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/a").click( ) #locate monitor system time.sleep(5) self.driver.find_element_by_xpath( "/html/body/form/table/tbody/tr[2]/td/ul/li[2]/div/ul/li/div/ul/li[3]/a" ).click() #locate log self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(8) i = 0 while 1: i = i + 1 if i == 1: Levelxpath = "/html/body/table[2]/tbody/tr/td/a" Timexpath = "/html/body/table[2]/tbody/tr/td[3]" Messagexpath = "/html/body/table[2]/tbody/tr/td[4]" else: Levelxpath = "/html/body/table[2]/tbody/tr[%d]/td/a" % i Timexpath = "/html/body/table[2]/tbody/tr[%d]/td[3]" % i Messagexpath = "/html/body/table[2]/tbody/tr[%d]/td[4]" % i try: Levelvalue = self.driver.find_element_by_xpath(Levelxpath).text Timevalue = self.driver.find_element_by_xpath(Timexpath).text Messagevalue = self.driver.find_element_by_xpath( Messagexpath).text Levelvalue = "ID" + Levelvalue except: print "finish read the table" i = 0 break if pkg[0].find(Timevalue) != -1 and pkg[0].find( Messagevalue) != -1: break if i == 0: print "can't find the sniffered packet in syslog table" raise print "ManuallyCheckConfig successfully\n"
class PrivilegeLevelsCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self,brserType,DUTIP): self.pubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.pubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200: sys.exit() if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.pubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def startWebConfig(self,prjName): print "\n*******************StartWebMac*******************" domain = "http://admin:@%s"%(self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "Start web successfully!" def checkPriviLevelsConfig(self,v): print "\n******Check Privilege Levels Configuration*******" self.pubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Security")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Security")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Switch")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Switch")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("priv_lvl.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("priv_lvl.htm")).click() self.pubModuleEle.location("/html/frameset/frameset/frame[2]") randomNumber = self.pubModuleEle.GetRandomNumber(0,3) if randomNumber == 0: value = "cro_" elif randomNumber == 1: value = "crw_" elif randomNumber == 2: value = "sro_" elif randomNumber == 3: value = "srw_" print "\nStart to check the %sxxx ralated parameters..."%value ele = ["Aggregation", "Diagnostics", "EPS", "ERPS", "ETHER_SAT", "ETH_LINK_OAM","EVC", "IP", "IPMC_LIB", "IPMC_Snooping", "LACP", "LLDP", "Loop_Protect","MAC_Table", "MEP", "MVR", "Maintenance", "Mirroring", "PHY", "PTP", "Port_Security", "Ports", "Private_VLANs", "QoS", "SNMP", "Security", "Spanning_Tree", "Static_Routing", "System", "Timer", "VCL", "VLAN_Translation", "VLANs", "sFlow"] i = 0 while i<len(ele): ele1 = "%s%s" %(value, ele[i]) oldval = int("%d"%v) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(ele1)) getsel = Select(self.driver.find_element_by_id(ele1)) getSetval = getsel.first_selected_option.text getval = int(getSetval) if getval == oldval: print "\nPASS: Current %s%s value is %d" %(value, ele[i],getval) else: print "\nFAIL: Current %s%s value is %s not %d" %(value, ele[i],getval, oldval) time.sleep(1) i = i+1 print "\n***********Check Configuration Finish************" def setPriviLevel(self,v): print "\n************SetPrivilegeLevelConfig**************\n" self.pubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(2) ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Security")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Security")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Switch")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Switch")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("priv_lvl.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("priv_lvl.htm")).click() self.pubModuleEle.location("/html/frameset/frameset/frame[2]") ele = ["Aggregation", "Diagnostics", "EPS", "ERPS", "ETHER_SAT", "ETH_LINK_OAM","EVC", "IP", "IPMC_LIB", "IPMC_Snooping", "LACP", "LLDP", "Loop_Protect","MAC_Table", "MEP", "MVR", "Maintenance", "Mirroring", "PHY", "PTP", "Port_Security", "Ports", "Private_VLANs", "QoS", "SNMP", "Security", "Spanning_Tree", "Static_Routing", "System", "Timer", "VCL", "VLAN_Translation", "VLANs", "sFlow"] value = "%d"%v v1 = "cro_" v2 = "crw_" v3 = "sro_" v4 = "srw_" i = 0 while i<len(ele): ele1 = "%s%s" %(v1, ele[i]) ele2 = "%s%s" %(v2, ele[i]) ele3 = "%s%s" %(v3, ele[i]) ele4 = "%s%s" %(v4, ele[i]) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(ele1)) select = Select(self.driver.find_element_by_id(ele1)) select.select_by_value(value) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(ele2)) select = Select(self.driver.find_element_by_id(ele2)) select.select_by_value(value) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(ele3)) select = Select(self.driver.find_element_by_id(ele3)) select.select_by_value(value) WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(ele4)) select = Select(self.driver.find_element_by_id(ele4)) select.select_by_value(value) print "Set all %s value to %d\n" %(ele[i], v) i=i+1 time.sleep(1) self.clickSaveButton() print "\n************SetPrivilegeLevelFinish**************\n" def clickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath("/html/body/form/p/input").send_keys(Keys.RETURN) if self.browserType == "chrome": self.driver.find_element_by_xpath("/html/body/form/p/input").click()
class WebVlanPortsCase: #Initialize module, include "ip address","browser type","driver object" def __init__(self,brserType,DUTIP): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200: print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome": print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox": print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle #Starting browser... (Chrome,Firefox) def StartWebVlanPorts(self,prjName): print "========startWebVlanPorts========" domain = "http://admin:@%s"%(self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" #Finding the "users" table in the web def ClickButtonType(self): if self.browserType == "firefox": print "clickButtonTypeFirefox" WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("addNewEntry")).send_keys(Keys.RETURN) if self.browserType == "chrome": print "clickButtonTypeChrome" WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("addNewEntry")).click() def ClickSaveButton(self): if self.browserType == "firefox": self.driver.find_element_by_xpath("//input[@value='Save']").send_keys(Keys.RETURN) if self.browserType == "chrome": self.driver.find_element_by_xpath("//input[@value='Save']").click() def factorydefault(self): self.PubModuleEle.factorydefault() time.sleep(3) self.driver.close() #######################################---PortClass---################################################# def EnginePorts(self): print "=========EnginePorts=========" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(1) ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("VLANs")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("VLANs")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("vlan_port.htm")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id("vlan_port.htm")).click() #Set the value of "select" by Id. def SetSelectValue(self,elemId,i): print "========setSelectValue=======" print elemId value = "<>" randomNumber = 0 if elemId == "__ctl__": elemId = elemId+"%d"%i print elemId if elemId == "__ctl__1" or elemId == "porttypev2_" or elemId == "mgmtport": randomNumber = self.PubModuleEle.GetRandomNumber(0,3) print "The PortType random number is %d."%randomNumber if randomNumber == 0: value = "Unaware" elif randomNumber == 1: value = "C-port" elif randomNumber == 2: value = "S-port" elif randomNumber == 3: value = "S-custom-port" if elemId == "__ctl__4" or elemId == "frametypev2_": randomNumber = self.PubModuleEle.GetRandomNumber(0,2) print "The Frame Type random number is %d."%randomNumber if randomNumber == 0: value = "All" elif randomNumber == 1: value = "Tagged" elif randomNumber == 2: value = "Untagged" if elemId == "__ctl__6" or elemId == "selpvlan_": randomNumber = self.PubModuleEle.GetRandomNumber(0,1) print "The Mode random number is %d."%randomNumber if randomNumber == 0: value = "Specific" elif randomNumber == 1: value = "None" self.randomValue = randomNumber if elemId == "__ctl__8" or elemId == "tx_tag_": randomNumber = self.PubModuleEle.GetRandomNumber(0,3) print "The Tx Tag random number is %d."%randomNumber if randomNumber == 0 or randomNumber == 1: value = "Untag_pvid" randomNumber = 0 elif randomNumber == 2: value = "Tag_all" elif randomNumber == 3: value = "Untag_all" if elemId == "porttypev2_" or elemId == "frametypev2_" or elemId == "selpvlan_" or elemId == "tx_tag_": elemId = elemId+"%d"%i print elemId print value select = Select(self.driver.find_element_by_id(elemId)) time.sleep(1) select.select_by_visible_text(value) return randomNumber #Set the value of "checkbox" by Id. def SetCheckboxValue(self,eId,i): print "=======setCheckboxValue======" print eId randomNumber = random.randint(0,1) eId = eId+"%d"%i print eId if randomNumber == 1: ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(eId)).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_id(eId)).click() return randomNumber #Set the value of "input" component. def SetPortsInputValue(self,eId,i): print "======setPortsInputValue=====" print eId randomNumber = random.randint(1,4094) eId = eId+"%d"%i print eId randomNumStr = "%d"%randomNumber print randomNumStr if self.randomValue != 1: self.driver.execute_script('document.getElementById("'+eId+'").value="";') ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(eId)).perform() WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id(eId)).send_keys(randomNumStr) return str(randomNumStr) return "1" def SetPortsParameters(self): global sportsValueStr global MgmtPort print "======setPortsParameters=====" time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") sportsRandomNumber = random.randint(1536,65535) sportsValueStr = (self.PubModuleEle.dec2hex(sportsRandomNumber)) self.driver.execute_script('document.getElementById("tpid").value="";') ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("tpid")).perform() WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("tpid")).send_keys(sportsValueStr) MgmtPort = (str(self.SetSelectValue("mgmtport",-1))) time.sleep(1) self.SetSelectValue("__ctl__",1) time.sleep(1) FrameType_ctl_ = self.SetCheckboxValue("__ctl__",3) time.sleep(1) self.SetSelectValue("__ctl__",4) time.sleep(1) self.SetSelectValue("__ctl__",6) time.sleep(1) self.SetPortsInputValue("__ctl__",7) time.sleep(1) self.SetSelectValue("__ctl__",8) time.sleep(1) for pos in range(1,9): value = self.SetSelectValue("porttypev2_",pos) PortType.append(str(value)) time.sleep(1) value = self.SetCheckboxValue("ingressflt_",pos) if FrameType_ctl_ == 1: value = value^1 IngressFiltering.append(str(value)) time.sleep(1) value = self.SetSelectValue("frametypev2_",pos) FrameType.append(str(value)) time.sleep(1) value = self.SetSelectValue("selpvlan_",pos) VlanMode.append(str(value)) time.sleep(1) value = self.SetPortsInputValue("pvid_",pos) VlanId.append(str(value)) time.sleep(1) value = self.SetSelectValue("tx_tag_",pos) TxTag.append(str(value)) time.sleep(1) time.sleep(2) self.ClickSaveButton() def RebootCheckPorts(self): print "======setPortsParameters=====" self.PubModuleEle.DutReboot() self.EnginePorts() time.sleep(3) self.CheckPortsParameters() def CheckPortsParameters(self): GetPortType = [] GetIngressFiltering = [] GetFrameType = [] GetVlanMode = [] GetVlanId = [] GetTxTag = [] print "======CheckPortsParameters=====" time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") for i in xrange(1,9): PortTypeId = "porttypev2_" + "%d"%i IngressFilteringId = "ingressflt_" + "%d"%i FrameTypeId = "frametypev2_" + "%d"%i VlanModeId = "selpvlan_" + "%d"%i VlanIdId = "pvid_" + "%d"%i TxTagId = "tx_tag_" + "%d"%i time.sleep(1) GetPortType.append(str(self.driver.find_element_by_id(PortTypeId).get_attribute("value"))) GetIngressFiltering.append(str(self.driver.find_element_by_id(IngressFilteringId).get_attribute("checked"))) GetFrameType.append(str(self.driver.find_element_by_id(FrameTypeId).get_attribute("value"))) GetVlanMode.append(str(self.driver.find_element_by_id(VlanModeId).get_attribute("value"))) GetVlanId.append(str(self.driver.find_element_by_id(VlanIdId).get_attribute("value"))) GetTxTag.append(str(self.driver.find_element_by_id(TxTagId).get_attribute("value"))) if(GetIngressFiltering[i-1] == "true"): GetIngressFiltering[i-1] = "1" else: GetIngressFiltering[i-1] = "0" sportsValueStrId = "tpid" MgmtPortId = "mgmtport" GetsportsValueStr = str(self.driver.find_element_by_id(sportsValueStrId).get_attribute("value")) GetMgmtPort = str(self.driver.find_element_by_id(MgmtPortId).get_attribute("value")) for i in xrange(0,8): if GetPortType[i] == PortType[i]: pass else: raise if GetIngressFiltering[i] == IngressFiltering[i]: pass else: raise if GetFrameType[i] == FrameType[i]: pass else: raise if GetVlanId[i] == VlanId[i]: pass else: raise if GetTxTag[i] == TxTag[i]: pass else: raise if GetsportsValueStr == sportsValueStr: pass else: raise if GetMgmtPort == MgmtPort: pass else: raise for i in xrange(0,8): if GetVlanMode[i] == VlanMode[i]: pass else: raise def SetMgmtPortandCheck(self): print "======SetMgmtPortandCheck=====" self.SetMgmtPort("8100","Unaware") received = self.Checkpacket("Unaware",0x8100) self.Check(received) received = self.Checkpacket("Unaware",0x88a8) self.Check(received) received = self.Checkpacket("Unaware",0x9100) self.Check(received) received = self.Checkpacket("Unaware",0x9200) self.Check(received) self.SetMgmtPort("8100","C-port") received = self.Checkpacket("C-port",0x8100) self.Check(received) received = self.Checkpacket("C-port",0x88a8) self.Check(received+1) received = self.Checkpacket("C-port",0x9100) self.Check(received+1) received = self.Checkpacket("C-port",0x9200) self.Check(received+1) self.SetMgmtPort("8100","S-port") received = self.Checkpacket("S-port",0x8100) self.Check(received+1) received = self.Checkpacket("S-port",0x88a8) self.Check(received) received = self.Checkpacket("S-port",0x9100) self.Check(received+1) received = self.Checkpacket("S-port",0x9200)####can't find sent packet self.Check(received+1) self.SetMgmtPort("8101","S-custom-port") received = self.Checkpacket("S-custom-port",0x8101) self.Check(received) received = self.Checkpacket("S-custom-port",0x8100) self.Check(received+1) received = self.Checkpacket("S-custom-port",0x88a8) self.Check(received+1) received = self.Checkpacket("S-custom-port",0x9100) self.Check(received+1) received = self.Checkpacket("S-custom-port",0x9200) self.Check(received+1) def SetMgmtPort(self,Ethertype,PortType): print "======setMgmtPortsParameters=====" time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.driver.execute_script('document.getElementById("tpid").value="";') ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("tpid")).perform() WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("tpid")).send_keys(Ethertype) select = Select(self.driver.find_element_by_id("mgmtport")) select.select_by_visible_text(PortType) time.sleep(1) self.ClickSaveButton() time.sleep(3) self.Refresh() time.sleep(1) def Refresh(self): print "======Refresh=====" time.sleep(1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("refresh")).perform() WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("refresh")).click() def Checkpacket(self,PortType,Ethertype): print "======Checkpacket=====" time.sleep(1) parameters = PubReadConfig("") mgmtmac = str(parameters.GetParameter("PC","MGMTMAC")) mgmtip = str(parameters.GetParameter("PC","MGMTIP")) dstip = str(parameters.GetParameter("DUT1","IP")) mgmtnic = str(parameters.GetParameter("PC","MGMTNIC")) if PortType == "Unaware": eth = Ether(src=mgmtmac,type=0x0806,dst="ff:ff:ff:ff:ff:ff") arp = ARP(hwtype=0x0001,ptype=0x0800,op=0x0001,hwsrc=mgmtmac,psrc="192.168.1.42",pdst=dstip,hwdst="ff:ff:ff:ff:ff:ff") a = eth/arp else: eth = Ether(src=mgmtmac,type=Ethertype,dst="ff:ff:ff:ff:ff:ff") priority = random.randint(0,7) dot1q=Dot1Q(type=0x0806,prio=priority,vlan=1) arp = ARP(hwtype=0x0001,ptype=0x0800,op=0x0001,hwsrc=mgmtmac,psrc="192.168.1.42",pdst=dstip,hwdst="ff:ff:ff:ff:ff:ff") a = eth/dot1q/arp os.system("ifconfig eth0 promisc") Reply = srp1(a,iface=mgmtnic,timeout=3) os.system("ifconfig eth0 -promisc") if Reply == None: return 0 else: if Reply.op == 2: return 1 else: return 0 def Check(self,value): if value == 1: pass else: raise #######################################---VlanClass---################################################# #Finding the "users" table in the web def EngineVlan(self): print "========engineValn========" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") time.sleep(1) ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("VLANs")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("VLANs")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("VLAN Membership")).perform() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("VLAN Membership")).click() def SetPortMembers(self,prefix,n): for i in range(1,n+1): portPrefix="%s%d"%(prefix,i) intValue = random.randint(0,1) if intValue == 1: elem = self.driver.find_element_by_id(portPrefix) elem.click() def AddNewVlan(self,num): time.sleep(1) number="%d"%(num+1) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") print '=========addNewVlan=======' for i in range(1,num+1): time.sleep(2) #self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(1) prefix = 'vlan' vlanName="%s%d"%(prefix,i+1) print vlanName vlanId="%d"%(i+1) print "The vlan id is "+vlanId self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("addNewEntry")).perform() WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("addNewEntry")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(1) vlanIdElemName="vid_new_1" print "The element name is "+vlanIdElemName+" in the web." vlanNameElemName="name_new_1" print "The vlan element name is "+vlanNameElemName ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(vlanIdElemName)).perform() self.PubModuleEle.SetInputValue(vlanIdElemName) WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id(vlanIdElemName)).send_keys(vlanId) ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(vlanNameElemName)).perform() WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id(vlanNameElemName)).send_keys(vlanName) self.SetPortMembers("mask_new_1_",8) self.ClickSaveButton() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") time.sleep(2) WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("addNewEntry")) ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("NumberOfEntries")).perform() self.driver.execute_script('document.getElementById("NumberOfEntries").value="";') WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("NumberOfEntries")).send_keys(number) self.driver.find_element_by_xpath("//input[@value='Refresh']").click() def AddVlanHandle(self,num): print "=======addVlanHandle======" self.driver.implicitly_wait(5) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.AddNewVlan(num) #Add new user #delete the users that have been added. def DeleteVlan(self,num): print '========deleteVlan========' time.sleep(1) number="%d"%(num+1) time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("NumberOfEntries")).perform() self.driver.execute_script('document.getElementById("NumberOfEntries").value="";') WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("NumberOfEntries")).send_keys(number) self.driver.find_element_by_xpath("//input[@value='Refresh']").click() time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") for pos in range(1,num+1): prefix = 'delete_' delElemTag="%s%d"%(prefix,(pos+1)) ActionChains(self.driver).move_to_element(self.driver.find_element_by_id(delElemTag)).perform() WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id(delElemTag)).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.ClickSaveButton() time.sleep(3) #check the vlan added is existence after rebooting or not. def CheckElem(self,num): time.sleep(5) number="%d"%(num+1) print "========checkElem========" st = self.PubModuleEle.ConnectionServer() print "Connection status %d."%(st) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") self.EngineVlan() time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[2]") n = 0 print "Start to check the element...\n" ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("NumberOfEntries")).perform() self.driver.execute_script('document.getElementById("NumberOfEntries").value="";') WebDriverWait(self.driver, 30).until(lambda driver : driver.find_element_by_id("NumberOfEntries")).send_keys(number) self.driver.find_element_by_xpath("//input[@value='Refresh']").click() time.sleep(3) n = self.driver.execute_script('return document.getElementById("vlanData").rows.length;') n = n-3 if n == num: time.sleep(2) print "Add vlan number is %d."%(n) print "The element checked is existence, check successfully!" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") #self.driver.close() else: time.sleep(2) print "n=%d"%(n) print "The element checked is not existence, check failed!" self.PubModuleEle.location("/html/frameset/frameset/frame[2]") raise #self.driver.close() #Because the operation way of the "submit" button is different in the different broswer, so we need distinguish to use. def ClickDeleteVlanButton(self): if self.browserType == "firefox": WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_name("warm")).send_keys(Keys.RETURN) if self.browserType == "chrome": WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_name("warm")).click() #Find the "Restart Device" table and reboot the system, and check when the server is normal connection. def CheckRebootElement(self,num): print "======checkRebootElement=====" time.sleep(3) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("VLANs")).click() WebDriverWait(self.driver, 30).until(lambda driver :driver.find_element_by_link_text("Configuration")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[1]") WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Maintenance")).click() time.sleep(1) WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Restart Device")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") self.ClickDeleteVlanButton() n = 0 time.sleep(2) while True: n = n + 1 st = self.PubModuleEle.ConnectionServer() if st == 0: print "Connection status %d. Failure... [%d]"%(st,n) if st == 200: print "Connection status %d. Success! [%d]"%(st,n) break time.sleep(5) if n==10: print "No connection,exit after 5 seconds!" time.sleep(5) self.driver.close() print "OK,start to check element!" self.CheckElem(num)
class WebMirrorCase: def __init__(self,brserType="firefox",DUTIP="192.168.3.74"): self.PubModuleEle = PubModuleCase(DUTIP) print "Initializing..." self.browserType = brserType.lower() self.DUTIPAddr = DUTIP st = self.PubModuleEle.ConnectionServer() print "Connection status %d."%st if st != 200 : print "Can't connect server!" sys.exit() else: print "Connection successful!" if self.browserType == "chrome" : print "Starting chrome browser..." self.driver = webdriver.Chrome() if self.browserType == "firefox" : print "Starting firefox browser..." self.driver = webdriver.Firefox() self.PubModuleEle.SetPubModuleValue(self.driver) self.tmp_handle = self.driver.current_window_handle def StartWebMirror(self,prjName): print "========startWebMirror========" domain = "http://admin:@%s"%(self.DUTIPAddr) print domain self.driver.get(domain) print self.driver.title assert (self.driver.title == prjName) print "start web successfully!" self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("mirror.htm")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_id("mirror.htm")).click() def into_mirror_web(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("mirror.htm")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_id("mirror.htm")).click() def ConfigMirror(self): self.PubModuleEle.location("/html/frameset/frameset/frame[2]") select1 = Select(self.driver.find_element_by_id("portselect")) index1 = random.randint(0,8) index1 = str(index1) select1.select_by_value(index1) mirror_conf = {'port_to_mirror' : index1} mirror_port = {'0':'Disabled','1':'Rx only','2':'Tx only','3':'Enabled'} for i in range (1,9): i = str(i) select_path = "mode_%s"%i select2 = Select(self.driver.find_element_by_id(select_path)) if i == index1: index2 = random.randint(0,1) else: index2 = random.randint(0,3) index2 = str(index2) time.sleep(1) select2.select_by_value(index2) mirror_conf[i] = mirror_port[index2] self.driver.find_element_by_xpath("/html/body/form/p/input[2]").click() return mirror_conf def DeleteMirror(self): self.PubModuleEle.location("/html/frameset/frameset/frame[1]") self.driver.find_element_by_link_text("Maintenance").click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Factory Defaults")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Factory Defaults")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_name("factory")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_name("factory")).click() temp = 1 while True: try: elem = self.driver.find_element_by_xpath("/html/body/h1") if elem.text == "Configuration Factory Reset Done": print "=======Factory Default successfully======\n" break raise error except: time.sleep(3) if temp != 10: temp = temp+1 else: print "there is an error while factory default" break def CheckMirrorConfig(self,conf): print "=====DUT will reboot soon=======" re=self.PubModuleEle.DutReboot() if re == 0: sys.exit() time.sleep(2) self.PubModuleEle.location("/html/frameset/frameset/frame[1]") ActionChains(self.driver).move_to_element(self.driver.find_element_by_link_text("Configuration")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_link_text("Configuration")).click() ActionChains(self.driver).move_to_element(self.driver.find_element_by_id("mirror.htm")).perform() WebDriverWait(self.driver,30).until(lambda driver: driver.find_element_by_id("mirror.htm")).click() self.PubModuleEle.location("/html/frameset/frameset/frame[2]") port_select = Select(self.driver.find_element_by_id("portselect")) if port_select.first_selected_option.text != conf["port_to_mirror"]: print "the Configuration changed after the dut reboot" sys.exit() else: for i in range (1,9): i = str(i) select_path = "mode_%s"%i select = Select(self.driver.find_element_by_id(select_path)) if select.first_selected_option.text != conf[i]: print "the Configuration changed after the dut Rboot" sys.exit() print "===all Configuration is correct=====" def CloseSession(self): self.driver.close()