美文网首页接口测试
Python结合Jmeter实现接口和UI自动化实例讲解

Python结合Jmeter实现接口和UI自动化实例讲解

作者: 莫依痕 | 来源:发表于2018-03-08 15:36 被阅读0次

1、先在Jmeter里设置好接口内容

Jmeter数据.png

2、不打开Jmeter.bat,这里讲解两种方法调用Jmeter

a、通过自己写bat文件,把命令直接放在bat文件里运行即可
第一个地址是Jmeter.bat的地址,第二个地址是具体要跑的Jmeter脚本的地址

E:\\application20160314\\apache-jmeter-3.2\\bin\\jmeter.bat -n -t E:\\JmeterResult\\exam.jmx
start.bat.png

b、输入命令,直接用os.system(参数)方法,只支持一行的命令,多行的话,还是使用第一种方法实现

import os

cmd = "E:\\application20160314\\apache-jmeter-3.2\\bin\\jmeter.bat -n -t E:\\JmeterResult\\exam.jmx"
os.system(cmd)

3、调用结束后,去具体项目查看实现的结果

考试列表.png

4、调用接口创建完考试后,因为项目新建时,案子ID是数据库递增,不允许用户随机生成,故无法使用接口直接创建案子,这里用python实现UI自动化,创建案子

附上python完整代码:

#coding=utf-8
from selenium import webdriver
from closeThread import closeThread
import time
from datetime import datetime
import os
import random

class examTest():
    def newExam(self):
        cmd = "E:\\application20160314\\apache-jmeter-3.2\\bin\\jmeter -n -t E:\\JmeterResult\\exam.jmx"
        os.system(cmd)
    #打开考试网页
    def openUrl(self,driver):
        driver.maximize_window()
        driver.implicitly_wait(5)
        #打开在线考试网页,由于是具体项目,这里不方便给出具体的URL
        driver.get("http://xxx--xxx-xxx.beta.101.com")
        time.sleep(2)

    #登录
    def loginExam(self,driver):
        driver.find_element_by_xpath("//*[@id='root']/div/div[2]/div[1]/div[1]/input").send_keys("123456")
        driver.find_element_by_xpath("//*[@id='root']/div/div[2]/div[1]/div[2]/input").send_keys("123456")
        time.sleep(2)
        driver.find_element_by_xpath("//*[@id='root']/div/div[2]/div[1]/button").click()
        time.sleep(2)

    #考试列表进行考试
    def createExam(self,driver):
        #进入考试列表
        driver.find_element_by_xpath("//*[@id='containerBox']/div/div[2]/div/ul/li[1]/span/span[1]").click()
        #点击开始考试按钮
        driver.find_element_by_xpath("//*[@id='containerBox']/div/div[2]/div/div[1]/div/div[1]/table/tbody/tr[1]/td[5]/button").click()
        time.sleep(2)
        # 输入文档标题
        driver.find_element_by_xpath("//*[@id='addProject-dialog-content']/dl/dd[2]/input").send_keys(strGen(random.randint(1,50)))
        # 点击确定按钮
        driver.find_element_by_xpath("//*[@id='addProject']/div[3]/div[2]/button[2]").click()
        time.sleep(3)
        # 点击编辑按钮
        driver.find_element_by_xpath("//*[@id='containerBox']/div/div[2]/div/div[1]/div/div[1]/table/tbody/tr[1]/td[5]/div/i").click()
        time.sleep(3)
    def loginDesign(self,driver):
        driver.find_element_by_xpath("//*[@id='root']/div/div[2]/div[1]/div[1]/input").send_keys("161018")
        driver.find_element_by_xpath("//*[@id='root']/div/div[2]/div[1]/div[2]/input").send_keys("123456")
        driver.find_element_by_xpath("//*[@id='root']/div/div[2]/div[1]/button").click()
        time.sleep(5)
    # 开始考试并完成考试
    def finishExam(self,driver):
        # 点击“完成考试”按钮
        driver.find_element_by_xpath("//*[@id='root']/div/div[3]/div[2]/div/div[1]/div/div[2]/div/div/button/span").click()
        time.sleep(2)
        driver.find_element_by_xpath("//*[@id='completeExam']/div[3]/div[2]/button[2]/span").click()
        driver.find_element_by_xpath("//*[@id='completeExam']/div[3]/div[2]/button/span").click()
        time.sleep(2)

    def selfValue(self,driver):
        # 点击自评按钮
        driver.find_element_by_xpath("//*[@id='containerBox']/div/div[2]/div/div[1]/div/div[1]/table/tbody/tr[1]/td[5]/div/i[2]").click()
        time.sleep(1)
        driver.find_element_by_xpath("//*[@id='containerBox']/div/div[2]/div/ul/li[1]/span").click()
        # 输入批改意见,获取当前时间并格式化:test2018-03-08 14:54:33
        driver.find_element_by_xpath("//*[@id='containerBox']/div/div[2]/div/div/div[1]/div[2]/div[1]/textarea").send_keys("test"+str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
        # 勾选成绩
        driver.find_element_by_xpath("//*[@id='containerBox']/div/div[2]/div/div/div[2]/div[2]/div/label[1]/span[1]/input").click()
        time.sleep(1)
        # 点击自评按钮
        driver.find_element_by_xpath("//*[@id='containerBox']/div/button/span").click()
        time.sleep(2)
        # 勾选自评的checklist
        clickChecklist("checklist__checkitem_checkStatus")
    # 提交CheckBox勾选
    def selfCommit(self,driver):
        # 勾选提交改卷的节点
        driver.find_element_by_xpath("//*[@id='containerBox']/div/div[2]/div[2]/div[1]/i").click()
        time.sleep(1)
        # clickChecklist("submitPapers__status")
        # 点击提交的确定按钮
        driver.find_element_by_xpath("//*[@id='containerBox']/div/div[3]/div/button/span").click()
        time.sleep(2)
        # 勾选提交的checklist
        clickChecklist("checklist__checkitem_checkStatus")

# 生成数字、字母和部分特殊字符的随机数
def strGen(num):
    list1= [chr(i) for i in range(65,91)] + [chr(i) for i in range(97,123)] + [ str(i) for i in range(10)] + ['.','-','~','_']
    value=""
    for i in range(num):
        value = value + list1[random.randint(0, len(list1)-1)]
    return value
# 勾选checklist
def clickChecklist(classValue):
    checkboxs = driver.find_elements_by_tag_name('i')  
    for checkbox in checkboxs:
        className = checkbox.get_attribute("class")
        if className.find(classValue) > -1:
            checkbox.click()
    time.sleep(1)
    # 点击“检查完成”按钮
    driver.find_element_by_xpath("//*[@id='checkList']/div[3]/button").click()
    time.sleep(5)
# 自评各个节点进行批改意见和成绩录入
def clickSelfValue():
    checkboxs = driver.find_elements_by_xpath(".//*[@type='li']")
    for  i in checkboxs:
        i.click()
        time.sleep(1)
        driver.find_element_by_xpath("//*[@id='containerBox']/div/div[2]/div/div/div[1]/div[2]/div[1]/textarea").send_keys("test"+str(random.randint(1,50)))
        driver.find_element_by_xpath("//*[@id='containerBox'/div/div[2]/div/div/div[2]/div[2]/div/label[1]/span[1]/span").click()
        
#------------------------------使用方法-------------------------------------------
driver = webdriver.Chrome()
exam = examTest()
exam.newExam()
exam.openUrl(driver)
exam.loginExam(driver)
exam.createExam(driver)
exam.loginDesign(driver)
exam.finishExam(driver)
exam.selfValue(driver)
exam.selfCommit(driver)
driver.quit()

close = closeThread()
close.killThread()

相关文章

网友评论

    本文标题:Python结合Jmeter实现接口和UI自动化实例讲解

    本文链接:https://www.haomeiwen.com/subject/ncdyfftx.html