使用Protractor进行AngularJS e2e测试案例

环境:

y@y:karma-t01$ protractor --version
Version 3.0.0
y@y:karma-t01$ node -v
v4.2.2
y@y:karma-t01$ 
y@y:karma-t01$ webdriver-manager update
y@y:karma-t01$ webdriver-manager start

安装http-server:

y@y:karma-t01$ npm install -g http-server
y@y:karma-t01$ http-server    -p    8080

浏览器:Chrome 版本 46.0.2490.86 (64-bit)

目录结构:

y@y:karma-t01$ tree -L 2
.
├── client
│   ├── app
│   ├── bower_components
│   └── bower.json
├── e2e
│   └── todo
├── karma.conf.js
├── protractor.conf.js
└── readme

(1)e2e/todo/todo.spec.js

'use strict';

describe('',function(){


    beforeEach(function(){
        browser.get('/client/app/todo/todo.html');
    });

    it('',function(){
        var todoListItems = element.all(by.repeater('item in list'));

        expect(todoListItems.count()).toBe(3);
    });
});

(2)client/app/todo/todo.js

'use strict';

var app = angular.module('Application',[]);

app.controller('TodoCtrl',function($scope){
    $scope.list = ['user1','user2','user3'];


});

(3)client/app/todo/todo.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body ng-app="Application">

    <div ng-controller="TodoCtrl">
        <ul ng-repeat="item in list">
            <li>{{item}}</li>
        </ul>
    </div>

    <script src="../../bower_components/angular/angular.js"></script>
    <script src="todo.js"></script>
</body>
</html>

(4)protractor.conf.js

'use strict';

exports.config = {
  allScriptsTimeout: 110000,

  baseUrl: 'http://localhost:8080',

  chromeOnly: true,

  specs: [
    'e2e/**/*.spec.js'
  ],

  exclude: [],

  capabilities: {
    'browserName': 'chrome'
  },

  framework: 'jasmine',

  jasmineNodeOpts: {
    defaultTimeoutInterval: 30000
  }
};

进行测试:

(1)启动webdriver构建测试工程

y@y:karma-t01$ webdriver-manager start
seleniumProcess.pid: 27021
13:17:58.939 INFO - Launching a standalone Selenium Server

(2)启动http服务

y@y:karma-t01$ http-server -p 8080
Starting up http-server, serving ./
Available on:
  http:127.0.0.1:8080
  http:192.168.0.177:8080
Hit CTRL-C to stop the server

(3)运行测试脚本

y@y:karma-t01$ protractor protractor.conf.js 
Starting selenium standalone server...
[launcher] Running 1 instances of WebDriver
Selenium standalone server started at http://192.168.0.177:47286/wd/hub
Started
.


1 spec, 0 failures
Finished in 1.207 seconds
Shutting down selenium standalone server.
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 passed

 

posted @ 2015-11-29 13:23  yshy  阅读(1067)  评论(0编辑  收藏  举报