-
Notifications
You must be signed in to change notification settings - Fork 39
/
JavascriptExecutor.js
115 lines (96 loc) · 3.22 KB
/
JavascriptExecutor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict';
describe('JavascriptExecutor', function() {
var assert = require('assert');
var path = require('path');
var driver = require(path.resolve(__dirname, '..', 'lib', 'driver')).driver;
var wd = require(
path.resolve(__dirname, '..', '..', 'src', 'webdriver-sync')
);
var WebElement = wd.WebElement;
var TimeUnit = wd.TimeUnit;
beforeEach(function() {
driver.get('http://google.com');
});
after(function(){
driver.quit();
});
describe('#executeScript', function() {
it('can return a WebElement', function() {
var el = driver.executeScript(
'return document.querySelector("[name=q]");'
);
assert(el instanceof WebElement);
});
it('can return an array of WebElements', function() {
var divs = driver.executeScript(
'return document.querySelectorAll("div");'
);
divs.forEach(function(div) {
assert(div instanceof WebElement);
});
});
it('can return numbers', function() {
assert.equal(driver.executeScript('return 5;'), 5);
});
it('can return strings', function() {
assert.equal(driver.executeScript('return "boo";'), 'boo');
});
it('can return objects', function() {
var result = driver.executeScript('return {asdf:5};');
assert.equal(result.asdf, 5);
});
it('can return arrays', function() {
var result = driver.executeScript('return ["boo"];');
assert.equal(result[0], 'boo');
});
it('handles nested arrays and objects', function(){
var result = driver.executeScript('return {arr:["boo"],obj:{asdf:5}};');
assert.equal(result.arr[0]+result.obj.asdf, 'boo5');
});
});
describe('#executeAsyncScript', function() {
before(function(){
driver.manage().timeouts().setScriptTimeout(5, TimeUnit.SECONDS);
});
it('can return a WebElement', function() {
var el = driver.executeAsyncScript(
wrapInAsync('document.querySelector("[name=q]")')
);
assert(el instanceof WebElement);
});
it('can return an array of WebElements', function() {
var divs = driver.executeAsyncScript(
wrapInAsync('document.querySelectorAll("div")')
);
divs.forEach(function(div) {
assert(div instanceof WebElement);
});
});
it('can return numbers', function() {
assert.equal(driver.executeAsyncScript(wrapInAsync('5')), 5);
});
it('can return strings', function() {
assert.equal(driver.executeAsyncScript(wrapInAsync('"boo"')), 'boo');
});
it('can return objects', function() {
var result = driver.executeAsyncScript(wrapInAsync('{asdf:5}'));
assert.equal(result.asdf, 5);
});
it('can return arrays', function() {
var result = driver.executeAsyncScript(wrapInAsync('["boo"]'));
assert.equal(result[0], 'boo');
});
it('handles nested arrays and objects', function(){
var result = driver.executeAsyncScript(
wrapInAsync('{arr:["boo"],obj:{asdf:5}}')
);
assert.equal(result.arr[0]+result.obj.asdf, 'boo5');
});
});
function wrapInAsync(value){
return 'var callback = arguments[arguments.length - 1];'
+ 'setTimeout(function(){'
+ ' callback('+value+');'
+ '}, 10);';
}
});