我有这样的文件夹结构: project structure
我正在关注 this tutorial
我的 package.json 文件是这样的:
{
"name": "node_cucumber_e2e",
"version": "1.0.1",
"description": "Sample E2E by Bill to learn cucumber and protractor",
"main": "index.js",
"scripts": {
"start": "node index.js",
"start-driver": "node .\\node_modules\\protractor\\bin\\webdriver-manager start",
"update-driver": "node .\\node_modules\\protractor\\bin\\webdriver-manager update",
"protractor": "node .\\node_modules\\protractor\\bin\\protractor configs/protractor.conf.js"
},
"keywords": [
"Sample_project",
],
"author": "",
"license": "MIT",
"devDependencies": {
"protractor": "^5.4.2"
}
}
我做了以下事情:
- 在本地安装protractor
- 通过
npm run update-driver
更新 webdriver-manager
- 通过
npm run start-driver
启动webdriver-manager - 像下面这样编辑 protractor.conf
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: [`../tests/e2e/specs/*.js`],
baseURL: 'http://localhost:8080/',
framework: 'jasmine',
}
然后,当我尝试通过 npm run protractor
启动 Protractor 时,浏览器是打开的,但是,它没有转到我指定的 url,控制台是这个 ==> output console
我的 spec js 文件是,希望有人能帮助我
describe('Protractor Demo App', function() {
it ('should have a title', function() {
browser.get('https://juliemr.github.io/protractor-demo/');
expect(browser.getTitle()).toEqual('Super Calculator');
});
});
最佳答案
这里有几件事需要注意:
调用 selenium 驱动程序运行测试的方式。还有其他方法可以做到这一点。我更喜欢执行
directConnect = true
并选择headless
选项,以便能够在遇到问题时更好地进行调试。protractor-conf.js
文件引用以下代码片段:export const config: Config = { directConnect: true, capabilities: { 'browserName': 'chrome', chromeOptions: { //args: [ "--headless", "--disable-gpu" ] }, }, }
由于
browser.get() 和 browser.getTitle()
是我们正在进行的异步调用,您可以使用then()< 处理它返回的 promise
或await
用于每个已完成的async
调用。我更喜欢使用
async/await
来处理这样的调用,我会重写spec
为:describe('Protractor Demo App',() => { it ('should have a title', async() => { await browser.get('https://juliemr.github.io/protractor-demo/'); expect(await browser.getTitle()).toEqual('SuperCalculator'); }); });
IMO 它有助于更有效地处理 promise ,同时保持我的代码干净。 让我知道这是否适合您!
进一步改进:
要在您的 package.json
中更新 webdriver-manager,
"webdriver-update": "./node_modules/protractor/bin/webdriver-manager update"
代替 npm run update-driver
。
关于javascript - 为什么 browser.get 方法没有转到指定的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59312504/