码迷,mamicode.com
首页 > 其他好文 > 详细

自动化测试-数据驱动实践(新增2)

时间:2019-05-31 13:12:01      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:cti   gem   detail   work   input   scroll   click   off   Nid   

对上次的脚本进行了小改动,部分字段(如身份证等唯一项)增加了数据驱动,读取Excel,输入框定位将xpath改为css,执行一次的时间减少了大约1分半

ExcelUitl.py

#encoding=utf-8
from openpyxl import load_workbook

class ParseExcel(object):

    def __init__(self, excelPath, sheetName):
        # 将要读取的excel加载到内存
        self.wb = load_workbook(excelPath)
        # 通过工作表名称获取一个工作表对象
        self.sheet = self.wb.get_sheet_by_name(sheetName)
        # 获取工作表中存在数据的区域的最大行号
        self.maxRowNum = self.sheet.max_row

    def getDatasFromSheet(self):
        # 用于存放从工作表中读取出来的数据
        dataList = []
        for line in self.sheet.rows:
            # 遍历工作表中数据区域的每一行,
            # 并将每行中各个单元格的数据取出存于列表tmpList中,
            # 然后再将存放一行数据的列表添加到最终数据列表dataList中
            tmpList = []
            #如果单元格为空,则在列表中插入一个为空的unicode字符串,以免输入账号密码时报错(重点人员4列数据)
            if line[1].value == None:
                tmpList.append(u"")
            else:
                tmpList.append(line[1].value)
            if line[2].value == None:
                tmpList.append(u"")
            else:
                tmpList.append(line[2].value)
            if line[3].value == None:
                tmpList.append(u"")
            else:
                tmpList.append(line[3].value)
            if line[4].value == None:
                tmpList.append(u"")
            else:
                tmpList.append(line[4].value)
            #第5列数据为断言结果
            tmpList.append(line[5].value)
            dataList.append(tmpList)
        # 将获取工作表中的所有数据的迭代对象返回
        # 因为工作表中的第一行是标题行,所以需要去掉
        return dataList[1:]

if __name__ == __main__:
    excelPath = uD:\\test\\DataDrivenTesting\\重点人员新增测试.xlsx
    sheetName = u"身份信息"
    pe = ParseExcel(excelPath, sheetName)
    #print pe.getDatasFromSheet()
    for i in pe.getDatasFromSheet():
        for j in i:
            print j , type(j),
        print
        #i[1],i[2]

new_person.py

#encoding=utf-8
import time
import unittest
import ddt
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver import ActionChains
from ExcelUtil import ParseExcel

excelPath = u"D:\\test\\DataDrivenTesting\\重点人员新增测试.xlsx"
sheetName = u"身份信息"
#ParseExcel类的实例对象
excel = ParseExcel(excelPath,sheetName)

@ddt.ddt
class addPerson(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        #启动浏览器
        cls.driver = webdriver.Ie(executable_path = "D:\\IEDriverServer")
        cls.driver.maximize_window()
        #需要访问的地址
        url = "xxx"
        # 访问重点人员平台
        cls.driver.get(url)
        time.sleep(2)
        # 找到页面中的登录用户名输入框元素
        userName = cls.driver.find_element_by_id("username")
        # 输入用户名
        userName.send_keys("test_yongh")
        # 找到页面中登录密码输入框元素
        pwd = cls.driver.find_element_by_id("password")
        # 输入密码
        pwd.send_keys("a123456")
        #找到页面中的登录按钮元素
        loginButton = cls.driver.find_element_by_id("button")
        #点击登录按钮
        loginButton.click()
        #设置等待时间
        cls.driver.implicitly_wait(10)
        # 进入 人员信息管理页面
        cls.driver.find_element_by_xpath(//*[@id="management_tab"]/a[2]/li/span[2]).click()

    @ddt.data(*excel.getDatasFromSheet())
    def test_addPerson(self,data):
        # 读取Excel中的身份信息及期望结果,人员身份证、人员姓名、关系人身份证、关系人姓名
        id,chineseName,relationId,relationName,expectdata = tuple(data)
        #设置等待时间
        self.driver.implicitly_wait(5)
        #在人员信息页面,点击“新增”
        addBtn = self.driver.find_element_by_xpath(//span[@class="btns_icon_add btns_icon"])
        addBtn.click()
        time.sleep(1)
        #找到身份证号输入框并输入身份证号,使用css比xpath稍微快一点
        idEle = self.driver.find_element_by_css_selector(input[ng-model = "detailInfo.personList.idcard"])
        idEle.send_keys(id)
        #由于焦点移出身份证输入框时会对身份证进行校验,会清空其他数据,所以先点击一下空白区域
        action = ActionChains(self.driver)
        action.move_by_offset(0,0).click().perform()
        time.sleep(1)
        #找到中文姓名输入框并输入姓名
        chineseNameEle = self.driver.find_element_by_css_selector(input[ng-model="detailInfo.personList.chineseName"])
        chineseNameEle.clear()
        chineseNameEle.send_keys(chineseName)
        #找到民族下拉框并选择一个民族 ,使用Select模块选中下拉框的速度并没有提升
        nationsSelect = self.driver.find_element_by_xpath(//select[@ng-model="detailInfo.personList.nationCode"])
        allNations = nationsSelect.find_elements_by_tag_name("option")
        allNations[2].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_css_selector(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[6].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"]) #用于翻页
        #选择一个政治面貌
        allPolitics = politicsSelect.find_elements_by_tag_name("option")
        allPolitics[1].click()

        # 页面向下载滚动到“政治面貌”
        self.driver.execute_script("arguments[0].scrollIntoView();", politicsSelect)

        # 找到兵役状况下拉并选择一个兵役状况
        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_css_selector(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[2].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"]) #用于翻页
        # 选择一个职业类别
        allWorkTypes = workTypeSelect.find_elements_by_tag_name("option")
        allWorkTypes[128].click()

        # 页面向下载滚动到“职业类别”
        self.driver.execute_script("arguments[0].scrollIntoView();", workTypeSelect)

        # 找到职业输入框并输入职业名称
        work = self.driver.find_element_by_css_selector(input[ng-model="works.work"])
        work.send_keys(u"教师")
        # 找到电脑数量输入框并输入数据
        compute = self.driver.find_element_by_css_selector(input[ng-model="devices.num"])
        compute.send_keys(2)
        #找到身高输入框
        height = self.driver.find_element_by_css_selector(input[ng-model="detailInfo.personList.height"])
        # 页面向下载滚动到“身高”
        self.driver.execute_script("arguments[0].scrollIntoView();", height)
        #输入身高
        height.send_keys(168)
        # 找到体重输入框
        weight = self.driver.find_element_by_css_selector(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_css_selector(input[ng-model="detailInfo.personList.feautres"])
        # 输入特殊
        feature.send_keys(u"")
        #滚动到页面最下方
        self.driver.execute_script("window.scrollTo(100,document.body.scrollHeight);")
        # 找到关系人身份证输入框
        relationIdEle = self.driver.find_element_by_xpath(//input[@ng-model="personnel.idnumber"])
        # 输入关系人身份证号
        relationIdEle.send_keys(relationId)
        # 由于焦点移出身份证输入框时会对身份证进行校验,会清空其他数据,所以先点击一下空白区域
        action = ActionChains(self.driver)
        action.move_by_offset(0, 0).click().perform()
        time.sleep(1)
        # 找到关系人姓名输入框并输入姓名
        relationNameEle = self.driver.find_element_by_xpath(//input[@ng-model="personnel.name"])
        relationNameEle.clear()
        relationNameEle.send_keys(relationName)
        # 找到关系人民族下拉框并选择“汉族”
        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)
    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

if __name__ == "__main__":
    unittest.main()

 

自动化测试-数据驱动实践(新增2)

标签:cti   gem   detail   work   input   scroll   click   off   Nid   

原文地址:https://www.cnblogs.com/test-chen/p/10954582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!