CodeceptJS學習筆記-入門01


 

1.在常用的工作目錄新建一個文件夾(eg:codeceptdemo),打開控制台cd到該目錄

2.請首先初始化npm

PS E:\Docoument> cd codeceptdemo
PS E:\Docoument\codeceptdemo> npm init -y
Wrote to E:\Docoument\codeceptdemo\ package .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:\Docoument\codeceptdemo>

2.使用Puppeteer安裝CodeceptJS,如果安裝特別慢,可以嘗試執行(npm config set registry https://registry.npm.taobao.org

npm install codeceptjs puppeteer --save-dev
PS E:\Docoument\codeceptdemo> 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:\Docoument\codeceptdemo\node_modules\puppeteer
> node install.js
 
Downloading Chromium r756035 - 144.6  Mb [====================] 100 % 0 .0s
Chromium ( 756035 ) downloaded to E:\Docoument\codeceptdemo\node_modules\puppeteer\.local-chromium\win64- 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:\Docoument\codeceptdemo>

 

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:\Docoument\codeceptdemo> code .
PS E:\Docoument\codeceptdemo>

5. 在github_test.js編寫測試用例

Feature( 'github ' );
 
 
Scenario( 'test something' , (I) => {
     //在瀏覽器打開頁面
    
I.amOnPage('https://github.com'); I.see('GitHub');
});

6.輸入執行命令,就可以看到執行結果

(base) E:\Docoument\codeceptdemo>npx codeceptjs run --steps
CodeceptJS v2. 6.5
Using test root "E:\Docoument\codeceptdemo"
 
loginaccount --
   test something
    
     I am on page "https://github.com" I see "GitHub"
   √ OK in 4061ms
 
 
   OK  | 1  passed   // 5s
 
(base) E:\Docoument\codeceptdemo>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM