角度e2e测试显示奇怪的行为:某些测试在从命令行启动时失败但在直接形成浏览器时成功

时间:2013-10-03 19:29:39

标签: angularjs karma-runner angular-scenario

我有一个带有rails后端和angularjs前端的应用程序。我为角度写了一些e2e测试。 当我在浏览器(http://localhost:3000/clientapi/test/e2e/runner.html)上键入url时,它们非常有效,但是当我从命令行使用karma(scripts / e2e-test.sh)启动它们时,一些测试奇怪地失败了。

karma-e2e-conf.js:

basePath: '../',

files: [
  'test/e2e/**/*.js'
],

frameworks: ['ng-scenario'],

autoWatch: false,

browsers: ['Chrome'],

singleRun: true,

proxies: {
  '/': 'http://localhost:3000/clientapi/'
},

junitReporter: {
  outputFile: 'test_out/e2e.xml',
  suite: 'e2e'
},


urlRoot: '__karma__',

index.html:

<div class="container">

    <h1 class="text-center">Clients list</h1>
    <br />


    <a type="button" class="btn btn-default marginTopBottom" href= "#/client/new" >New client</a> 


    <div class="row">
        <div >
            <div class="col-xs-4"><input id='sdsd' ng-model="search" class="form-control" type="text" placeholder="Search"/></div>
             <a type="button" class="btn btn-default" ng-href= "#/index/{{ page + 1}}"  style='float:right' >Next page</a> 
             <a type="button" class="btn btn-default" href= "#/index/{{page - 1}}" ng-hide='hidePreviousButton' style='float:right'>Previous page</a> 

        </div>
    </div>

    <table class="table table-bordered table-hover marginTopBottom">

        <tr >
            <th>Name</th>
            <th>Address</th>
            <th colspan="3">Options</th>
        </tr>

        <tr id='clientList' ng-repeat = "client in clients | filter:search | orderBy:'name'">
            <td>{{client.client.name}}</td>
            <td>{{client.client.address}}</td>
            <td><a href="#/client/{{client.client.id}}" type="button" class="btn btn-default">Show</a></td>
            <td><a href="#/client/{{client.client.id}}/edit" type="button" class="btn btn-default">Edit</a></td>
            <td><button ng-click='delete_client( $index )' type="button" class="btn btn-default">Delete</button></td> 
        </tr>

    </table>

    <br />

</div>

我的测试文件:

describe('testing routes', function(){

    beforeEach( function() {
        browser().navigateTo('../../app/index');
    });

    it('should have a working /index/:page route', function(){
        browser().navigateTo('#/index/5');
        expect(browser().location().url()).toBe('/index/5');
    });

    it('should have a working client/new route', function(){
        browser().navigateTo('#/client/new');
        expect(browser().location().url()).toBe('/client/new');
    });

    it('should have a working client/:id/edit route', function(){
        browser().navigateTo('#/client/361/edit');
        expect(browser().location().url()).toBe('/client/361/edit');
    });

    it('should have a working client/:id route', function(){
        browser().navigateTo('#/client/361');
        expect(browser().location().url()).toBe('/client/361');
    });

});


describe('Index view', function(){

    beforeEach( function() {
        browser().navigateTo('../../app/index');
    });

    it('should contain 100 clients on the first page', function(){
        expect(repeater("#clientList").count()).toBe(100);
    });

    it('should filter the client list as the user types into the search box', function(){
        input('search').enter('addd');
        expect(repeater("#clientList").count()).toBe(1);
    });

    it('should have a button "next page" that leads to the expected url', function(){
        element('a:contains("Next")').click();
        expect(browser().location().url()).toBe('/index/1');
    });

    it('should have a button "new client" that leads to the expected url', function(){
        element('a:contains("New client")').click();
        expect(browser().location().url()).toBe('/client/new');
    });

    it('should have a button "show" for each client that leads to the expected url', function(){
        element('table tr td:contains("VIP") ~ td a:contains("Show")').click();
        expect(browser().location().url()).toBe('/client/361');
    });

    it('should have a button "edit" for each client that leads to the expected url', function(){
        element('table tr td:contains("VIP") ~ td a:contains("Edit")').click();
        expect(browser().location().url()).toBe('/client/361/edit');
    });


});


describe('Show and edit views', function(){

    beforeEach( function() {
        browser().navigateTo('../../app/index');
    });

    it("should show a client's data and take into account any modification from the editor view", function(){
        browser().navigateTo('#/client/361');
        expect(element('td:eq(0)').text()).toBe('VIP');

        browser().navigateTo('#/client/361/edit');
        input('client.client.name').enter('expendable');
        element('button').click();

        browser().navigateTo('http://localhost:3000/clientapi/app/index#/client/361/edit');
        expect(browser().location().url()).toBe('/client/361/edit');
        expect(element('input:eq(0)').val()).toBe('expendable');

        browser().navigateTo('http://localhost:3000/clientapi/app/index#/client/361/edit');
        input('client.client.name').enter('VIP');
        element('button').click();

    });


    it('should be impossible to save changes when the required fields are empty', function(){

        browser().navigateTo('#/client/361/edit');
        input('client.client.name').enter('');
        element('button').click();

        expect(browser().location().url()).toBe('/client/361/edit');

    });

});

测试结果:

Chromium 28.0.1500 (Ubuntu) Incredibly cool client management app Index view should contain 100 clients on the first page FAILED
    expect repeater '#clientList' count toBe 100
    http://localhost:9876/base/test/e2e/scenarios.js?1380821266000:43:4: expected 100 but was 0
Chromium 28.0.1500 (Ubuntu) Incredibly cool client management app Index view should filter the client list as the user types into the search box FAILED
    expect repeater '#clientList' count toBe 1
    http://localhost:9876/base/test/e2e/scenarios.js?1380821266000:48:4: expected 1 but was 0
Chromium 28.0.1500 (Ubuntu) Incredibly cool client management app Index view should have a button "show" for each client that leads to the expected url FAILED
    element 'table tr td:contains("VIP") ~ td a:contains("Show")' click
    http://localhost:9876/base/test/e2e/scenarios.js?1380821266000:62:4: Selector table tr td:contains("VIP") ~ td a:contains("Show") did not match any elements.
Chromium 28.0.1500 (Ubuntu) Incredibly cool client management app Index view should have a button "edit" for each client that leads to the expected url FAILED
    element 'table tr td:contains("VIP") ~ td a:contains("Edit")' click
    http://localhost:9876/base/test/e2e/scenarios.js?1380821266000:67:4: Selector table tr td:contains("VIP") ~ td a:contains("Edit") did not match any elements.
Chromium 28.0.1500 (Ubuntu) Incredibly cool client management app Show and edit views should show a client's data and take into account any modification from the editor view FAILED
    expect element 'td:eq(0)' text toBe "VIP"
    http://localhost:9876/base/test/e2e/scenarios.js?1380821266000:83:4: expected "VIP" but was ""
Chromium 28.0.1500 (Ubuntu): Executed 12 of 12 (5 FAILED) (7.341 secs / 6.798 secs)

有什么想法吗?

0 个答案:

没有答案