当前位置: 首页 > 工具软件 > CodeceptJS > 使用案例 >

codeceptjs集成Jenkins实例进击

公孙智
2023-12-01

codeceptjs作为web UI自动化测试框架,可以和多种工具集成,比如webdriverio,puppeteer...
该实例用codeceptjs && webdriverio,除了典型的pageObject操作和元素分离,着重讲下codeceptjs中一些特殊场景用到的知识

1. DataTable -- 当相同场景使用不同数据集测试时,使用datatable非常便利,如demo中用到的

let appInfo = new DataTable(['appProduct', 'appBrand', 'appRegion']);
appInfo.add([productAndBrand.products.IP, productAndBrand.brands.SUN, productAndBrand.regions.UAT]);

appInfo.add([productAndBrand.products.IP, productAndBrand.brands.AAMI, productAndBrand.regions.UAT]);

 

Feature('IP - finish a purchase by happy path for brands');
Data(appInfo).Scenario('create a quote and purchase @IP', (I, current) => {
    I.landOnPage(current.appProduct, current.appBrand, current.appRegion);
    ......
    });

2. executeScript -- 当在codeceptjs中使用原生JavaScript时使用,如demo中用到的

//这里一个元素点击了两次才选择上,不太清楚原因,有可能是该web产品使用的架构导致的
 I.executeScript(function (questionBtn) {
            $(questionBtn).click();
            $(questionBtn).click();
        }, questionBtn);


3. 注册--当在test中用到I调用自写函数时,该函数需要在steps_file文件中注册,同时也需要在config文件中引入
如demo中 steps_file文件中
 amOnAboutYouPage: aboutYouPage,

codeceptjs.conf.js文件中
 include: {
        I: './steps_file.js',
        aboutYou: './pageHandle/aboutYouPageHandle.js',
        ....
        }

4. 在Jenkins上run时,要特别注意 -- 运行codeceptjs时需要先启动selenium-standalone,再启动run test的命令;但是一般情况下,在Jenkins上运行时都是前一个命令运行结束了,下一个才能继续执行;所以当两个命令都存活时,selenium-standalone在后台执行。如demo中在windows sever上运行

call START /B selenium-standalone start
//等待5s时间,需要selenium-standalone启动后,再run test
waitfor SomethingThatIsNeverHappening /t 5 2>NUL
//直接用npm run,因为在config文件中配置了具体run命令
call npm run test:all

 5. 同时在Jenkins上运行时,需要捕捉test passed 或者failed, 通过Jenkins的 stage显示,但又不想遇到failed 的就全部停止run,所以可以先记录failed标志,最后再对Jenkins标识failed。如demo中

set failure=0
call npm run test:all
if %errorlevel% NEQ 0 (
  set failure=%errorlevel%
)
if %failure% neq 0 (
exit /b %failure%
)

6.用selenium-standone运行时,会有很多没什么用的info信息(server发出的command),在Jenkins的console里如果要看error信息时,就会有很多冗余信息,因此我们想屏蔽掉这部分info信息。在selenium-standalone的lib文件夹下引入jdk环境的logging.properties文件并修改log类型在WARNING或以上,同时在start文件中修改javaArgs:指向-Djava.util.logging.config.file=logging.properties


7.有时候运行用例时会提示warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.。。。。
原因:同一个事件添加多个Listener时,超过10次(nodejs中EventEmitter默认为10),触发条件有几种:https://mcculloughwebservices.com/2016/10/30/possible-eventemitter-memory-leak-detected/(都是有event emitter多次https://jonathanong.github.io/understanding-possible-eventemitter-leaks.html
解决:
const emitter = new EventEmitter()

【在step_file的 bootstrap中】设置
emitter.setMaxListeners(25)
// or 0 to turn off the limit
emitter.setMaxListeners(0)

  
更换框架codeceptjs+webdriverio为codeceptjs+puppeteer
优点:1.puppeteer自带chrome/chromium,不依赖于运行机器上的浏览器,也就是说无论在Windows上还是Linux上都不用安装相关浏览器,当然也可以配置用运行机器上的浏览器。
     2.用puppeteer运行时可以不用提前启动webdriver之类的服务,一个命令就可以运行用例了。
缺点:1.puppeteer目前只绑定了chrome系列相关的浏览器,不能用其他浏览器运行,比如IE。。
     2.从webdriverio更换为puppeteer后,不能很好识别原用的一些等待方法,如waitForText,waitforVisible。。。

 Note: puppeteer在jenkins上run时,如果遇到只能启动chrome,但不能自动输入链接运行时,就需要检查下chrome的配置
如果想使chrome show时,最好使用——
chrome:{
   headless:false,
   args:[‘—no-sandbox’, ‘—disable-setuid-sandbox’]
}
如果不想chrome show时,可以这样配置—
chrome:{
headless:true
}
或者 show:false
 

demo实例:https://download.csdn.net/download/u010675455/10509949,其中删除了一些对整个框架没影响的文件和用例。

 

 类似资料: