Jenkins高级篇之Pipeline实践篇-6-Selenium和Jenkins持续集成-pipeline参数化构建selenium自动化测试

本文主要是介绍Jenkins高级篇之Pipeline实践篇-6-Selenium和Jenkins持续集成-pipeline参数化构建selenium自动化测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

       这篇来思考下如何写一个方法,可以修改config.properties文件里面的属性。加入这个方法可以根据key修改value,那么我们就可以通过jenkins上变量,让用户输入,来修改config.properties文件里面的值。例如测试服务器地址和浏览器类型的名称。如果用户在Jenkins界面填写浏览器是chrome,那么我们就修改config.properties里面的browser=chrome,如果用户填写的是firefox,那么我们就在启动selenium测试之前去修改browser=firefox。这样,灵活的控制参数,就达到了我们第一个需求。

1.根据Java的方法

我们知道grooy是可以无缝执行java代码,我也写了一个java的代码,放在pipleline/selenium.groovy文件里,结果我在测试的时候,报了一个错误,这个需要jenkins管理员去approve这个脚本的执行,但是我的jenkins环境上没有这个approve的相关的script,很神奇。

import hudson.model.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;def setKeyValue(key, value, file_path) {Properties prop = new Properties()try {prop.load(new FileInputStream(file_path))}catch (FileNotFoundException e){e.printStackTrace()}catch (IOException e) {e.printStackTrace()}// write into file try {prop.setProperty(key, value)FileOutputStream fos = new FileOutputStream(file_path)prop.store(fos, null)fos.close()}catch (FileNotFoundException e){e.printStackTrace()}catch (IOException e) {e.printStackTrace()}
}return this;

结果测试下,报了一个沙箱错误:

ERROR: Error met:org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.util.Properties

其实,这个类似错误,我在工作中也遇到过,一般来说管理员去点击批准这个脚本执行就可以。我的环境,在升级一个script security相关的插件之后,连脚本批准入口都不显示了,很神奇。只好换一种思路去写代码了。

2.Pipeline自带的readFile和writeFile

我的思路是这样的,先通过readFile方法获取到原来文件的内容,返回是一个字符串对象。然后根据换行符把字符串对象给切片,拿到一个list对象,遍历这个list,用if判断,根据Key找到这行,然后改写这行。由于这我们只是在内存改写,所以需要提前定义一个list对象来把改写和没有改写的都给add到新list,然后定义一个空字符串,遍历新list,每次拼接一个list元素都加上换行符。这样得到字符串就是一个完整内容,然后把这个内容利用writeFile方法写回到原来的config文件。

具体代码是这样的。

selenium_jenkins.groovy文件

import hudson.model.*;pipeline{agent anyparameters {string(name: 'BROWSER_TYPE', defaultValue: 'chrome', description: 'Type a browser type, should be chrome/firefox')string(name: 'TEST_SERVER_URL', defaultValue: '', description: 'Type the test server url')string(name: 'NODE', defaultValue: 'win-anthony-demo', description: 'Please choose a windows node to execute this job.')}stages{stage("Initialization"){steps{script{browser = BROWSER_TYPE?.trim()test_url = TEST_SERVER_URL?.trim()win_node = NODE?.trim()}}}stage("Git Checkout"){steps{script{node(win_node) {checkout([$class: 'GitSCM', branches: [[name: '*/master']],userRemoteConfigs: [[credentialsId: '6f4fa66c-eb02-46dc-a4b3-3a232be5ef6e', url: 'https://github.com/QAAutomationLearn/JavaAutomationFramework.git']]])}}}}stage("Set key value"){steps{script{node(win_node){selenium_test = load env.WORKSPACE + "\\pipeline\\selenium.groovy"config_file = env.WORKSPACE + "\\Configs\\config.properties"try{selenium_test.setKeyValue2("browser", "abc123", config_file)file_content = readFile config_fileprintln file_content}catch (Exception e) {error("Error met:" + e)}}}}}stage("Run Selenium Test"){steps{script{node(win_node){println "Here will start to run selenium test."}}}}}}

selenium.groovy文件

import hudson.model.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;def setKeyValue(key, value, file_path) {Properties prop = new Properties()try {prop.load(new FileInputStream(file_path))}catch (FileNotFoundException e){e.printStackTrace()}catch (IOException e) {e.printStackTrace()}// write into file try {prop.setProperty(key, value)FileOutputStream fos = new FileOutputStream(file_path)prop.store(fos, null)fos.close()}catch (FileNotFoundException e){e.printStackTrace()}catch (IOException e) {e.printStackTrace()}
}def setKeyValue2(key, value, file_path) {// read file, get string objectfile_content_old = readFile file_pathprintln file_content_old//遍历每一行,判断,然后替换字符串lines = file_content_old.tokenize("\n")new_lines = []lines.each { line ->if(line.trim().startsWith(key)) {line = key + "=" + valuenew_lines.add(line)}else {new_lines.add(line)}}// write into filefile_content_new = ""new_lines.each{line ->file_content_new += line + "\n"}writeFile file: file_path, text: file_content_new, encoding: "UTF-8"
}
return this;

这里看第二个方法是我的思路,虽然比较啰嗦,但是实现了这个修改文件的需求。

测试结果看看是否把browser = chrome 改成browser = abc123

[Pipeline] readFile
[Pipeline] echo
baseURL=http://demo.guru99.com/v4/index.php
# https://www.utest.com  backup website
userName=mngr169501
password=sYhYtUd# browser driver path
firefoxpath=./Drivers\\geckodriver.exe
chromepath=./Drivers\\chromedriver.exe# browser instance
# the browser vlaue will only be firefox or chrome here
browser=chrome[Pipeline] writeFile
[Pipeline] readFile
[Pipeline] echo
baseURL=http://demo.guru99.com/v4/index.php
# https://www.utest.com  backup website
userName=mngr169501
password=sYhYtUd# browser driver path
firefoxpath=./Drivers\\geckodriver.exe
chromepath=./Drivers\\chromedriver.exe# browser instance
# the browser vlaue will only be firefox or chrome here
browser=abc123[Pipeline] }
[Pipeline] // node

你如果对日志不放心,你可以去你拉取之后代码路径去看看是否修改了,修改的对不对。

接下来,我把github里面config.properties 中browser的值改成firefox,然后我通过pipeline代码去改成chrome,来跑一个集成测试,看看行不行,测试环境URL就无法改,这个没有第二套同样产品的地址,这个就留个你们自己项目上的测试环境和生产环境,自己试一下。

提交之后,两个文件代码如下

import hudson.model.*;pipeline{agent anyparameters {string(name: 'BROWSER_TYPE', defaultValue: 'chrome', description: 'Type a browser type, should be chrome/firefox')string(name: 'TEST_SERVER_URL', defaultValue: '', description: 'Type the test server url')string(name: 'NODE', defaultValue: 'win-anthony-demo', description: 'Please choose a windows node to execute this job.')}stages{stage("Initialization"){steps{script{browser_type = BROWSER_TYPE?.trim()test_url = TEST_SERVER_URL?.trim()win_node = NODE?.trim()}}}stage("Git Checkout"){steps{script{node(win_node) {checkout([$class: 'GitSCM', branches: [[name: '*/master']],userRemoteConfigs: [[credentialsId: '6f4fa66c-eb02-46dc-a4b3-3a232be5ef6e', url: 'https://github.com/QAAutomationLearn/JavaAutomationFramework.git']]])}}}}stage("Set key value"){steps{script{node(win_node){selenium_test = load env.WORKSPACE + "\\pipeline\\selenium.groovy"config_file = env.WORKSPACE + "\\Configs\\config.properties"try{selenium_test.setKeyValue2("browser", browser_type, config_file)//test_url 你自己替代file_content = readFile config_fileprintln file_content}catch (Exception e) {error("Error met:" + e)}}}}}stage("Run Selenium Test"){steps{script{node(win_node){run_bat = env.WORKSPACE + "\\run.bat"bat (run_bat)}}}}}}
import hudson.model.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;def setKeyValue(key, value, file_path) {// read file, get string objectfile_content_old = readFile file_pathprintln file_content_old//遍历每一行,判断,然后替换字符串lines = file_content_old.tokenize("\n")new_lines = []lines.each { line ->if(line.trim().startsWith(key)) {line = key + "=" + valuenew_lines.add(line)}else {new_lines.add(line)}}// write into filefile_content_new = ""new_lines.each{line ->file_content_new += line + "\n"}writeFile file: file_path, text: file_content_new, encoding: "UTF-8"
}
return this;

测试结果:

http://65.49.216.200:8080/job/selenium-pipeline-demo/23/console

主要日志如下:

[Pipeline] node
Running on win-anthony-demo in C:\JenkinsNode\workspace\selenium-pipeline-demo
[Pipeline] {
[Pipeline] load
[Pipeline] { (C:\JenkinsNode\workspace\selenium-pipeline-demo\pipeline\selenium.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] readFile
[Pipeline] echo
baseURL=http://demo.guru99.com/v4/index.php
# https://www.utest.com  backup website
userName=mngr169501
password=sYhYtUd# browser driver path
firefoxpath=./Drivers\\geckodriver.exe
chromepath=./Drivers\\chromedriver.exe# browser instance
# the browser vlaue will only be firefox or chrome here
browser=chrome[Pipeline] writeFile
[Pipeline] readFile
[Pipeline] echo
baseURL=http://demo.guru99.com/v4/index.php
# https://www.utest.com  backup website
userName=mngr169501
password=sYhYtUd# browser driver path
firefoxpath=./Drivers\\geckodriver.exe
chromepath=./Drivers\\chromedriver.exe# browser instance
# the browser vlaue will only be firefox or chrome here
browser=chrome[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Run Selenium Test)
[Pipeline] script
[Pipeline] {
[Pipeline] node
Running on win-anthony-demo in C:\JenkinsNode\workspace\selenium-pipeline-demo
[Pipeline] {
[Pipeline] bat
[selenium-pipeline-demo] Running batch script
C:\JenkinsNode\workspace\selenium-pipeline-demo>C:\JenkinsNode\workspace\selenium-pipeline-demo\run.batC:\JenkinsNode\workspace\selenium-pipeline-demo>cd C:\Users\Anthont\git\AnthonyWebAutoDemo C:\Users\Anthont\git\AnthonyWebAutoDemo>mvn clean install 
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for AnthonyAutoV10:AnthonyAutoV10:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 19, column 15
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] -------------------< AnthonyAutoV10:AnthonyAutoV10 >--------------------
[INFO] Building AnthonyAutoV10 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for com.beust:jcommander:jar:1.66 is missing, no dependency information available
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ AnthonyAutoV10 ---
[INFO] Deleting C:\Users\Anthont\git\AnthonyWebAutoDemo\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ AnthonyAutoV10 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Anthont\git\AnthonyWebAutoDemo\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ AnthonyAutoV10 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ AnthonyAutoV10 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Anthont\git\AnthonyWebAutoDemo\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ AnthonyAutoV10 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 11 source files to C:\Users\Anthont\git\AnthonyWebAutoDemo\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ AnthonyAutoV10 ---
[INFO] Surefire report directory: C:\Users\Anthont\git\AnthonyWebAutoDemo\target\surefire-reports-------------------------------------------------------T E S T S
-------------------------------------------------------
Running TestSuite
Starting ChromeDriver 2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab) on port 1780
Only local connections are allowed.
Dec 23, 2018 9:52:43 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSSINFO [main] (TC_NewCustomer_004.java:22)- Login is completedINFO [main] (TC_NewCustomer_004.java:28)- Proving customer details.............INFO [main] (TC_NewCustomer_004.java:46)- Validating adding new customer..............
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 166.426 sec - in TestSuiteResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ AnthonyAutoV10 ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: C:\Users\Anthont\git\AnthonyWebAutoDemo\target\AnthonyAutoV10-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ AnthonyAutoV10 ---
[INFO] Installing C:\Users\Anthont\git\AnthonyWebAutoDemo\target\AnthonyAutoV10-0.0.1-SNAPSHOT.jar to C:\Users\Anthont\.m2\repository\AnthonyAutoV10\AnthonyAutoV10\0.0.1-SNAPSHOT\AnthonyAutoV10-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\Anthont\git\AnthonyWebAutoDemo\pom.xml to C:\Users\Anthont\.m2\repository\AnthonyAutoV10\AnthonyAutoV10\0.0.1-SNAPSHOT\AnthonyAutoV10-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:05 min
[INFO] Finished at: 2018-12-23T21:55:24+08:00
[INFO] ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

这里灵活的参数,我们实现了需求。

下一篇文章,我们来思考下如何把测试过程的日志和报告文件给放到Jenkins上,点击链接可以预览。这个我之前也没有做过,先研究下,有知道的同学可以告诉我哈。

 

这篇关于Jenkins高级篇之Pipeline实践篇-6-Selenium和Jenkins持续集成-pipeline参数化构建selenium自动化测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1051017

相关文章

PyTorch高级特性与性能优化方式

《PyTorch高级特性与性能优化方式》:本文主要介绍PyTorch高级特性与性能优化方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、自动化机制1.自动微分机制2.动态计算图二、性能优化1.内存管理2.GPU加速3.多GPU训练三、分布式训练1.分布式数据

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

Spring Boot集成SLF4j从基础到高级实践(最新推荐)

《SpringBoot集成SLF4j从基础到高级实践(最新推荐)》SLF4j(SimpleLoggingFacadeforJava)是一个日志门面(Facade),不是具体的日志实现,这篇文章主要介... 目录一、日志框架概述与SLF4j简介1.1 为什么需要日志框架1.2 主流日志框架对比1.3 SLF4

Spring Boot集成Logback终极指南之从基础到高级配置实战指南

《SpringBoot集成Logback终极指南之从基础到高级配置实战指南》Logback是一个可靠、通用且快速的Java日志框架,作为Log4j的继承者,由Log4j创始人设计,:本文主要介绍... 目录一、Logback简介与Spring Boot集成基础1.1 Logback是什么?1.2 Sprin

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

Linux高并发场景下的网络参数调优实战指南

《Linux高并发场景下的网络参数调优实战指南》在高并发网络服务场景中,Linux内核的默认网络参数往往无法满足需求,导致性能瓶颈、连接超时甚至服务崩溃,本文基于真实案例分析,从参数解读、问题诊断到优... 目录一、问题背景:当并发连接遇上性能瓶颈1.1 案例环境1.2 初始参数分析二、深度诊断:连接状态与

Spring Boot 常用注解详解与使用最佳实践建议

《SpringBoot常用注解详解与使用最佳实践建议》:本文主要介绍SpringBoot常用注解详解与使用最佳实践建议,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、核心启动注解1. @SpringBootApplication2. @EnableAutoConfi

Python+wxPython构建图像编辑器

《Python+wxPython构建图像编辑器》图像编辑应用是学习GUI编程和图像处理的绝佳项目,本教程中,我们将使用wxPython,一个跨平台的PythonGUI工具包,构建一个简单的... 目录引言环境设置创建主窗口加载和显示图像实现绘制工具矩形绘制箭头绘制文字绘制临时绘制处理缩放和旋转缩放旋转保存编

MySQL复合查询从基础到多表关联与高级技巧全解析

《MySQL复合查询从基础到多表关联与高级技巧全解析》本文主要讲解了在MySQL中的复合查询,下面是关于本文章所需要数据的建表语句,感兴趣的朋友跟随小编一起看看吧... 目录前言:1.基本查询回顾:1.1.查询工资高于500或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J1.2.按照部门

史上最全nginx详细参数配置

《史上最全nginx详细参数配置》Nginx是一个轻量级高性能的HTTP和反向代理服务器,同时也是一个通用代理服务器(TCP/UDP/IMAP/POP3/SMTP),最初由俄罗斯人IgorSyso... 目录基本命令默认配置搭建站点根据文件类型设置过期时间禁止文件缓存防盗链静态文件压缩指定定错误页面跨域问题