CodeceptJS学习笔记-入门01

 

1.在常用的工作目录新建一个文件夹(eg:codeceptdemo),打开控制台cd到该目录

2.请首先初始化npm

PS E:Docoument> cd codeceptdemo
PS E:Docoumentcodeceptdemo> npm init -y
Wrote to E:Docoumentcodeceptdemopackage.json:
 
{
  "name": "codeceptdemo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
 
 
PS E:Docoumentcodeceptdemo>

2.使用Puppeteer安装CodeceptJS,如果安装特别慢,可以尝试执行(npm config set registry https://registry.npm.taobao.org

npm install codeceptjs puppeteer --save-dev
PS E:Docoumentcodeceptdemo> npm install codeceptjs puppeteer --save-dev
npm WARN deprecated mkdirp@0.5.4: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
 
> puppeteer@3.3.0 install E:Docoumentcodeceptdemo ode_modulespuppeteer
> node install.js
 
Downloading Chromium r756035 - 144.6 Mb [====================] 100% 0.0s
Chromium (756035) downloaded to E:Docoumentcodeceptdemo ode_modulespuppeteer.local-chromiumwin64-756035
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN ws@7.3.0 requires a peer of bufferutil@^4.0.1 but none is installed. You must install peer dependencies yourself.
npm WARN ws@7.3.0 requires a peer of utf-8-validate@^5.0.2 but none is installed. You must install peer dependencies yourself.
npm WARN codeceptdemo@1.0.0 No description
npm WARN codeceptdemo@1.0.0 No repository field.
 
+ codeceptjs@2.6.5
+ puppeteer@3.3.0
added 339 packages from 739 contributors in 203.098s
 
19 packages are looking for funding
  run `npm fund` for details
 
PS E:Docoumentcodeceptdemo>

3.在当前目录中初始化CodeceptJS (use node node_modules/.bin/codeceptjs if you have issues with npx)

npx codeceptjs init

3.1 执行这个命令之后,第一个提示

? Where are your tests located? (./*_test.js)


这个提示是设置名称是以_test.js结尾的都会被当成测试用例执行,也可以自己定义成其他的

3.2第二个提示

? What helpers do you want to use? (Use arrow keys)
> WebDriver
  Puppeteer
  TestCafe
  Protractor
  Nightmare
  Appium
  Playwright

这个需要按上下按键选择,这里我选的是Puppeteer

3.3第三个提示

? Where should logs, screenshots, and reports to be stored? (./output)

这个是日志、屏幕截图和报告存放的目录,也可以自定义,这里我就用默认output直接回车

3.4 第四个提示,选择语言

? Do you want localization for tests? (See https://codecept.io/translation/) (Use arrow keys)
> English (no localization)
  pt-BR
  ru-RU
  it-IT
  pl-PL
  zh-CN
  zh-TW
(Move up and down to reveal more choices)

我选择zh-CN

3.5 第五个提示

? [Puppeteer] Base url of site to be tested (http://localhost)

输入我们需要测试的网址

3.6 第六个提示

? [Puppeteer] Show browser window (Y/n)

这个是设置我们的浏览器是正常模式还是无头模式

3.7 第七个提示

? [Puppeteer] Browser viewport size (1200x900)

设置浏览器大小,根据需要设置,我写的是1920x1080

3.8 第八个提示

? Feature which is being tested (ex: account, login, etc)

这个主要是用来说明我们要测试的功能,也就是测试用例的标题,可以随便起个名字,我写的github

3.9第九个提示

? Feature which is being tested (ex: account, login, etc) github
? Filename of a test (github_test.js)

这里就是测试用例的文件名,默认是Feature的名字加上_test.js。然后新建就成功了

4. 在vscode中打开,大多数都能直接在终端用命令打开

PS E:Docoumentcodeceptdemo> code .
PS E:Docoumentcodeceptdemo>

5. 在github_test.js编写测试用例

Feature('github');
 
 
Scenario('test something', (I) => {
    //在浏览器打开页面
    
I.amOnPage('https://github.com');
  I.see('GitHub');
});

6.输入执行命令,就可以看到执行结果

(base) E:Docoumentcodeceptdemo>npx codeceptjs run --steps
CodeceptJS v2.6.5
Using test root "E:Docoumentcodeceptdemo"
 
loginaccount --
  test something
    
     I am on page "https://github.com"
     I see "GitHub"
  √ OK in 4061ms
 
 
  OK  | 1 passed   // 5s
 
(base) E:Docoumentcodeceptdemo>
原文地址:https://www.cnblogs.com/7047-zfy/p/13187575.html