Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 8f7c9d1

Browse files
committed
docs(tutorial): add an intro tutorial and TOC and spruce up all docs
1 parent a01f888 commit 8f7c9d1

File tree

8 files changed

+594
-213
lines changed

8 files changed

+594
-213
lines changed

Diff for: README.md

+12-94
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,35 @@
11
Protractor [![Build Status](https://travis-ci.org/angular/protractor.png?branch=master)](https://travis-ci.org/angular/protractor)
22
==========
33

4-
Protractor is an end to end test framework for [AngularJS](http://angularjs.org/) applications built on top of [WebDriverJS](https://code.google.com/p/selenium/wiki/WebDriverJs). Protractor runs tests against your application running in a real browser, interacting with it as a user would.
4+
Protractor is an end-to-end test framework for [AngularJS](http://angularjs.org/) applications. Protractor is a Node.js program built on top of [WebDriverJS](https://code.google.com/p/selenium/wiki/WebDriverJs). Protractor runs tests against your application running in a real browser, interacting with it as a user would.
55

6-
Protractor can be run as a standalone binary, or included into your tests as a library. Use [Protractor as a library](https://github.com/angular/protractor/blob/master/docs/library-only.md) if you would like to manage WebDriver and your test setup yourself.
76

8-
For more information
9-
- [read the docs](https://github.com/angular/protractor/tree/master/docs/getting-started.md)
10-
- [Read the FAQ](https://github.com/angular/protractor/blob/master/docs/faq.md)
11-
- See the [supported browsers](https://github.com/angular/protractor/tree/master/docs/browser-setup.md)
12-
- [See the API](https://github.com/angular/protractor/blob/master/docs/api.md)
7+
Getting Started
8+
---------------
139

14-
To run the sample tests
15-
-----------------------
10+
The Protractor documentation for users is located in the [protractor/docs](https://github.com/angular/protractor/tree/master/docs) folder.
1611

17-
Install protractor with.
12+
To get set up and running quickly:
13+
- Work through the [Tutorial](https://github.com/angular/protractor/blob/master/docs/tutorial.md)
14+
- Take a look at the [Table of Contents](https://github.com/angular/protractor/blob/master/docs/toc.md)
1815

19-
npm install -g protractor
16+
To better understand how Protractor works with the Selenium WebDriver and Selenium Sever see the reference materials.
2017

21-
Start up a selenium server (See the appendix below for help with this). By default, the tests expect the selenium server to be running at `http://localhost:4444/wd/hub`.
2218

23-
The node module's example folder contains a simple test suite which runs against angularjs.org. Run with:
24-
25-
protractor example/conf.js
26-
27-
28-
Using the Protractor runner
29-
---------------------------
30-
31-
The Protractor runner is a binary which accepts a config file. Install protractor with
32-
33-
npm install -g protractor
34-
# Run the line below to see command line options
35-
protractor
36-
37-
You will need a *configuration file* containing setup info and *test files* containing the actual test scripts. The config file specifies how the runner should start webdriver, where your test files are, and global setup options. The test files use Jasmine framework by default ([read about using mocha instead](https://github.com/angular/protractor/tree/master/docs/using-mocha.md)).
38-
39-
Create a configuration file - an example with detailed comments is shown in `node_modules/protractor/docs/referenceConf.js`. Edit the configuration file to point to your test files.
40-
41-
```javascript
42-
// myConf.js
43-
exports.config = {
44-
seleniumAddress: 'http://localhost:4444/wd/hub',
45-
specs: ['myTest.js', 'myTestFolder/*Test.js']
46-
}
47-
```
48-
49-
The configuration file must specify a way to connect to webdriver. This can be
50-
* `seleniumAddress`: The address of a running selenium server.
51-
* `seleniumServerJar`: The location of the selenium standalone .jar file on your machine. Protractor will use this to start up the selenium server.
52-
* `sauceUser` and `sauceKey`: The username and key for a [SauceLabs](http://www.saucelabs.com) account. Protractor will use this to run tests on SauceLabs.
53-
If none of the above are specific, Protractor will try to start the standalone .jar from the default location in node_modules/protractor/selenium.
54-
55-
The runner exposes global variables `browser`, `by` and `element`. Check out [getting started docs](https://github.com/angular/protractor/blob/master/docs/getting-started.md) to learn how to write a test.
56-
57-
```javascript
58-
// myTest.js
59-
describe('angularjs homepage', function() {
60-
it('should greet the named user', function() {
61-
browser.get('http://www.angularjs.org');
62-
63-
element(by.model('yourName')).sendKeys('Julie');
64-
65-
var greeting = element(by.binding('yourName'));
66-
67-
expect(greeting.getText()).toEqual('Hello Julie!');
68-
});
69-
});
70-
```
71-
72-
Run with
73-
74-
protractor myConf.js
75-
76-
77-
Cloning and running Protractor's own tests
78-
------------------------------------------
79-
Clone the github repository.
19+
For contributors
20+
----------------
21+
Clone the github repository:
8022

8123
git clone https://github.com/angular/protractor.git
8224
cd protractor
8325
npm install
8426

8527
Start up a selenium server. By default, the tests expect the selenium server to be running at `http://localhost:4444/wd/hub`.
8628

87-
Protractor's test suite runs against the included testapp. Start that up with
29+
Protractor's test suite runs against the included test application. Start that up with
8830

8931
npm start
9032

9133
Then run the tests with
9234

9335
npm test
94-
95-
96-
Appendix A: Setting up a standalone selenium server
97-
---------------------------------------------------
98-
99-
WebdriverJS does not natively include the selenium server - you must start a standalone selenium server. All you need is the latest [selenium-server-standalone.](http://selenium-release.storage.googleapis.com/index.html). To drive individual browsers, you may need to install separate driver binaries.
100-
101-
To use with chrome browsers, [download chromedriver](http://chromedriver.storage.googleapis.com/index.html).
102-
[More information about chromedriver](https://sites.google.com/a/chromium.org/chromedriver/)
103-
104-
The `webdriver-manager` script is included in the npm package to manage downloads for you. To see the options, run
105-
106-
npm install -g protractor
107-
webdriver-manager
108-
109-
Download and start the selenium server with
110-
111-
webdriver-manager update
112-
webdriver-manager start
113-
114-
Note the [Java Development Kit (JDK)](http://www.oracle.com/technetwork/java/javase/downloads/index.html) is also required to run the webdriver. You may have to download and install it if your computer does not already have it.
115-
116-
For alternate ways to download and start the selenium standalone, see
117-
[the webdriver docs](http://docs.seleniumhq.org/docs/03_webdriver.jsp#running-standalone-selenium-server-for-use-with-remotedrivers).

Diff for: docs/browser-setup.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ capabilities: {
356356
Appendix A: Using with the Protractor Library
357357
---------------------------------------------
358358

359-
If you are not using the Protractor runner (for example, you're using Mocha) and you are setting up webdriver yourself, you will need to create a capabilities object and pass it in to the webdriver builder. The `webdriver.Capabilities` namespace offers some preset options. See [the webdriver capabilities source](https://code.google.com/p/selenium/source/browse/javascript/webdriver/capabilities.js).
359+
If you are not using the Protractor runner and you are setting up webdriver yourself, you will need to create a capabilities object and pass it in to the webdriver builder. The `webdriver.Capabilities` namespace offers some preset options. See [the webdriver capabilities source](https://code.google.com/p/selenium/source/browse/javascript/webdriver/capabilities.js).
360360

361361
```javascript
362362
driver = new webdriver.Builder().

Diff for: docs/install.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Getting Installed
2+
=================
3+
4+
Prerequisites
5+
-------------
6+
Node.js
7+
8+
Protractor is a Node.js program. To run Protractor, you will need to have Node.js installed. Check the version of node you have by running `node --version`. It should be greater than v0.10.0.
9+
10+
Node.js comes with the Protractor npm package, which you can use to install Protractor.
11+
12+
JDK
13+
---
14+
15+
To run the Selenium Server, you will need to have Java Development Kit (JDK) installed. Check this by running `java --version` from the command line.
16+
17+
Installing Protractor
18+
---------------------
19+
20+
Use npm to install Protractor globally (omit the -g if you’d prefer not to install globally):
21+
22+
npm install -g protractor
23+
24+
Check that Protractor is working by running `protractor --version`.
25+
26+
The Protractor install includes the following:
27+
- `protractor` command line tool
28+
- `webdriver-manager` command line tool
29+
- Protractor API (library)
30+
31+
Installing the Selenium Server
32+
------------------------------
33+
Use `webdriver-manager` to set up the standalone Selenium Server.
34+
35+
First, run the update command. This will install the server and ChromeDriver.
36+
37+
webdriver-manager update
38+
39+
Next, start the server with:
40+
41+
webdriver-manager start
42+
43+
You will see a lot of output logs, starting with INFO. The last line will be 'Info - Started org.openqa.jetty.jetty.Server'.
44+
45+
Leave the server running while you conduct your test sessions.

Diff for: docs/locators.md

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
Using Locators
2+
==============
3+
4+
The heart of end to end tests for webpages is finding DOM elements, interacting with them, and getting information about the current state of your application. This doc is an overview of how to locate and perform actions on DOM elements using Protractor.
5+
6+
Overview
7+
--------
8+
9+
Protractor exports a global function `element`, which takes a *Locator* and will return an *ElementFinder*. This function finds a single element - if you need to manipulate multiple elements, use the `element.all` function.
10+
11+
The *ElementFinder* has a set of *action methods*, such as `click()`, `getText()`, and `sendKeys`. These are the core way to interact with an element and get information back from it.
12+
13+
When you find elements in Protractor all actions are asynchronous. Behind the scenes, all actions are sent to the browser being controlled using the JSON Webdriver Wire Protocol. The browser then performs the action as a user natively would.
14+
15+
Locators
16+
--------
17+
18+
A locator tells Protractor how to find a certain DOM element. Protractor exports locator factories on the global `by` object. The most common locators are:
19+
20+
```
21+
// find an element using a css selector
22+
by.css('.myclass')
23+
24+
// find an element with the given id
25+
by.id('myid')
26+
27+
// find an element with a certain ng-model
28+
by.model('name')
29+
30+
// find an element bound to the given variable
31+
by.binding('bindingname')
32+
```
33+
34+
See a [list of Protractor specific locators](/docs/api.md#api-protractorby).
35+
36+
The locators are passed to the `element` function, as below:
37+
38+
```js
39+
element(by.css('some-css'));
40+
element(by.model('item.name'));
41+
element(by.binding('item.name'));
42+
```
43+
44+
When using CSS Selectors as a locator, you can use the shortcut $() notation:
45+
46+
```js
47+
$('my-css');
48+
49+
// Is the same as
50+
51+
element(by.css('my-css'));
52+
```
53+
54+
Actions
55+
-------
56+
57+
`element()` returns an ElementFinder object. The ElementFinder knows how to locate the DOM element using the locator you passed in as a parameter, but it has not actually done so yet. It will not contact the browser until an *action* method has been called.
58+
59+
The most common action methods are:
60+
61+
```js
62+
var el = element(locator);
63+
64+
// Click on the element
65+
el.click();
66+
67+
// Send keys to the element (usually an input)
68+
el.sendKeys('my text');
69+
70+
// Clear the text in an element (usually an input)
71+
el.clear();
72+
73+
// Get the value of an attribute, for example, get the value of an input
74+
el.getAttribute('value');
75+
```
76+
77+
Since all actions are asynchronous, all action methods return a [promise](https://code.google.com/p/selenium/wiki/WebDriverJs#Promises). So, to log the text of an element, you would do something like:
78+
```js
79+
var el = element(locator);
80+
el.getText().then(function(text) {
81+
console.log(text);
82+
});
83+
```
84+
85+
Any action available in WebDriverJS on a WebElement is available on an ElementFinder. [See a full list](https://github.com/angular/protractor/blob/master/docs/api.md#api-webdriver-webelement-prototype-getdriver).
86+
87+
88+
Finding Multiple Elements
89+
-------------------------
90+
91+
To deal with multiple DOM elements, use the `element.all` function. This also takes a locator as its only parameter.
92+
93+
```js
94+
element.all(by.css('.selector')).then(function(elements) {
95+
// elements is an array of ElementFinders.
96+
});
97+
```
98+
99+
`element.all()` has several helper functions:
100+
101+
```js
102+
// Number of elements.
103+
element.all(locator).count();
104+
105+
// Get my index (starting at 0).
106+
element.all(locator).get(index);
107+
108+
// First and last.
109+
element.all(locator).first();
110+
element.all(locator).last();
111+
```
112+
113+
114+
Finding Sub-Elements
115+
--------------------
116+
117+
Simply chain element and element.all together to find sub-elements. For example:
118+
119+
Using a single locator to find an element:
120+
121+
```js
122+
element(by.css('some-css'));
123+
```
124+
125+
Using a single locator to find a list of elements:
126+
```js
127+
element.all(by.css('some-css'));
128+
```
129+
130+
to find a sub-element:
131+
```js
132+
element(by.css('some-css')).element(by.tag('tag-within-css'));
133+
```
134+
135+
to find a list of sub-elements:
136+
```js
137+
element(by.css('some-css')).all(by.tag('tag-within-css'));
138+
```
139+
140+
You can chain with get/first/last as well like so:
141+
142+
```js
143+
element.all(by.css('some-css')).first().element(by.tag('tag-within-css'));
144+
element.all(by.css('some-css')).get(index).element(by.tag('tag-within-css'));
145+
element.all(by.css('some-css')).first().all(by.tag('tag-within-css'));
146+
```
147+
148+
Behind the scenes: ElementFinders versus WebElements
149+
----------------------------------------------------
150+
151+
If you're familiar with WebDriver and WebElements, or you're just curious about the WebElements mentioned above, you may be wondering how they relate to ElementFinders.
152+
153+
When you call `driver.findElement(locator)`, WebDriver immediately sends a command over to the browser asking it to locate the element. This isn't great for creating page objects, because we want to be able to do things in setup (before a page may have been loaded) like
154+
155+
```js
156+
var myButton = ??;
157+
```
158+
159+
and re-use the varialbe `myButton` throughout your test. ElementFinders get around this by simply storing the locator information until an action is called.
160+
161+
```js
162+
var myButton = element(locator);
163+
// No command has been sent to the browser yet.
164+
```
165+
166+
Not until you call an action will the browser recieve any commands.
167+
168+
```js
169+
myButton.click();
170+
// Now two commands are sent to the browser - find the element, and then click it
171+
```
172+
173+
ElementFinders also enable chaining to find subelements, such as `element(locator1).element(locator2)`.
174+
175+
All WebElement actions are wrapped in this way and available on the ElementFinder, in addition to a couple helper methods like `isPresent`.
176+
177+
You may always access the underlying WebElement using `element(locator).getWebElement()`, but you should not need to.
178+

0 commit comments

Comments
 (0)