Python、Java 集成Jenkins做自动化的记录

安装Virtualenv

pip install virtualenv

pip install virtualenvwrapper # 这是对virtualenv的封装版本,一定要在virtualenv后安装

创建虚拟环境

选择一个用来存放虚拟环境的文件,如E:/python3

cd E:python3  # 进入该文件
virtualenv envname   # 创建一个名字为envname的虚拟环境
dir     # 查看当前目录可以知道一个envname的文件已经被创建

安装selenium

pip install selenium

 查看是否已经安装 pip show selenium

下载

Chromedriver 放到python的Scripts路径下  与selenium版本需要对应

编写代码测试:

#!/usr/bin/env python2

# -*- coding: utf-8 -*-

from selenium import webdriver  # 导入webdriver包

driver = webdriver.Chrome()

driver.get("https://www.baidu.com")

启动虚拟环境

# 进入虚拟环境文件
cd envname
# 进入相关的启动文件夹
cd Scripts

activate  # 启动虚拟环境
deactivate # 退出虚拟环境

配置脚本的环境变量

python_env="脚本路径",将%python_env%\ 添加到path后面

Pycharm调试BUG Error: no such option: --multiproc

去除第一个系统返回参数:

import sys
if __name__ == '__main__':
    greet(sys.argv[1:])

原文:https://stackoverflow.com/questions/39279595/is-debugging-possible-with-python-click-using-intellij

Postman简单使用

设置全局变量 postman.setGlobalVariable("key", string);

取值 var value =  postman.getGlobalVariable("key");

类型转换:var _key = parseInt(key);

POST请求体body中参数拼接 "k":"{{workspacr_id}}"

Url 和参数栏目引用全局环境参数(Cookie设置为环境参数)类似cookie=token

取值:{{cookie}}

得到response的值:var jsonData = JSON.parse(responseBody);

判断response的值:tests["success"] = jsonData.success === true;  括号内的success是需要判断的参数名 可以自己取名,无限制,外面的success是response含有的参数,===表示类型转换后进行比较。

node.js安装、Jenkins执行自动化脚本

npm -v #显示版本,检查npm 是否正确安装。

npm install express #安装express模块

npm install -g express #全局安装express模块

npm list #列出已安装模块

npm show express #显示模块详情

npm update #升级当前目录下的项目的所有模块

npm update express #升级当前目录下的项目的指定模块

npm update -g express #升级全局安装的express模块

npm uninstall express #删除指定的模块

npm install -g newman 安装newman

批量执行postman的case(xx/xx/request)

CMD命令行单个case:

 newman run C:/Users/admin/Desktop/testcase.json  --reporters cli,html,json,junit --reporter-json-export jsonOut.json --reporter-junit-export xmlOut.xml --reporter-html-export htmlOut.html  --timeout-request 5000 --globals C:/Users/admin/Desktop/globals.json

--reporter是报告结果的格式,jsonOut.*是报告结果。 timeout是超时,--globals 是配置全局配置文件

Jenkins执行shell

#!/bin/bash
caseList="case1 case2" #用空格隔开
for case in $caseList;
do newman run test.json  --folder $case --reporters cli,html,json,junit --reporter-json-export report/$case.json --reporter-junit-export report/$case.xml --reporter-html-export report/$case.html  --timeout-request 5000 --globals $1 --bail

done

执行 sh test.sh  test_globals.json

$0表示文件名,$1表示命令行接受的第一个参数

test_globals是配置sh参数,指定全局配置用test_globals.json

一个case是一个colection下的一个文件夹,而不是一个collection。且一个case下很大get post方法,失败必须停止执行这个case。

--bail表示只要case中有一个请求失败,则该case结束,不再执行

--folder表示执行指定collection下的某一个文件夹(“case1”)

遇到使用selenium时提示:ImportError:No module named selenium

命令行执行 python -m pip install -U selenium

元素定位

partialLinkText 一个定位包含给定链接文本的元素的位置

linkText 一个元素,通过它显示的精确文本来定位元素

//div[starts-with(@class,'ql-editor')]";//通用备注输入-富文本框

starts-with(@class,'xxx') 定位class的开头是xxx的元素

@CacheLookup 缓存当前元素定位,如果元素在页面需要多次使用,则不可以使用该缓存注解

@FindBy 查找单个元素,支持xPath,id,等待

@FindBys

 @Findbys({
    @FindBy(className = "A"),
    @FindBy(className = "B")
}) 得到List

取交集,先根据第一个注解获取到对应元素,然后根据第二个注解再筛选出对应的页面元素

@FindAll

@FindAll({
    @FindBy(id = "A"),
    @FindBy(id = "B")
})

得到List

取并集 获取满足第一个或第二个注解的元素

maven构建TestNg,动态改变环境

TestNg传参

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd">
    <suitename="suite">
    <testname="test">
    <parametername="host"value="??"/>
    <parametername="workspace"value="??"/>
<classes>
    <classname="com.qutoutiao.Test"/>
</classes>
</test>
</suite>

使用参数

@Parameters({"host","workspace"})//根据传入的参数,修改域名。默认不传则是测试环境
@BeforeClass
public void method(String host,String workspace){}

maven 命令行动态切换testng-online.xml、testng-test.xml

@BeforeClass:只会在类的@Test启动的时候运行一次,后续不再执行
@BeforeMethod:每次@Test执行前,都会执行一次
@AfterClass:只会在该类的所有@Test执行完,才执行一次

配置pom属性

<properties>

<xmlFile>testng-test.xml</xmlFile><!--设置默认的文件是测试环境-->

</properties>

引用

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <testFailureIgnore>true</testFailureIgnore>
        <suiteXmlFiles>
            <suiteXmlFile>${xmlFile}</suiteXmlFile>
        </suiteXmlFiles>
        <workingDirectory>target/</workingDirectory>
    </configuration>
</plugin>

命令行

线上执行:mvn clean test -DxmlFile=testng-online.xml

测试执行:mvn clean test -DxmlFile=testng-test.xml

 

使用Gradle实现上述切换xml以执行不同case,在build.gradle中增加

task runTest(type: Test) {

    def testsuite = System.getProperty("testsuite")

    def testinclude = System.getProperty("include")

    systemProperty 'configurefailuserpolicy', 'true'

 

    useTestNG() {

        if (null != testsuite) {

            suites testsuite

        }

        configFailurePolicy 'continue'

    }

 

    if(null != testinclude) {

        include System.getProperty("include")

    }

 

    testLogging {

        onOutput { descriptor, event ->

            logger.lifecycle(event.message )

        }

    }

    systemProperties System.getProperties()

    reports.html.enabled = true

    outputs.upToDateWhen { false }

}

执行命令 sh gw clean runTest -Dtestsuite="config/testngsuites/ui_test_cases.xml"

runTest是方法,-D表示传入参数testsuite,=后面是值,config/testngsuites是项目根目录开始的路径

 

坑点:

Selenuim 浏览器驱动版本

无界面UI自动化

Jenkins构建环境变量,文件权限

元素定位失效等问题

必须命令行执行test才有TestNg报告

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值