PS C:\study\demo> webdriver-manager
Usage: webdriver-manager <command>
Commands:
update: install or update selected binaries
start: start up the selenium server
status: list the current available drivers
Options:
--out_dir Location to output/expect [default: "C:\\Users\\xxx\\AppData\\Roaming\\npm\\node_modules\\protractor\\selenium"]
--seleniumPort Optional port for the selenium standalone server
--ignore_ssl Ignore SSL certificates [default: false]
--proxy Proxy to use for the install or update command
--alternate_cdn Alternate CDN to the binaries
--standalone Install or update selenium standalone [default: true]
--chrome Install or update chromedriver [default: true]
--ie Install or update IEDriver [default: false]
Please specify one command
这里给出了默认的保存地址。
成功下载之后,启动服务器检查一下。
PS C:\study\demo> webdriver-manager start
seleniumProcess.pid: 4856
15:16:07.179 INFO - Launching a standalone server
Setting system property webdriver.chrome.driver to C:\Users\Whao\AppData\Roaming\npm\node_modules\protractor\selenium\chromedriver.exe
15:16:07.238 INFO - Java: Oracle Corporation 23.6-b04
15:16:07.238 INFO - OS: Windows 7 6.1 amd64
15:16:07.245 INFO - v2.45.0, with Core v2.45.0. Built from revision 5017cb8
15:16:07.317 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
15:16:07.318 INFO - Version Jetty/5.1.x
15:16:07.318 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
15:16:07.319 INFO - Started HttpContext[/selenium-server,/selenium-server]
15:16:07.319 INFO - Started HttpContext[/,/]
15:16:07.364 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@601edf33
15:16:07.364 INFO - Started HttpContext[/wd,/wd]
15:16:07.367 INFO - Started SocketListener on 0.0.0.0:4444
15:16:07.367 INFO - Started org.openqa.jetty.jetty.Server@792bf755
我们的 Protractor 测试将会把测试请求发送到这个服务器,通过它来控制本地的浏览器进行测试,在我们的整个教程中,保持这个服务器的运行,在下面的地址,你可以看到关于服务器状态的信息。
http://localhost:4444/wd/hub
3. 演练
Step 0 write a test
保持上面的命令行窗口运行,重新打开另外一个命令行窗口,创建一个用于测试的干净文件夹。
Protractor 需要两个文件来运行测试,一个测试规范文件,一个配置文件。
让我们从示例的 AngularJS 应用开始写一个简单的测试,我们使用位于 http://juliemr.github.io/protractor-demo/ 的超级计算器应用,测试将检查页面的 title 是否符合我们的预期。
复制下面的代码到 spec.js 文件中。
// spec.js
describe('Protractor Demo App', function() {
var firstNumber = element(by.model('first'));
var secondNumber = element(by.model('second'));
var goButton = element(by.id('gobutton'));
var latestResult = element(by.binding('latest'));
beforeEach(function() {
browser.get('http://juliemr.github.io/protractor-demo/');
});
it('should have a title', function() {
expect(browser.getTitle()).toEqual('Super Calculator');
});
it('should add one and two', function() {
firstNumber.sendKeys(1);
secondNumber.sendKeys(2);
goButton.click();
expect(latestResult.getText()).toEqual('3');
});
it('should add four and six', function() {
// Fill this in.
expect(latestResult.getText()).toEqual('10');
});
});
这里,我们把导航移到了 beforeEach 中,这个函数会在每一个 it 块之前执行。我们还把 ElementFinder 保存在变量中进行共享使用。
Step 3 - changing the configuration
我们已经写了一些测试,现在来看看配置文件。在配置文件中,我们可以配置使用什么浏览器,如何连接到 Selenium 服务器等等,先改变一下我们使用的服务器。