量角器测试在本地传递,但在Travis-CI上没有传递

时间:2016-07-22 12:08:31

标签: javascript angularjs protractor travis-ci angularjs-e2e

我最近在AngularJS应用程序中添加了使用Protractor的端到端测试。我在本地运行测试并且它们都通过了,但是当我承诺使用GitHub和Travis for CI时,大多数测试都失败了。

我注意到失败的测试是那些需要路由到其他状态的测试(我正在使用Angular UI路由器)。

scenarios.js

describe('Test', function () {

    beforeEach(function () {
        browser.get('/');
    })

    it('should open the user page', function () {

        //browser.get("/");
        browser.sleep(3000);
        var button = element(by.id('createSession'));
        button.click().then(function () {
            browser.sleep(3000);
            expect(browser.getLocationAbsUrl()).toEqual("/user");
        });
    });

    it('should create a session and add a user', function () {

        //browser.get("/");
        browser.sleep(3000);
        var button = element(by.id('createSession'));
        button.click();
        browser.sleep(3000);
        var input = element(by.id('username'));
        input.sendKeys('Simona');
        var joinButton = element(by.id('joinSession'));
        joinButton.click();
        browser.sleep(3000);
        expect(element(by.id('addStart')).isPresent()).toBe(true);

    });

    it('should join an existing session', function () {

        //browser.get("/");
        browser.sleep(3000);
        var inputSession = element(by.id('sessionId'));
        inputSession.sendKeys('testing123');
        var joinSessionBtn = element(by.id('enterSession'));
        joinSessionBtn.click();
        browser.sleep(3000);
        var input = element(by.id('username'));
        input.sendKeys('Simona1');
        var joinButton = element(by.id('joinSession'));
        joinButton.click();
        browser.sleep(3000);
        expect(element(by.id('addStart')).isPresent()).toBe(true);

    });

    it('should add user to active users', function () {

        //browser.get("/");
        browser.sleep(3000);
        var inputSession = element(by.id('sessionId'));
        inputSession.sendKeys('testing123');
        var joinSessionBtn = element(by.id('enterSession'));
        joinSessionBtn.click();
        browser.sleep(3000);
        var input = element(by.id('username'));
        input.sendKeys('Simona');
        var joinButton = element(by.id('joinSession'));
        joinButton.click();
        browser.sleep(3000);
        var user = element(by.id('Simona'));
        expect(user.isPresent()).toBe(true);

    });

    it('should not join a non-existing session', function () {

        //browser.get("http://localhost:8000/");
        browser.sleep(3000);
        var inputSession = element(by.id('sessionId'));
        inputSession.sendKeys('simonovaSesija');
        var joinSessionBtn = element(by.id('enterSession'));
        joinSessionBtn.click();
        browser.sleep(3000);
        var warning = element(by.id('warningSession')).isDisplayed();
        expect(warning).toBe(true);

    });


    it('should add an anonymous user on empty username input', function () {

        //browser.get("http://localhost:8000/");
        browser.sleep(3000);
        var inputSession = element(by.id('sessionId'));
        inputSession.sendKeys('testing123');
        var joinSessionBtn = element(by.id('enterSession'));
        joinSessionBtn.click();
        browser.sleep(3000);
        var input = element(by.id('username'));
        input.sendKeys('');
        var joinButton = element(by.id('joinSession'));
        joinButton.click();
        browser.sleep(4000);
        var user = element(by.id('Anonymous'));
        expect(user.isPresent()).toBe(true);

    });

});

protractor.conf.js

    exports.config = {

      allScriptsTimeout: 11000,

      specs: [
        "scenarios.js"
      ],

      capabilities: {
        "browserName": "chrome"
      },

      baseUrl: "http://localhost:8000/",

      framework: "jasmine",

      jasmineNodeOpts: {
        defaultTimeoutInterval: 30000
      }

    };

的package.json

    {
      "name": "angular-seed",
      "private": false,
      "version": "0.0.0",
      "description": "A starter project for AngularJS",
      "repository": "https://github.com/angular/angular-seed",
      "license": "MIT",
      "devDependencies": {
        "bower": "^1.7.7",
        "http-server": "^0.9.0",
        "jasmine-core": "^2.4.1",
        "karma": "^0.13.22",
        "karma-chrome-launcher": "^0.2.3",
        "karma-firefox-launcher": "^0.1.7",
        "karma-jasmine": "^0.3.8",
        "karma-junit-reporter": "^0.4.1",
        "protractor": "^3.2.2"
      },
      "scripts": {
        "postinstall": "bower install",
        "prestart": "npm install",
        "start": "http-server -a localhost -p 8000 -c-1 ./app",
        "pretest": "npm install",
        "test": "karma start karma.conf.js",
        "test-single-run": "karma start karma.conf.js --single-run",
        "preupdate-webdriver": "npm install",
        "update-webdriver": "webdriver-manager update",
        "preprotractor": "npm run update-webdriver",
        "protractor": "protractor e2e-tests/protractor.conf.js",
        "update-index-async": "node -e \"var fs=require('fs'),indexFile='app/index-async.html',loaderFile='app/bower_components/angular-loader/angular-loader.min.js',loaderText=fs.readFileSync(loaderFile,'utf-8').split(/sourceMappingURL=angular-loader.min.js.map/).join('sourceMappingURL=bower_components/angular-loader/angular-loader.min.js.map'),indexText=fs.readFileSync(indexFile,'utf-8').split(/\\/\\/@@NG_LOADER_START@@[\\s\\S]*\\/\\/@@NG_LOADER_END@@/).join('//@@NG_LOADER_START@@\\n'+loaderText+'    //@@NG_LOADER_END@@');fs.writeFileSync(indexFile,indexText);\""
      },
      "dependencies": {
        "bower": "^1.7.9",
        "express": "^4.14.0"
      }

}

.travis.yml

language: node_js
node_js:
  - '4.4'

addons:
  firefox: "latest"

# blocklist
branches:
  except:
    - master

# safelist
branches:
  only:
    - dev

before_script:
  - export CHROME_BIN=chromium-browser
  - export DISPLAY=:99.0
  - sh -e /etc/init.d/xvfb start
  - npm start > /dev/null &
  - npm run update-webdriver
  - sleep 1 # give server time to start

script:
  - node_modules/.bin/karma start karma.conf.js --no-auto-watch --single-run --reporters=dots
  - node_modules/.bin/protractor e2e-tests/protractor.conf.js

如您所见,我正在使用angular-seed的Angular模板。

有关为何发生这种情况的任何建议?

1 个答案:

答案 0 :(得分:1)

知道你会得到什么样的错误会非常有帮助,但无论如何,你都有一些明显的罪魁祸首......

  1. CI无头(即通过XVFB),你的本地不是。这两者将有不同的时间问题。
  2. 您有很多睡眠,这可能会导致您的测试在不同的环境中表现不同(例如CI与本地)。这是sleep不好的原因之一。这也会让我相信你的应用 Angular?如果这是真的,那么自己处理任何wait会更好。如果为真,那么为什么所有人都睡觉?
  3. 希望有所帮助。