サイプレスのAPIテスト.IO


サイプレスとは何か
サイプレスは、現代のWebのために構築された次世代フロントエンドのテストツールです.Cypress.ioとその機能について学んでください.
事前の要件
  • ノードをインストールします.JSとNPM https://www.npmjs.com/get-npm
  • あなたが
  • を使いたいAPI
    セットアップ
  • プロジェクトとCDのディレクトリを作成します.
  • は、新しいNPMパッケージプロジェクトを設定するためにmkdir cypress-api-automation-tests && cd cypress-api-automation-testsを実行します.
  • はNPM npm init --yを介してサイプレスをインストールする.
  • は、npm i cypress --save-devを実行することによってサイブレスを確認します.
  • 現在、サイプレスと一緒のサイプレスフォルダ.JSONファイルはプロジェクトディレクトリに作成されます.
  • “統合”フォルダには、サイプレステストの例が含まれます.
  • “cypress . json”ファイルを編集して、すべてのテストのためのbaseURLを追加します
  • {
    "baseUrl": "https://jsonplaceholder.typicode.com/"
    }
    
    テストの作成と実行
    「統合」フォルダの下の
  • は新しいファイルを作成します."typicode api test . js "という名前を付けます
  • /// <reference types="cypress" />
    
    describe('JSON Typicode', () => {
        it('Get all user posts', () => {
           cy.request('/posts')
           .then((response) => {
               expect(response.status).to.equal(200);
            })
         })
     })
    
    2 . cypressでテストを実行し、結果を通知します.

    応答中に返される他のいくつかのオブジェクトをアサートし、正しく動作しているか確認してください.
    アサーション
    レスポンスオブジェクトの使用可能なキーをチェックします.
    {
        "userId": 1,
        "id": 1,
        "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
        "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
      }
    
    userid、id、titleおよびbodyキーがResponseオブジェクトに存在するかどうかを確認する必要があります.
    /// <reference types="cypress" />
    
    describe('JSON Typicode', () => {
        it('Get all user posts', () => {
            cy.request('/posts')
                .then((response) => {
                    let first_response_object = Object.keys(response.body[0]);
                    let keys = [`userId`, `id`, `title`, `body`];
                    for (let key of keys) {
                        expect(first_response_object).to.includes(key)
                    }
                })
        })
    })
    
    結果は

    ノート
  • オブジェクト.キー( object )は利用可能なキーの配列を返します.
  • とキー配列を反復処理し、includeメソッドでアサートします.
  • 新しいユーザー投稿を投稿します.
    リクエスト本文は次のようになります
    {
                "userId": 1,
                "id": 1,
                "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
                "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
              }
    

    無視されたテストとして追加することで例題フォルダを無視します.JSON
    {
        "baseUrl": "https://jsonplaceholder.typicode.com/",
        "ignoreTestFiles": "**/examples/*.js"
    }
    
    GITレポ:https://github.com/Bharath-Kumar-S/cypress-api-automation-tests
    これが役に立つことを願っています.あなたのフィードバックを残してください.