Cypress.ioを使用してアプリケーションをエンドツーエンドでテストする方法

https://unsplash.com/@kellysikkemaからの画像
https://unsplash.com/@kellysikkemaからの画像

この記事では、次のことを学びます。





  • サイプレスとは何ですか、いつ使用するか





  • サイプレスを使用したテストの基本





  • 高度なサイプレスコマンド





  • ユーザーインターフェイス要素との相互作用





  • サイプレスを使用したベストプラクティス






前書き

アプリケーションをテストするには、次の手順を実行する必要があります。





  • アプリケーションを起動します





  • サーバーが起動するのを待ちます





  • ( , )





  • , ( , ..)









. , .





Cypress. Cypress , , :





  • ( , ..)













!  Cypress .  , , , .





, — , . Cypress .





, Cypress, .






Cypress

, :





プロジェクトの初期化

, Cypress:





サイプレスのインストール
Cypress

 . Linux,   ,  Cypress NPM.





, Cypress , :





サイプレスのオープニング
Cypress

(Test Runner):





テストランナーインターフェース
Test Runner

.






Cypress

Cypress ,   cypress/integration



.  :





ヒノキ/統合に移動します
cypress/integration

JavaScript  basicTest.js



:





JavaScriptファイルの作成
JavaScript

Cypress, Test Runner :





リアルタイムのファイル構造の更新

.





/cypress/integration/basicTest.js



:





basicTest.jsファイルのコード
basicTest.js
  • 1:  describe



    Cypress .





  • 2:  it



    , .





  • 3: .  , 2 + 2 4.  false



    , .





,  basicTest.js



Cypress.





TestRunnerでbasicTest.jsをクリックする
basicTest.js Test Runner

:





テスト実行結果

! , .





, ?   /cypress/integration/basicTest.js



   describe



:





basicTest.jsに追加するコード
basicTest.js
  • 2: 4 5 10, . , .





.  :





2回目のテストの結果

, .  , .





basicTest.js



  :





basicTest.jsに追加するコード
basicTest.js
  • 2: 5 5   100, .





:





テスト結果:成功しました!
: !

!  .  expect



  BDD (behavior-driven) .  , (test-driven assertions).





 /cypress/integration/basicTest.js



:





(test-driven assertions) assert

TDD  assert



.





 basicTest.js



:





  • 2:  name



      age



    .





  • 6:  isObject



    person



    .   true



    ,  value is object



    .  , .





  • 10: ,  name



      .





  • 14: ,  name



      .





.  :





テストを実行した結果

!  .  Cypress.





 basicTest.js



:





-

 Demoblaze , , .





 /cypress/integration/



 basicCommandsTest.js



.  :





basicCommandsTest.jsのコード
basicCommandsTest.js
  • 3:  visit



    , Cypress - Demoblaze.





 basicCommandsTest.js



Test Runner:





TestRunnerでbasicCommandsTest.jsをクリックします
basicCommandsTest.js Test Runner

:





!  .  Cypress.





 basicCommandsTest.js



:






Cypress:

. , , HTML Cypress.





Cypress   JQuery  -.





, myButton



id



, :





IDを介して要素を取得する
id

, myButton



, :





クラス名を介して要素を取得する

.





  The-Internet  .  -   / .





« ».





テストページ

DevTools, ,  button



 onclick



,  addElement()



.





DeveloperToolsのスクリーンショット
DeveloperTools

  :





アイテムの識別

 /cypress/integration



 runningClickCommand.js



.  :





  • 2: -.





  • 6: button



    .





. :





結果の出力

, !  , ,  Add Element



.





.





  The-Internet’s login.  .





テスト用のサイトのスクリーンショット
DeveloperToolsのスクリーンショット
DeveloperTools
DeveloperToolsのスクリーンショット
DeveloperTools

username



 id



 username



,  password



 id



 password



.  , Login



 type



 submit



.  ,  username



 password



,   JQuery id



:





IDによる要素の識別
id

,  button



,    , :





 /cypress/integration



 runningTypeCommand.js



.  :





  • 3: .





  • 6:  username



     type



      ,  tomsmith



    .





  • 7:  password



     SuperSecretPassword



    .





  • 10: «».





.  :





コード実行結果の出力

!  .





  The-Internet .





DevTools:





開発者ツール
Developer Tools

 type



  checkbox



.  ,   form



 id



 checkboxes



.    JQuery -:





チェックボックスを特定する

 /cypress/integration/



 runningCheckCommand.js



:





  • 4: ,  check



    , .





  • 7: Cypress .





  • 8: .   uncheck



    , .





. :





テスト実行結果

!  .  Cypress.





.  , HTML, ,  ul



 li



.





 should



.     — The-Internet’s Add Element





テストされたページのスクリーンショット
開発者ツール
Developer Tools

 Delete



 added-manually



.   button



:





私たちの承認

:





アイテムを受け取り、承認する

, :





私たちの承認

:





アイテムを受け取り、承認する

 /cypress/integration/runningClickCommand.js



:





runningClickCommand.jsのコード
runningClickCommand.js
  • 1:  added-manually



    .  , (have.length)



      .





  • 3:  Add Element



    , , (have.text)



       Add Element



    .





.  :





打ち上げ結果

!  .   each



.





 cypress/integration/runningClickCommand.js



:





each

   The-Internet’s Add Elements page:





テストサイトのスクリーンショット

,  Delete



.  ;  ,  Delete



  . , .





-  each



. , .   Delete



 click



.





Developer Tools:





 Delete



 class



 added-manually



.       each



, :





要素を取得してそれぞれを使用する
each

 /cypress/integration/runningClickCommand.js



:





runningClickCommand.jsのコード
runningClickCommand.js
  • 2: ,  .added-manually



    .   $el



    .





  • 3: , Cypress.  .





:





コード実行の結果

!  , .





:





:





  • 2: , Cypress,  Add Element



    20 .





.  :





コード実行の結果

, - . !





, Cypress.





 cypress/integration/runningClickCommand.js



:






, .  :





最良の構造ではありません

Test Runner :





テストランナーでのテスト構造の表示
Test Runner

Cypress , .  , :





良好なプロジェクト構造

, :





:





サンプルコード

,     get



 type



.    ,  .





 cypress/support/commands.js



:





  • 1: ,  identifier



     data



    .





  • 2: , .





 .  /cypress/support/commands.js



.





:





サンプルコード

, .





«»

:





HTML  id



 first



.





Cypress .  , , :





 and



 should



.





Cypress

exec



  .  .





 localhost



, ,    Cypress.





ターミナルコマンド

GitHub

GitHub





  • Cypress Tutorial Videos





  • End-To-End Testing With Cypress by codedamn





  • Real Confidence With Cypress





  • Best practices with Cypress: AssertJS Conference






テストは、アプリケーションが正しく機能することを保証するため、開発プロセスの重要なステップです。テストの作成にはかなりの時間とエネルギーがかかるため、一部のプログラマーはプログラムを手動でテストすることを好みます。幸い、サイプレスは、開発者が短い時間枠でテストを作成できるようにすることで、この問題を解決しました。





最後まで頑張ってくれてありがとう!混乱を感じた場合は、コードを試して例を確認することをお勧めします。








All Articles