新建一个文件夹,用来放置 protractor 项目。
创建配置文件
新建一个 conf.js
文件,将下边的内容放到该文件中:
exports.config = {
framework: 'jasmine',
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['spec.js']
}
通过配置文件,我们期望使用 jasmine 作为测试的 framework,测试请求会发送到启动的 Selenium Server 的实例,去执行 spec.js
这个测试文件。
创建测试文件
新建一个 spec.js
文件,将下边的内容放到该文件中:
describe('Protractor Demo App', function() {
it('should have a title', function() {
browser.get('https://baidu.com');
expect(browser.getTitle()).toEqual('百度一下,你就知道');
});
});
运行测试
使用下边的命令启动测试:
protractor conf.js
第一次启动的时候遇到下边的错误:
protractor : File C:\Users\xxx\AppData\Roaming\npm\protractor.ps1 cannot be loaded because running scripts is disabled on this system. For more
information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ protractor conf.js
+ ~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
Bing 了一下,是因为 windows 不允许执行 powershell 脚本。解决办法,邮件点击开始菜单,选择 “Windows Powershell (Admin)”,然后执行下边的命令:
set-ExecutionPolicy RemoteSigned
问题解决了,再运行一下启动命令,一个浏览器窗口会自动弹出来,打开了百度页面。
但是命令行中又提升出错:
E/protractor - Could not find Angular on page https://baidu.com/ : retries looking for angular exceeded
Message:
Failed: Angular could not be found on the page https://baidu.com/. If this is not an Angular application, you may need to turn off waiting for Angular.
Please see
https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load
Bing 了一下,是因为 Protractor 是为了 Angular 页面创建的,所以默认它会去页面上找 ng-app
这个 tag,但是百度不是 Angular 的所以就会出现这个问题。解决方法很简单,在 browser.get()
前加上下边的代码来 turn off waiting for Angular
:
browser.waitForAngularEnabled(false);
彻底成功执行后,会快速自动打开一个Chrome 浏览器,访问 Baidu 然后关闭,在命令行会看到下边的结果:
PS C:\GIT\Protractor> protractor conf.js
[23:47:39] I/launcher - Running 1 instances of WebDriver
[23:47:39] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
Started
.
1 spec, 0 failures
Finished in 1.4 seconds
[23:47:43] I/launcher - 0 instance(s) of WebDriver still running
[23:47:43] I/launcher - chrome #01 passed