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

Commit 7d74184

Browse files
committedOct 8, 2013
feat(explorer): add an interactive element explorer
When debugging or first writing test suites, you may find it helpful to try out Protractor commands without starting up the entire test suite. You can do this with the element explorer. This change introduces a first version of the element explorer. Closes #107
1 parent b501ceb commit 7d74184

File tree

2 files changed

+184
-2
lines changed

2 files changed

+184
-2
lines changed
 

‎bin/elementexplorer.js

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* This is an explorer to help get the right element locators, and test out what
5+
* Protractor commands will do on your site without running a full test suite.
6+
*
7+
* This beta version only uses the Chrome browser.
8+
*
9+
* Usage:
10+
*
11+
* Expects a selenium standalone server to be running at http://localhost:4444
12+
* from protractor directory, run with:
13+
*
14+
* ./bin/elementexplorer.js <urL>
15+
*
16+
* This will load up the URL on webdriver and put the terminal into a REPL loop.
17+
* You will see a > prompt. The `ptor` and `protractor` variables will
18+
* be available. Enter a command such as:
19+
*
20+
* > ptor.findElement(protractor.By.id('foobar')).getText()
21+
*
22+
* or
23+
*
24+
* > ptor.get('http://www.angularjs.org')
25+
*
26+
* try just
27+
*
28+
* > ptor
29+
*
30+
* to get a list of functions you can call.
31+
*
32+
* Typing tab at a blank prompt will fill in a suggestion for finding
33+
* elements.
34+
*/
35+
36+
var webdriver = require('selenium-webdriver');
37+
var protractor = require('../lib/protractor.js');
38+
var repl = require('repl');
39+
var util = require('util');
40+
var vm = require('vm');
41+
42+
var driver, ptor;
43+
44+
var INITIAL_SUGGESTIONS = [
45+
'ptor.findElement(protractor.By.id(\'\'))',
46+
'ptor.findElement(protractor.By.css(\'\'))',
47+
'ptor.findElement(protractor.By.name(\'\'))',
48+
'ptor.findElement(protractor.By.binding(\'\'))',
49+
'ptor.findElement(protractor.By.input(\'\'))',
50+
'ptor.findElement(protractor.By.select(\'\'))',
51+
'ptor.findElement(protractor.By.textarea(\'\'))',
52+
'ptor.findElement(protractor.By.xpath(\'\'))',
53+
'ptor.findElement(protractor.By.tagName(\'\'))',
54+
'ptor.findElement(protractor.By.className(\'\'))'
55+
];
56+
57+
var list = function(locator) {
58+
return ptor.findElements(locator).then(function(arr) {
59+
var found = [];
60+
for (var i = 0; i < arr.length; ++i) {
61+
arr[i].getText().then(function(text) {
62+
found.push(text);
63+
});
64+
}
65+
return found;
66+
});
67+
};
68+
69+
var flowEval = function(code, context, file, callback) {
70+
71+
var vmErr,
72+
result,
73+
flow = webdriver.promise.controlFlow();
74+
75+
flow.execute(function() {
76+
try {
77+
result = vm.runInThisContext(code, file);
78+
} catch (e) {
79+
vmErr = e;
80+
callback(vmErr, null);
81+
}
82+
if (vmErr && process.domain) {
83+
process.domain.emit('error', vmErr);
84+
process.domain.exit();
85+
}
86+
return result;
87+
}).then(function(res) {
88+
if (!vmErr) {
89+
callback(null, res);
90+
}
91+
}, function(err) {
92+
callback('There was a webdriver error: ' + err.name + ' ' + err.message,
93+
null);
94+
});
95+
};
96+
97+
var startRepl = function() {
98+
var flowRepl = repl.start({
99+
'useGlobal': true,
100+
'eval': flowEval
101+
});
102+
103+
var originalComplete = flowRepl.complete;
104+
105+
flowRepl.complete = function(line, completeCallback) {
106+
if (line == '') {
107+
completeCallback(null, [INITIAL_SUGGESTIONS, '']);
108+
} else {
109+
originalComplete.apply(this, arguments);
110+
}
111+
};
112+
113+
flowRepl.on('exit', function() {
114+
driver.quit();
115+
util.puts('Shutting down. Goodbye.');
116+
});
117+
};
118+
119+
var startUp = function() {
120+
driver = new webdriver.Builder().
121+
usingServer('http://localhost:4444/wd/hub').
122+
withCapabilities({'browserName': 'chrome'}).build();
123+
124+
driver.getSession().then(function(session) {
125+
driver.manage().timeouts().setScriptTimeout(11000);
126+
127+
ptor = protractor.wrapDriver(driver);
128+
129+
// Set up globals to be available from the command line.
130+
global.driver = driver;
131+
global.ptor = ptor;
132+
global.protractor = protractor;
133+
global.list = list;
134+
135+
util.puts('Type <tab> to see a list of locator strategies.');
136+
util.puts('Use the `list` helper function to find elements by strategy:');
137+
util.puts(' e.g., list(protractor.By.binding(\'\')) gets all bindings.');
138+
util.puts('');
139+
140+
var url = process.argv[2] || 'about:blank';
141+
util.puts('Getting page at: ' + url);
142+
driver.get(url);
143+
144+
startRepl();
145+
});
146+
};
147+
148+
startUp();

‎docs/debugging.md

+36-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ This uses the [node debugger](http://nodejs.org/api/debugger.html). Enter
6363
`c` to start execution and continue after the breakpoint.
6464

6565
We use `ptor.debugger();` instead of node's `debugger;` statement so that
66-
the test pauses after the get command has been *executed*. Using `debugger;`
67-
pauses the test after the get command is *scheduled* but has not yet
66+
the test pauses after the get command has beenexecuted*. Using `debugger;`
67+
pauses the test after the get command isscheduled* but has not yet
6868
been sent to the browser.
6969

7070
Protractor's `debugger` method works by scheduling a node debug breakpoint
@@ -80,6 +80,40 @@ used from the browser's console.
8080
// Should return the input element with model 'user.name'.
8181
```
8282

83+
Testing out Protractor interactively
84+
------------------------------------
85+
86+
When debugging or first writing test suites, you may find it helpful to
87+
try out Protractor commands without starting up the entire test suite. You can
88+
do this with the element explorer.
89+
90+
91+
Currently, the explorer runs only with chrome and expects a selenium standalone
92+
server to be running at http://localhost:4444.
93+
94+
From protractor directory, run with:
95+
96+
./bin/elementexplorer.js <urL>
97+
98+
This will load up the URL on webdriver and put the terminal into a REPL loop.
99+
You will see a > prompt. The `ptor` and `protractor` variables will
100+
be available. Enter a command such as:
101+
102+
> ptor.findElement(protractor.By.id('foobar')).getText()
103+
104+
or
105+
106+
> ptor.get('http://www.angularjs.org')
107+
108+
try just
109+
110+
> ptor
111+
112+
to get a list of functions you can call.
113+
114+
Typing tab at a blank prompt will fill in a suggestion for finding
115+
elements.
116+
83117
Taking Screenshots
84118
------------------
85119

0 commit comments

Comments
 (0)