Jenkins高级篇之Pipeline实践篇-5-Selenium和Jenkins持续集成-Pipelinejob草稿版

本文主要是介绍Jenkins高级篇之Pipeline实践篇-5-Selenium和Jenkins持续集成-Pipelinejob草稿版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

       前面,我们通过在jenkins上创建一个free style job,然后跑起来我们的selenium自动化测试。现在开始,我们要把这种方式给丢弃,采用pipeline的方式,让更多手工配置操作,写入到代码中去。一开始,我们从简单做起,慢慢来。这篇文章的目标就是,创建并成功运行一个pipeline job,先不考虑灵活的变量,我们只考虑如何在pipeline代码中去执行run.bat文件,从而把selenium自动化测试给跑起来。

1.项目结构修改

为了支持pipeline,我做了两件事。第一件事情,把jenkins-core-xxx.jar包添加到当前项目的build path里。我本来在maven pom.xml文件写Jenkins core的依赖配置,但是发现报错,也许是我环境问题,暂时就采用这样本地依赖。因为在pipeline代码中,我们将来在发送邮件的时候需要用到jenkins环境的信息,所以需要导入这个jar包。

第二件事情就是,添加一个pipeline的文件夹,里面第一个文件selenium_jenkins.groovy是一个写多个stage的文件,第二个文件selenium.groovy是一个模块方法,我本来把java修改config.properties文件里面的属性值的方法写入到这里,遇到了一个错误,还没有解决。这篇不介绍这个文件和文件内容。

下面代码是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("Run Selenium Test"){steps{script{node(win_node){run_bat = env.WORKSPACE + "\\run.bat"bat (run_bat)}}}}}}

上面三个stage,第一个初始化这个暂时不介绍,后面文章我需要用到,就是把测试地址url和浏览器类型,是firefox还是chrom,这个我需要做参数化处理,这篇文章不涉及,可以不管,留着后面用。第二个stage是在windows的机器上拉取代码,因为你不能保证,这台windows机器上一定有你最新的github项目的代码。这里提醒一下,这个windows node,需要你自己配置,如何配置,参考我文章。这个机器需要安装groovy,java.maven等环境,并设置环境变量。第三个stage,是我们这篇重点实现过程。定义你run.bat在你windows机器哪个位置,需要交代清楚。第二行代码就是执行一个bat命令,这里复习下,如果执行linux shell命令,这个地方命令把bat改成sh就可以。

2.在jenkins上创建一个pipeline的job

我Jenkins环境有一个例子可以参考:http://65.49.216.200:8080/job/selenium-pipeline-demo

重点介绍下,git和pipeline入口文件设置

3.运行测试

应该来说,每个人都不可能一次就运行成功,需要根据你运行报错,调整代码,修复问题。这里我有一个成功运行的例子:

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

这里还是贴一下日志内容:

Started by user admin
Replayed #14
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/selenium-pipeline-demo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository> git config remote.origin.url https://github.com/QAAutomationLearn/JavaAutomationFramework.git # timeout=10
Fetching upstream changes from https://github.com/QAAutomationLearn/JavaAutomationFramework.git> git --version # timeout=10
using GIT_ASKPASS to set credentials > git fetch --tags --progress https://github.com/QAAutomationLearn/JavaAutomationFramework.git +refs/heads/*:refs/remotes/origin/*> git rev-parse refs/remotes/origin/master^{commit} # timeout=10> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 8469a08f0ef6aaab86b706c4459fd122d3787292 (refs/remotes/origin/master)> git config core.sparsecheckout # timeout=10> git checkout -f 8469a08f0ef6aaab86b706c4459fd122d3787292
Commit message: "remove $ on env.workspace"> git rev-list --no-walk 8469a08f0ef6aaab86b706c4459fd122d3787292 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Initialization)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Git Checkout)
[Pipeline] script
[Pipeline] {
[Pipeline] node
Running on win-anthony-demo in C:\JenkinsNode\workspace\selenium-pipeline-demo
[Pipeline] {
[Pipeline] checkout> git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository> git config remote.origin.url https://github.com/QAAutomationLearn/JavaAutomationFramework.git # timeout=10
Fetching upstream changes from https://github.com/QAAutomationLearn/JavaAutomationFramework.git> git --version # timeout=10
using GIT_ASKPASS to set credentials > git fetch --tags --progress https://github.com/QAAutomationLearn/JavaAutomationFramework.git +refs/heads/*:refs/remotes/origin/*> git rev-parse "refs/remotes/origin/master^{commit}" # timeout=10> git rev-parse "refs/remotes/origin/origin/master^{commit}" # timeout=10
Checking out Revision 8469a08f0ef6aaab86b706c4459fd122d3787292 (refs/remotes/origin/master)> git config core.sparsecheckout # timeout=10> git checkout -f 8469a08f0ef6aaab86b706c4459fd122d3787292
Commit message: "remove $ on env.workspace"
[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 scriptC:\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 ]---------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/com/beust/jcommander/1.66/jcommander-1.66.pom
[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 29290
Only local connections are allowed.
Dec 23, 2018 5:26:20 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: 128.325 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:  02:24 min
[INFO] Finished at: 2018-12-23T17:28:20+08:00
[INFO] ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

这篇就介绍到这个,这个是pipeline方式运行selenium自动化的基础,后面我们根据需求,慢慢来优化和增加新代码。

这篇关于Jenkins高级篇之Pipeline实践篇-5-Selenium和Jenkins持续集成-Pipelinejob草稿版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

ShardingProxy读写分离之原理、配置与实践过程

《ShardingProxy读写分离之原理、配置与实践过程》ShardingProxy是ApacheShardingSphere的数据库中间件,通过三层架构实现读写分离,解决高并发场景下数据库性能瓶... 目录一、ShardingProxy技术定位与读写分离核心价值1.1 技术定位1.2 读写分离核心价值二

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分