简体   繁体   中英

Angular 4 (ejected) and E2E tests (Protractor/Selenium configuration)

I'm trying to run E2E tests via Protractor/Selenium on an ejected Angular 4 project.

My package.json :

...
"scripts": {
    "pree2e": "webdriver-manager update --standalone false --gecko false --quiet node",
    "e2e": "protractor ./protractor.conf.js"
}
...

My protractor.conf :

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub',
    directConnect: true,
    allScriptsTimeout: 60000,
    getPageTimeout: 60000,
    specs: [
        './src/e2e/**/*.e2e-test.ts'
    ],
    capabilities: {
        'browserName': 'chrome'
    },
    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 60000
    },
    onPrepare() {
        require('ts-node').register({
            project: 'tsconfig.e2e.json'
        });

        jasmine.getEnv().addReporter(new SpecReporter({
            spec: {
                displayStacktrace: true
            }
        }));

        browser.driver.manage().timeouts().setScriptTimeout(60000);
    }
};

When running npm run e2e Chrome boots up, but it's trying to open data:text/html,<html></html> for every test, and then shuts down quickly. What am I missing? I tried adding baseUrl to my protractor.conf , but it doesn't help, as it seems that Selenium is not even running.

I finally found a solution to my problem. I somehow needed to run ng serve , which can now, because the project was ejected, be run via npm run start . This is a Travis CI deployment (which I highly recommend), so see the before_script :

before_script:
    - nohup npm run start &
script:
    - npm run build -aot --target=production --environment=prod
    - npm run test
    - npm run e2e

Final package.json (remains default after ng eject ):

...
"scripts": {
    "ng": "ng",
    "lint": "ng lint",
    "build": "webpack",
    "start": "webpack-dev-server --port=4200",
    "test": "karma start ./karma.conf.js",
    "pree2e": "webdriver-manager update --standalone false --gecko false --quiet",
    "e2e": "protractor ./protractor.conf.js",
    "postinstall": "npm run build -aot --target=production --environment=prod"
}
...

Final protractor.conf (remains default after ng eject ):

exports.config = {
    allScriptsTimeout: 11000,
    specs: [
        './src/e2e/**/*.e2e-test.ts'
    ],
    capabilities: {
        'browserName': 'chrome'
    },
    directConnect: true,
    baseUrl: 'http://localhost:4200/',
    framework: 'jasmine',
    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 30000,
        print: function() {}
    },
    beforeLaunch: function() {
        require('ts-node').register({
            project: 'tsconfig.e2e.json'
        });
    },
    onPrepare() {
        jasmine.getEnv().addReporter(new SpecReporter({
            spec: {
                displayStacktrace: true
            }
        }));
    }
};

Here's what happens when you ng eject : https://github.com/angular/angular-cli/issues/6171 .

Two and a half days waisted. Thank you Angular CLI team for this bug, truly, thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM