自动化测试-数据驱动实践(新增)
自动化测试-数据驱动实践(新增人员)
对人员新增进行自动化,流程如下:
1、登录系统
2、进入人员管理模块
3、点击“新增”
4、输入全部必填项(非必填不影响功能)
5、点击“提交”
#encoding=utf-8 import time import unittest from selenium import webdriver from selenium.webdriver import ActionChains class addPerson(unittest.TestCase): def setUp(self): #启动浏览器 self.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer") #self.driver = webdriver.Chrome(executable_path = "D:\\chromedriver") #谷歌浏览器登录页面多个字段出现乱码 self.driver.maximize_window() #需要访问的地址,公司网址使用xxx代替 url = "XXX" # 访问重点人员平台 self.driver.get(url) time.sleep(2) # 找到页面中的登录用户名输入框元素 userName = self.driver.find_element_by_id("username") # 输入用户名 userName.send_keys("chenyl4") # 找到页面中登录密码输入框元素 pwd = self.driver.find_element_by_id("password") # 输入密码 pwd.send_keys("a123456") #找到页面中的登录按钮元素 loginButton = self.driver.find_element_by_id("button") #点击登录按钮 loginButton.click() time.sleep(5) def test_addPerson(self): #进入 人员信息管理页面 self.driver.find_element_by_xpath('//*[@id="management_tab"]/a[2]/li/span[2]').click() #在人员信息页面,点击“新增” addBtn = self.driver.find_element_by_xpath('//span[@class="btns_icon_add btns_icon"]') addBtn.click() time.sleep(1) #找到身份证号输入框并输入身份证号 id = self.driver.find_element_by_xpath('//*[@id="eInformation"]/div[1]/div[1]/div[2]/input') #id = self.driver.find_element_by_xpath('//input[@ng-model = "detailInfo.personList.idcard"]') id.send_keys("252322196705132538") #由于焦点移出身份证输入框时会对身份证进行校验,会清空其他数据,所以先点击一下空白区域 action = ActionChains(self.driver) action.move_by_offset(0,0).click().perform() time.sleep(1) #找到中文姓名输入框并输入姓名 chineseName = self.driver.find_element_by_xpath('//input[@ng-model = "detailInfo.personList.chineseName"]') chineseName.clear() chineseName.send_keys(u"段正淳") #找到民族下拉框并选择“汉族” nationsSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.nationCode"]') allNations = nationsSelect.find_elements_by_tag_name("option") allNations[1].click() #身份证有效点击“长期”,选择具体时间后续优化 idValidity = self.driver.find_element_by_xpath('//span[@class="operationBtn" and text()="长期"]') idValidity.click() #找到籍贯下拉框并选择一个籍贯(经开区) nativePlaceSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.birthplaceCode"]') allPlaces = nativePlaceSelect.find_elements_by_tag_name("option") allPlaces[469].click() # 找到户籍地址下拉框并选择一个籍贯(经开区) residenceAddressSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.registerAddress"]') allAddresses = residenceAddressSelect.find_elements_by_tag_name("option") allAddresses[469].click() #找到户籍详址并输入具体地址 addressDetail = self.driver.find_element_by_xpath('//input[@ng-model="detailInfo.personList.registerAddressDetail"]') addressDetail.clear() addressDetail.send_keys(allAddresses[469].text + u"89号路") #选择现住址,四个级联下拉,小区、楼栋、单元、门牌号 #找到小区下拉并选择一个小区 gardenSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.placeCode"]') allGardens = gardenSelect.find_elements_by_tag_name("option") allGardens[8].click() time.sleep(1) # 找到楼栋下拉并选择一个楼栋,必须先选择小区,楼栋下拉才会显示出数据 buildingSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.buildCode"]') allBuildings = buildingSelect.find_elements_by_tag_name("option") allBuildings[1].click() time.sleep(1) # 找到单元下拉并选择一个单元,必须先选择小区、楼栋,单元下拉才会显示出数据 unitSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.unitCode"]') allUnits = unitSelect.find_elements_by_tag_name("option") allUnits[1].click() time.sleep(1) # 找到单元下拉并选择一个门牌,必须先选择小区、楼栋、单元,门牌下拉才会显示出数据 houseSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.houseCode"]') allHouses = houseSelect.find_elements_by_tag_name("option") allHouses[1].click() time.sleep(1) # 找到政治面貌下拉 politicsSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.politicsCode"]') #页面向下载滚动到“政治面貌” self.driver.execute_script("arguments[0].scrollIntoView();", politicsSelect) #选择一个政治面貌 allPolitics = politicsSelect.find_elements_by_tag_name("option") allPolitics[1].click() # 找到兵役状况下拉并选择一个兵役状况 armySelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.armyCode"]') allArmys = armySelect.find_elements_by_tag_name("option") allArmys[1].click() # 找到状态下拉并选择一个状态 stateSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.inStateCode"]') allStates = stateSelect.find_elements_by_tag_name("option") allStates[1].click() # 找到婚姻状况下拉并选择一个状况 marriageSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.marriageCode"]') allMarriages = marriageSelect.find_elements_by_tag_name("option") allMarriages[1].click() # 找到联系方式输入框并输入一个手机号 telephone = self.driver.find_element_by_xpath('//input[@ng-model="detailInfo.personList.telephone"]') telephone.send_keys("18701568734") # 找到学历下拉并选择一个学历 enducationSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.cultureCode"]') allEnducations = enducationSelect.find_elements_by_tag_name("option") allEnducations[12].click() # 找到宗教信仰下拉并选择一个宗教 religionSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.religionCode"]') allReligions = religionSelect.find_elements_by_tag_name("option") allReligions[1].click() # 找到是否有驾照下拉并选择“无”,选择“是”会多出两个必填输入框,先不选,后期再优化 drivingLicenseSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.isDriver"]') allLicenses = drivingLicenseSelect.find_elements_by_tag_name("option") allLicenses[1].click() # 找到管控级别下拉并选择一个级别 controlLevelSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.controllevelCode"]') allLevels = controlLevelSelect.find_elements_by_tag_name("option") allLevels[1].click() # 找到职业下拉 workTypeSelect = self.driver.find_element_by_xpath('//select[@ng-model="works.workTypeCode"]') # 页面向下载滚动到“职业类别” self.driver.execute_script("arguments[0].scrollIntoView();", workTypeSelect) # 选择一个职业类别 allWorkTypes = workTypeSelect.find_elements_by_tag_name("option") allWorkTypes[128].click() # 找到职业输入框并输入职业名称 work = self.driver.find_element_by_xpath('//input[@ng-model="works.work"]') work.send_keys(u"教师") # 找到电脑数量输入框并输入数据 compute = self.driver.find_element_by_xpath('//input[@ng-model="devices.num"]') compute.send_keys(2) #找到身高输入框 height = self.driver.find_element_by_xpath('//input[@ng-model="detailInfo.personList.height"]') # 页面向下载滚动到“身高” self.driver.execute_script("arguments[0].scrollIntoView();", height) #输入身高 height.send_keys(168) # 找到体重输入框 weight = self.driver.find_element_by_xpath('//input[@ng-model="detailInfo.personList.weight"]') # 输入体重 weight.send_keys(56) # 找到血型下拉并选择一个血型 bloodTypeSelect = self.driver.find_element_by_xpath('//select[@ng-model="detailInfo.personList.bloodtypeCode"]') allBloodTyoes = bloodTypeSelect.find_elements_by_tag_name("option") allBloodTyoes[1].click() # 找到有无特殊特征输入框 feature = self.driver.find_element_by_xpath('//input[@ng-model="detailInfo.personList.feautres"]') # 输入特殊 feature.send_keys(u"无") #滚动到页面最下方 self.driver.execute_script("window.scrollTo(100,document.body.scrollHeight);") # 找到关系人身份证输入框 relationId = self.driver.find_element_by_xpath('//input[@ng-model="personnel.idnumber"]') # 输入关系人身份证号 relationId.send_keys("110120197205219823") # 由于焦点移出身份证输入框时会对身份证进行校验,会清空其他数据,所以先点击一下空白区域 action = ActionChains(self.driver) action.move_by_offset(0, 0).click().perform() time.sleep(1) # 找到关系人姓名输入框并输入姓名 relationName = self.driver.find_element_by_xpath('//input[@ng-model="personnel.name"]') relationName.clear() relationName.send_keys(u"阮星竹") # 找到关系人民族下拉框并选择“汉族” relationNationsSelect = self.driver.find_element_by_xpath('//select[@ng-model="personnel.nationCode"]') allRelationNations = relationNationsSelect.find_elements_by_tag_name("option") allRelationNations[1].click() # 找到关系人政治面貌下拉 relationPoliticsSelect = self.driver.find_element_by_xpath('//select[@ng-model="personnel.politicsCode"]') # 选择一个政治面貌 allRelationPolitics = relationPoliticsSelect.find_elements_by_tag_name("option") allRelationPolitics[1].click() # 找到与人员关系下拉 relationSelect = self.driver.find_element_by_xpath('//select[@ng-model="personnel.personnelRelationCode"]') # 选择一个关系 allRelations = relationSelect.find_elements_by_tag_name("option") allRelations[1].click() # 找到关系人类型下拉 relationTypeSelect = self.driver.find_element_by_xpath('//select[@ng-model="personnel.relationTypeCode"]') # 选择一个关系类型 allRelationTypes = relationTypeSelect.find_elements_by_tag_name("option") allRelationTypes[56].click() #找到提交并点击 submitBtn = self.driver.find_element_by_xpath('//span[@class="btns_icon3 btns_icon"]') submitBtn.click() # #找到保存按钮并点击 # saveBtn = self.driver.find_element_by_xpath('//span[@class="btns_icon4 btns_icon"]') # saveBtn.click() time.sleep(3) def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
总结:
本来想做数据驱动,但经过多次执行脚本后,发现多个问题,未使用数据驱动,问题如下,后续优化:
1、页面加载时间较长
2、登录后,点击左侧导航时容易报错
3、新增时,字段较长,元素定位慢,消耗的时间过长,尤其是下拉框的选择,有的下拉框会有上千个选项,需要优化下拉选择
4、页面较长,需要多次翻页
5、执行一次需要的时间为4-8分钟,比手工都慢