Bernice1357689
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Cypress ###### tags: `實習` > version 10.3.1 [TOC] ## 安裝 > [官方文件](https://docs.cypress.io/guides/getting-started/installing-cypress#What-you-ll-learn) ```javascript $ npm install cypress --save-dev ``` 安裝後就能看到 cypress 目錄了 ![](https://i.imgur.com/InhDvfM.png =30%x) ## 終端機執行測試 > [官方文件](https://docs.cypress.io/guides/references/configuration#Intelligent-Code-Completion) ```= cd 2021Intern npx cypress run --config-file "cypress.config.js" ``` - 測試檔案的執行順序在`cypress.config.js`的 specPattern 中 ```js= specPattern: [ "cypress/e2e/系統/帳號管理/帳號管理.cy.js", "cypress/e2e/IP資產管理者功能/網段管理.cy.js", "cypress/e2e/IP資產使用者功能/IP配發申請.cy.js", "cypress/e2e/IP資產使用者功能/IP配發許可.cy.js", "cypress/e2e/IP資產查詢/IP資產查詢.cy.js" ] ``` :::info specPattern 的內容會影響到執行`npx cypress open`後,瀏覽器上呈現的檔案內容 ::: - 執行過程 ![](https://i.imgur.com/mSz4653.png) --- ## 開啟測試執行環境 ``` % npx cypress open ``` - 之後會出現下面的視窗,有 E2E Testing 和 Component Testing 兩種測試方法 - E2E Testing:模擬使用者使用網站 - Component Testing:檢查程式碼中元件的問題 ![](https://i.imgur.com/iOR29xu.png =60%x) - 再點選其中一個瀏覽器就能打開 Cypress 的測試環境了 ![](https://i.imgur.com/Qqa6H6w.png =60%x) - 進入瀏覽器後可以看到 cypress 中所有的測試檔案 ![](https://i.imgur.com/22VdQA0.png) - 點選測試檔進去後就會開始執行測試,右邊的畫面可以看到實際測試網頁的過程 ![](https://i.imgur.com/BWVG5Qv.png) --- ## 目錄架構 :::info 整個架構都可以在外面的編輯器撰寫,像是 VScode 如果在寫測試檔案時,發現 Cypress 和 cy 沒有被定義而報錯,這不會影響測試執行 ::: - ### cypress/support:寫自定義方法,類似函式 - e2e.js:support 目錄的 root - 目前寫好的函式結構: - commands.js:Login/Logout - formActions.js:對表單會使用到的基本操作 - baseAction.js:對基本動作(ex:click,type...)加上 {force:true} - 操作方法 ```javascript= //In /support Cypress.Commands.add('Login', (data) => { cy.visit('http://localhost:8080') cy.get('input[placeholder="請填入帳號"]') .type(data.account) cy.get('input[placeholder="請填入密碼"]') .type(data.password) cy.get('button') .contains('Login') .click() }); //In /e2e cy.Login({account:'admin', password:'admin123!'}) ``` - ### cypress/fixtures:存放測試資料,json 格式 > 和其對應的函式: [cy.fixture()](#cyfixture‘’-引用-fixtures-中的資料) [fixtures 測資翻譯對照表](/5QRzIvGiSiurV9NZykJlmQ) - ### cypress/e2e:存放測試腳本 > 檔案格式都是 `.cy.js` 結尾 --- ## 腳本基本架構 ```javascript= // 父目錄名稱 let prev = '' // 子分頁名稱 let title = '' it('', ()=>{ // 要到哪個分頁 cy.GoToPage(title) //如果一個腳本有兩個測試檔,就會有兩個 fixture //cy.fixture() 會去抓 cypress/fixtures 相對應的測資檔案 cy.fixture(prev+title).then((data) => { describe('', () => { ... }) describe('', () => { context('', () => { }) }) }) }) ``` 其中第 10 行的 data,就是前面 fixture 抓到的檔案 - `it()`: 撰寫測試內容的地方,會把瀏覽器資料清空 - 測試以外的內容 | 比較 | before | beforeEach | afterEach | after | |:--------:| :------: | :----------: |:-----:| :---------: | | 執行順序 | 1 | 2 | 3 | 4 | | 執行時間 | `it()`前 | `it()`前 | `it()`後 | `it()`後 | | 執行次數 | 一次 | `it()`次數決定跑幾次 | `it()`次數決定跑幾次 | 一次 | - `describe()`和`context()` 功能是一樣的,裡面可以包很多小測試檔,可視為一個測試的群組,這兩個要放哪都可以 --- ## 指令 > [官方文件](https://docs.cypress.io/api/commands/and#Syntax) > 每句指令前面都會加上"cy." - 每行指令(cy.開頭)抓取的元素都是獨立的,寫到下一行就會重新抓取元素 ```json= // 如果想在 input 中輸入 admin // Correct cy.get('input[placeholder="請填入帳號"]').type("admin") // Wrong cy.get('input[placeholder="請填入帳號"]') cy.type("admin") ``` --- ## Cypress.Commands.add > [官方文件](https://docs.cypress.io/api/cypress-api/custom-commands#Syntax) - 各測試目錄底下的 commands.js:只有該測試目錄抓得到 - cypress/support/actions:這種的所有測試腳本都抓得到 --- ## 遇到的問題 :::danger 主控台只要一出現錯誤訊息,Cypress 就會視為測試失敗 ::: - 解決方法: 想要跳過錯誤訊息,可以在 describe() 中加入以下程式碼,就能無視錯誤訊息了 ```javascript= // cypress/support/e2e.js Cypress.on('uncaught:exception', (err, runnable) => { return false }) ``` ![](https://i.imgur.com/PcMjZvL.png =60%x) :::danger Dropdown 選到的選項失蹤或是跳到別的欄位去 ::: - 問題: 有可能是其他欄位的選項或是標籤和現在要選的目標是一樣的,可能要避開 1. 標籤名稱一樣:目前的做法是另外寫個函式直接指定哪個欄位(command.js) 2. 選項一樣:選別的選項 :::danger 本來應該選到表單欄位,腳本卻選到關鍵字那邊的欄位 ::: - 問題: 現在抓取表單欄位的方法: 1. 利用`.el-form-item__label`元素的文字內容(表單欄位標籤)選取元素 2. 往上一層`.el-form-item` 3. 再往下一層`.el-form-item__content` 但因為關鍵字欄位本身也是表單,也具有上面這些元素,而且腳本會優先選到關鍵字那邊的欄位,所以需要寫另外的函式直接抓到表單欄位 ![](https://i.imgur.com/SlkwJHB.png) - 解決方法: 另外寫個函式直接指定哪個欄位(command.js) :::danger 在跑`npx cypress run --config-file "cypress.config.js"`的時候,有的檔案被跳過 ::: - 解決方法:代表檔案路徑有錯 --- ## cypress/support/actions:自定義函式 [連結](/fuK7j_D5RWCb6-hZ8j7x2Q)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully