เขียน E2E Testing บน AngularJS ด้วย Protractor
บทความนี้ไม่ใช่ Tutorial นะครับ เป็นเพียงแค่การจดบันทึกของผมหลังจากที่เพิ่งหัดลองเขียน E2E Test บน AngularJS เผื่อจะมีประโยชน์แก่ผู้ที่กำลังสนใจในหัวข้อเดียวกัน สำหรับบทความนี้ผมก็อ่านมาจากหลายๆแหล่ง ยำๆมั่วๆ เท่าที่จะนึกออก ก็ประมาณด้านล่างนี้
- Protractor Tutorial
- Practical End-to-End Testing with Protractor
- Advanced Testing and Debugging in AngularJS
- Getting Started With Protractor
- Angular JS–Part 14, End to end tests
Step 1 : Create Project
เริ่มต้นผมทำการสร้างโปรเจ็ค Angular โดยใช้ Bower ทำการสร้างโฟลเดอร์ชื่อ protractor และสร้างไฟล์ bower.json ดังนี้
mkdir protractorcd protractortouch bower.jsonภายในไฟล์ bower.json กำหนดชื่อและเวอร์ชัน รวมถึง dependencies ต่างๆ ดังนี้
{ "name": "try-protractor", "version": "1.0.0", "ignore": [ "**/.*", "node_modules", "bower_components" ], "dependencies": { "angular": "~1.3.15" }}ทำการโหลด AngularJS ด้วยคำสั่ง
bower install angularหลังจากนั้นระบบจะโหลด dependencies มาไว้ที๋โฟลเดอร์ bower_components ภายในโปรเจ็ค
Step 2 : Create Index Page
ทำการสร้างไฟล์ index.html ขึ้นมา แบบง่ายๆ โดยตั้งชื่อ ng-app เป็น myApp และใช้ ng-controller ชื่อ MainController
ผมใช้ Controller As Syntax อ้างอิงจาก
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" /> <title>Try Protractor</title> </head> <body ng-app="myApp"> <div class="main" ng-controller="MainController as main"> <h1>{{ main.message }}</h1> </div>
<script src="bower_components/angular/angular.js"></script> <script src="js/app.js"></script> </body></html> Try Protractor