Deployed the page to Github Pages.

This commit is contained in:
Batuhan Berk Başoğlu 2024-11-03 21:30:09 -05:00
parent 1d79754e93
commit 2c89899458
Signed by: batuhan-basoglu
SSH key fingerprint: SHA256:kEsnuHX+qbwhxSAXPUQ4ox535wFHu/hIRaa53FzxRpo
62797 changed files with 6551425 additions and 15279 deletions

160
node_modules/selenium-webdriver/test/lib/by_test.js generated vendored Normal file
View file

@ -0,0 +1,160 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
'use strict';
var assert = require('assert');
var by = require('../../lib/by');
describe('by', function() {
describe('By', function() {
describe('className', function() {
it('delegates to By.css', function() {
let locator = by.By.className('foo');
assert.equal('css selector', locator.using);
assert.equal('.foo', locator.value);
});
it('escapes class name', function() {
let locator = by.By.className('foo#bar');
assert.equal('css selector', locator.using);
assert.equal('.foo\\#bar', locator.value);
});
it('translates compound class names', function() {
let locator = by.By.className('a b');
assert.equal('css selector', locator.using);
assert.equal('.a.b', locator.value);
locator = by.By.className(' x y z-1 "g" ');
assert.equal('css selector', locator.using);
assert.equal('.x.y.z-1.\\"g\\"', locator.value);
});
});
describe('id', function() {
it('delegates to By.css', function() {
let locator = by.By.id('foo');
assert.equal('css selector', locator.using);
assert.equal('*[id="foo"]', locator.value);
});
it('escapes the ID', function() {
let locator = by.By.id('foo#bar');
assert.equal('css selector', locator.using);
assert.equal('*[id="foo\\#bar"]', locator.value);
});
});
describe('name', function() {
it('delegates to By.css', function() {
let locator = by.By.name('foo')
assert.equal('css selector', locator.using);
assert.equal('*[name="foo"]', locator.value);
});
it('escapes the name', function() {
let locator = by.By.name('foo"bar"')
assert.equal('css selector', locator.using);
assert.equal('*[name="foo\\"bar\\""]', locator.value);
});
it('escapes the name when it starts with a number', function() {
let locator = by.By.name('123foo"bar"')
assert.equal('css selector', locator.using);
assert.equal('*[name="\\31 23foo\\"bar\\""]', locator.value);
});
it('escapes the name when it starts with a negative number', function() {
let locator = by.By.name('-123foo"bar"')
assert.equal('css selector', locator.using);
assert.equal('*[name="-\\31 23foo\\"bar\\""]', locator.value);
});
});
});
describe('checkedLocator', function() {
it('accepts a By instance', function() {
let original = by.By.name('foo');
let locator = by.checkedLocator(original);
assert.strictEqual(locator, original);
});
it('accepts custom locator functions', function() {
let original = function() {};
let locator = by.checkedLocator(original);
assert.strictEqual(locator, original);
});
// See https://github.com/SeleniumHQ/selenium/issues/3056
it('accepts By-like objects', function() {
let fakeBy = {using: 'id', value: 'foo'};
let locator = by.checkedLocator(fakeBy);
assert.strictEqual(locator.constructor, by.By);
assert.equal(locator.using, 'id');
assert.equal(locator.value, 'foo');
});
it('accepts class name', function() {
let locator = by.checkedLocator({className: 'foo'});
assert.equal('css selector', locator.using);
assert.equal('.foo', locator.value);
});
it('accepts css', function() {
let locator = by.checkedLocator({css: 'a > b'});
assert.equal('css selector', locator.using);
assert.equal('a > b', locator.value);
});
it('accepts id', function() {
let locator = by.checkedLocator({id: 'foobar'});
assert.equal('css selector', locator.using);
assert.equal('*[id="foobar"]', locator.value);
});
it('accepts linkText', function() {
let locator = by.checkedLocator({linkText: 'hello'});
assert.equal('link text', locator.using);
assert.equal('hello', locator.value);
});
it('accepts name', function() {
let locator = by.checkedLocator({name: 'foobar'});
assert.equal('css selector', locator.using);
assert.equal('*[name="foobar"]', locator.value);
});
it('accepts partialLinkText', function() {
let locator = by.checkedLocator({partialLinkText: 'hello'});
assert.equal('partial link text', locator.using);
assert.equal('hello', locator.value);
});
it('accepts tagName', function() {
let locator = by.checkedLocator({tagName: 'div'});
assert.equal('css selector', locator.using);
assert.equal('div', locator.value);
});
it('accepts xpath', function() {
let locator = by.checkedLocator({xpath: '//div[1]'});
assert.equal('xpath', locator.using);
assert.equal('//div[1]', locator.value);
});
});
});

View file

@ -0,0 +1,111 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
'use strict';
const Capabilities = require('../../lib/capabilities').Capabilities;
const Symbols = require('../../lib/symbols');
const assert = require('assert');
describe('Capabilities', function() {
it('can set and unset a capability', function() {
let caps = new Capabilities();
assert.equal(undefined, caps.get('foo'));
assert.ok(!caps.has('foo'));
caps.set('foo', 'bar');
assert.equal('bar', caps.get('foo'));
assert.ok(caps.has('foo'));
caps.set('foo', null);
assert.equal(null, caps.get('foo'));
assert.ok(caps.has('foo'));
});
it('requires string capability keys', function() {
let caps = new Capabilities();
assert.throws(() => caps.set({}, 'hi'));
});
it('can merge capabilities', function() {
let caps1 = new Capabilities()
.set('foo', 'bar')
.set('color', 'red');
let caps2 = new Capabilities()
.set('color', 'green');
assert.equal('bar', caps1.get('foo'));
assert.equal('red', caps1.get('color'));
assert.equal('green', caps2.get('color'));
assert.equal(undefined, caps2.get('foo'));
caps2.merge(caps1);
assert.equal('bar', caps1.get('foo'));
assert.equal('red', caps1.get('color'));
assert.equal('red', caps2.get('color'));
assert.equal('bar', caps2.get('foo'));
});
it('can be initialized from a hash object', function() {
let caps = new Capabilities({'one': 123, 'abc': 'def'});
assert.equal(123, caps.get('one'));
assert.equal('def', caps.get('abc'));
});
it('can be initialized from a map', function() {
let m = new Map([['one', 123], ['abc', 'def']]);
let caps = new Capabilities(m);
assert.equal(123, caps.get('one'));
assert.equal('def', caps.get('abc'));
});
describe('serialize', function() {
it('works for simple capabilities', function() {
let m = new Map([['one', 123], ['abc', 'def']]);
let caps = new Capabilities(m);
assert.deepEqual({one: 123, abc: 'def'}, caps[Symbols.serialize]());
});
it('does not omit capabilities set to a false-like value', function() {
let caps = new Capabilities;
caps.set('bool', false);
caps.set('number', 0);
caps.set('string', '');
assert.deepEqual(
{bool: false, number: 0, string: ''},
caps[Symbols.serialize]());
});
it('omits capabilities with a null value', function() {
let caps = new Capabilities;
caps.set('foo', null);
caps.set('bar', 123);
assert.deepEqual({bar: 123}, caps[Symbols.serialize]());
});
it('omits capabilities with an undefined value', function() {
let caps = new Capabilities;
caps.set('foo', undefined);
caps.set('bar', 123);
assert.deepEqual({bar: 123}, caps[Symbols.serialize]());
});
});
});

177
node_modules/selenium-webdriver/test/lib/events_test.js generated vendored Normal file
View file

@ -0,0 +1,177 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
'use strict';
const EventEmitter = require('../../lib/events').EventEmitter;
const assert = require('assert');
const sinon = require('sinon');
describe('EventEmitter', function() {
describe('#emit()', function() {
it('can emit events when nothing is registered', function() {
let emitter = new EventEmitter;
emitter.emit('foo');
// Ok if no errors are thrown.
});
it('can pass args to listeners on emit', function() {
let emitter = new EventEmitter;
let now = Date.now();
let messages = [];
emitter.on('foo', (arg) => messages.push(arg));
emitter.emit('foo', now);
assert.deepEqual([now], messages);
emitter.emit('foo', now + 15);
assert.deepEqual([now, now + 15], messages);
});
});
describe('#addListener()', function() {
it('can add multiple listeners for one event', function() {
let emitter = new EventEmitter;
let count = 0;
emitter.addListener('foo', () => count++);
emitter.addListener('foo', () => count++);
emitter.addListener('foo', () => count++);
emitter.emit('foo');
assert.equal(3, count);
});
it('only registers each listener function once', function() {
let emitter = new EventEmitter;
let count = 0;
let onFoo = () => count++;
emitter.addListener('foo', onFoo);
emitter.addListener('foo', onFoo);
emitter.addListener('foo', onFoo);
emitter.emit('foo');
assert.equal(1, count);
emitter.emit('foo');
assert.equal(2, count);
});
it('allows users to specify a custom scope', function() {
let obj = {
count: 0,
inc: function() {
this.count++;
}
};
let emitter = new EventEmitter;
emitter.addListener('foo', obj.inc, obj);
emitter.emit('foo');
assert.equal(1, obj.count);
emitter.emit('foo');
assert.equal(2, obj.count);
});
});
describe('#once()', function() {
it('only calls registered callback once', function() {
let emitter = new EventEmitter;
let count = 0;
emitter.once('foo', () => count++);
emitter.once('foo', () => count++);
emitter.once('foo', () => count++);
emitter.emit('foo');
assert.equal(3, count);
emitter.emit('foo');
assert.equal(3, count);
emitter.emit('foo');
assert.equal(3, count);
});
});
describe('#removeListeners()', function() {
it('only removes the given listener function', function() {
let emitter = new EventEmitter;
let count = 0;
emitter.addListener('foo', () => count++);
emitter.addListener('foo', () => count++);
let toRemove = () => count++;
emitter.addListener('foo', toRemove);
emitter.emit('foo');
assert.equal(3, count);
emitter.removeListener('foo', toRemove);
emitter.emit('foo');
assert.equal(5, count);
});
});
describe('#removeAllListeners()', function() {
it('only removes listeners for type if specified', function() {
let emitter = new EventEmitter;
let count = 0;
emitter.addListener('foo', () => count++);
emitter.addListener('foo', () => count++);
emitter.addListener('foo', () => count++);
emitter.addListener('bar', () => count++);
emitter.emit('foo');
assert.equal(3, count);
emitter.removeAllListeners('foo');
emitter.emit('foo');
assert.equal(3, count);
emitter.emit('bar');
assert.equal(4, count);
});
it('removes absolutely all listeners if no type specified', function() {
let emitter = new EventEmitter;
let count = 0;
emitter.addListener('foo', () => count++);
emitter.addListener('bar', () => count++);
emitter.addListener('baz', () => count++);
emitter.addListener('baz', () => count++);
emitter.emit('foo');
assert.equal(1, count);
emitter.emit('baz');
assert.equal(3, count);
emitter.removeAllListeners();
emitter.emit('foo');
assert.equal(3, count);
emitter.emit('bar');
assert.equal(3, count);
emitter.emit('baz');
assert.equal(3, count);
});
});
});

View file

@ -0,0 +1,272 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
'use strict';
const assert = require('assert');
const sinon = require('sinon');
const logging = require('../../lib/logging');
describe('logging', function() {
let mgr, root, clock;
beforeEach(function setUp() {
mgr = new logging.LogManager;
root = mgr.getLogger('');
clock = sinon.useFakeTimers();
});
afterEach(function tearDown() {
clock.restore();
});
describe('LogManager', function() {
describe('getLogger()', function() {
it('handles falsey input', function() {
assert.strictEqual(root, mgr.getLogger());
assert.strictEqual(root, mgr.getLogger(''));
assert.strictEqual(root, mgr.getLogger(null));
assert.strictEqual(root, mgr.getLogger(0));
});
it('creates parent loggers', function() {
let logger = mgr.getLogger('foo.bar.baz');
assert.strictEqual(logger.parent_, mgr.getLogger('foo.bar'));
logger = logger.parent_;
assert.strictEqual(logger.parent_, mgr.getLogger('foo'));
logger = logger.parent_;
assert.strictEqual(logger.parent_, mgr.getLogger(''));
assert.strictEqual(logger.parent_.parent_, null);
});
});
});
describe('Logger', function() {
describe('getEffectiveLevel()', function() {
it('defaults to OFF', function() {
assert.strictEqual(root.getLevel(), logging.Level.OFF);
assert.strictEqual(root.getEffectiveLevel(), logging.Level.OFF);
root.setLevel(null);
assert.strictEqual(root.getLevel(), null);
assert.strictEqual(root.getEffectiveLevel(), logging.Level.OFF);
});
it('uses own level if set', function() {
let logger = mgr.getLogger('foo.bar.baz');
assert.strictEqual(logger.getLevel(), null);
assert.strictEqual(logger.getEffectiveLevel(), logging.Level.OFF);
logger.setLevel(logging.Level.INFO);
assert.strictEqual(logger.getLevel(), logging.Level.INFO);
assert.strictEqual(logger.getEffectiveLevel(), logging.Level.INFO);
});
it('uses level from set on nearest parent', function() {
let ancestor = mgr.getLogger('foo');
ancestor.setLevel(logging.Level.SEVERE);
let logger = mgr.getLogger('foo.bar.baz');
assert.strictEqual(logger.getLevel(), null);
assert.strictEqual(logger.getEffectiveLevel(), logging.Level.SEVERE);
});
});
describe('isLoggable()', function() {
it('compares level against logger\'s effective level', function() {
const log1 = mgr.getLogger('foo');
log1.setLevel(logging.Level.WARNING);
const log2 = mgr.getLogger('foo.bar.baz');
assert(!log2.isLoggable(logging.Level.FINEST));
assert(!log2.isLoggable(logging.Level.INFO));
assert(log2.isLoggable(logging.Level.WARNING));
assert(log2.isLoggable(logging.Level.SEVERE));
log2.setLevel(logging.Level.INFO);
assert(!log2.isLoggable(logging.Level.FINEST));
assert(log2.isLoggable(logging.Level.INFO));
assert(log2.isLoggable(logging.Level.WARNING));
assert(log2.isLoggable(logging.Level.SEVERE));
log2.setLevel(logging.Level.ALL);
assert(log2.isLoggable(logging.Level.FINEST));
assert(log2.isLoggable(logging.Level.INFO));
assert(log2.isLoggable(logging.Level.WARNING));
assert(log2.isLoggable(logging.Level.SEVERE));
});
it('Level.OFF is never loggable', function() {
function test(level) {
root.setLevel(level);
assert(!root.isLoggable(logging.Level.OFF),
'OFF should not be loggable at ' + level);
}
test(logging.Level.ALL);
test(logging.Level.INFO);
test(logging.Level.OFF);
});
});
describe('log()', function() {
it('does not invoke loggable if message is not loggable', function() {
const log = mgr.getLogger('foo');
log.setLevel(logging.Level.OFF);
let callback = sinon.spy();
log.addHandler(callback);
root.addHandler(callback);
assert(!callback.called);
});
it('invokes handlers for each parent logger', function() {
const cb1 = sinon.spy();
const cb2 = sinon.spy();
const cb3 = sinon.spy();
const cb4 = sinon.spy();
const log1 = mgr.getLogger('foo');
const log2 = mgr.getLogger('foo.bar');
const log3 = mgr.getLogger('foo.bar.baz');
const log4 = mgr.getLogger('foo.bar.baz.quot');
log1.addHandler(cb1);
log1.setLevel(logging.Level.INFO);
log2.addHandler(cb2);
log2.setLevel(logging.Level.WARNING);
log3.addHandler(cb3);
log3.setLevel(logging.Level.FINER);
clock.tick(123456);
log4.finest('this is the finest message');
log4.finer('this is a finer message');
log4.info('this is an info message');
log4.warning('this is a warning message');
log4.severe('this is a severe message');
assert.equal(4, cb1.callCount);
assert.equal(4, cb2.callCount);
assert.equal(4, cb3.callCount);
const entry1 = new logging.Entry(
logging.Level.FINER,
'[foo.bar.baz.quot] this is a finer message',
123456);
const entry2 = new logging.Entry(
logging.Level.INFO,
'[foo.bar.baz.quot] this is an info message',
123456);
const entry3 = new logging.Entry(
logging.Level.WARNING,
'[foo.bar.baz.quot] this is a warning message',
123456);
const entry4 = new logging.Entry(
logging.Level.SEVERE,
'[foo.bar.baz.quot] this is a severe message',
123456);
check(cb1.getCall(0).args[0], entry1);
check(cb1.getCall(1).args[0], entry2);
check(cb1.getCall(2).args[0], entry3);
check(cb1.getCall(3).args[0], entry4);
check(cb2.getCall(0).args[0], entry1);
check(cb2.getCall(1).args[0], entry2);
check(cb2.getCall(2).args[0], entry3);
check(cb2.getCall(3).args[0], entry4);
check(cb3.getCall(0).args[0], entry1);
check(cb3.getCall(1).args[0], entry2);
check(cb3.getCall(2).args[0], entry3);
check(cb3.getCall(3).args[0], entry4);
function check(entry, expected) {
assert.equal(entry.level, expected.level, 'wrong level');
assert.equal(entry.message, expected.message, 'wrong message');
assert.equal(entry.timestamp, expected.timestamp, 'wrong time');
}
});
it('does not invoke removed handler', function() {
root.setLevel(logging.Level.INFO);
const cb = sinon.spy();
root.addHandler(cb);
root.info('hi');
assert.equal(1, cb.callCount);
assert(root.removeHandler(cb));
root.info('bye');
assert.equal(1, cb.callCount);
assert(!root.removeHandler(cb));
});
});
});
describe('getLevel()', function() {
it('converts named levels', function() {
assert.strictEqual(logging.Level.DEBUG, logging.getLevel('DEBUG'));
assert.strictEqual(logging.Level.ALL, logging.getLevel('FAKE'));
});
it('converts numeric levels', function() {
assert.strictEqual(
logging.Level.DEBUG,
logging.getLevel(logging.Level.DEBUG.value));
});
it('normalizes numeric levels', function() {
assert.strictEqual(
logging.Level.OFF,
logging.getLevel(logging.Level.OFF.value * 2));
let diff = logging.Level.SEVERE.value - logging.Level.WARNING.value;
assert.strictEqual(
logging.Level.WARNING,
logging.getLevel(logging.Level.WARNING.value + (diff * .5)));
assert.strictEqual(logging.Level.ALL, logging.getLevel(0));
assert.strictEqual(logging.Level.ALL, logging.getLevel(-1));
});
});
describe('Preferences', function() {
it('can be converted to JSON', function() {
let prefs = new logging.Preferences;
assert.equal('{}', JSON.stringify(prefs));
prefs.setLevel('foo', logging.Level.DEBUG);
assert.equal('{"foo":"DEBUG"}', JSON.stringify(prefs));
prefs.setLevel(logging.Type.BROWSER, logging.Level.FINE);
assert.equal('{"foo":"DEBUG","browser":"FINE"}', JSON.stringify(prefs));
});
});
});

View file

@ -0,0 +1,78 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
'use strict';
const promise = require('../../lib/promise');
const {enablePromiseManager} = require('../../lib/test/promise');
describe('Promises/A+ Compliance Tests', function() {
enablePromiseManager(() => {
// The promise spec does not define behavior for unhandled rejections and
// assumes they are effectively swallowed. This is not the case with our
// implementation, so we have to disable error propagation to test that the
// rest of our behavior is compliant.
// We run the tests with a separate instance of the control flow to ensure
// disablign error propagation does not impact other tests.
var flow = new promise.ControlFlow();
flow.setPropagateUnhandledRejections(false);
// Skip the tests in 2.2.6.1/2. We are not compliant in these scenarios.
var realDescribe = global.describe;
global.describe = function(name, fn) {
realDescribe(name, function() {
var prefix = 'Promises/A+ Compliance Tests '
+ 'SELENIUM_PROMISE_MANAGER=true 2.2.6: '
+ '`then` may be called multiple times on the same promise.';
var suffix = 'even when one handler is added inside another handler';
if (this.fullTitle().startsWith(prefix)
&& this.fullTitle().endsWith(suffix)) {
var realSpecify = global.specify;
try {
global.specify = function(name) {
realSpecify(name);
};
fn();
} finally {
global.specify = realSpecify;
}
} else {
fn();
}
});
};
require('promises-aplus-tests').mocha({
resolved: function(value) {
return new promise.Promise((fulfill) => fulfill(value), flow);
},
rejected: function(error) {
return new promise.Promise((_, reject) => reject(error), flow);
},
deferred: function() {
var d = new promise.Deferred(flow);
return {
resolve: d.fulfill,
reject: d.reject,
promise: d.promise
};
}
});
global.describe = realDescribe;
});
});

View file

@ -0,0 +1,884 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
/**
* @fileoverview Contains tests against promise error handling. Many tests use
* NativePromise to control test termination independent of promise
* (and notably promise.ControlFlow).
*/
'use strict';
const testutil = require('./testutil');
const assert = require('assert');
const promise = require('../../lib/promise');
const {enablePromiseManager} = require('../../lib/test/promise');
const NativePromise = Promise;
const StubError = testutil.StubError;
const throwStubError = testutil.throwStubError;
const assertIsStubError = testutil.assertIsStubError;
describe('promise error handling', function() {
enablePromiseManager(() => {
var flow, uncaughtExceptions;
beforeEach(function setUp() {
if (promise.USE_PROMISE_MANAGER) {
flow = promise.controlFlow();
uncaughtExceptions = [];
flow.on('uncaughtException', onUncaughtException);
}
});
afterEach(function tearDown() {
if (promise.USE_PROMISE_MANAGER) {
return waitForIdle(flow).then(function() {
assert.deepEqual(
[], uncaughtExceptions, 'There were uncaught exceptions');
flow.reset();
});
}
});
function onUncaughtException(e) {
uncaughtExceptions.push(e);
}
function waitForAbort(opt_flow, opt_n) {
var n = opt_n || 1;
var theFlow = opt_flow || flow;
theFlow.removeAllListeners(
promise.ControlFlow.EventType.UNCAUGHT_EXCEPTION);
return new NativePromise(function(fulfill, reject) {
theFlow.once('idle', function() {
reject(Error('expected flow to report an unhandled error'));
});
var errors = [];
theFlow.on('uncaughtException', onError);
function onError(e) {
errors.push(e);
if (errors.length === n) {
theFlow.removeListener('uncaughtException', onError);
fulfill(n === 1 ? errors[0] : errors);
}
}
});
}
function waitForIdle(opt_flow) {
var theFlow = opt_flow || flow;
return new NativePromise(function(fulfill, reject) {
if (theFlow.isIdle()) {
fulfill();
return;
}
theFlow.once('idle', fulfill);
theFlow.once('uncaughtException', reject);
});
}
it('testRejectedPromiseTriggersErrorCallback', function() {
return promise.rejected(new StubError).
then(assert.fail, assertIsStubError);
});
describe('callback throws trigger subsequent error callback', function() {
it('fulfilled promise', function() {
return promise.fulfilled().
then(throwStubError).
then(assert.fail, assertIsStubError);
});
it('rejected promise', function() {
var e = Error('not the droids you are looking for');
return promise.rejected(e).
then(assert.fail, throwStubError).
then(assert.fail, assertIsStubError);
});
});
describe('callback returns rejected promise triggers subsequent errback', function() {
it('from fulfilled callback', function() {
return promise.fulfilled().then(function() {
return promise.rejected(new StubError);
}).then(assert.fail, assertIsStubError);
});
it('from rejected callback', function() {
var e = Error('not the droids you are looking for');
return promise.rejected(e).
then(assert.fail, function() {
return promise.rejected(new StubError);
}).
then(assert.fail, assertIsStubError);
});
});
it('testReportsUnhandledRejectionsThroughTheControlFlow', function() {
promise.rejected(new StubError);
return waitForAbort().then(assertIsStubError);
});
describe('multiple unhandled rejections outside a task', function() {
it('are reported in order they occurred', function() {
var e1 = Error('error 1');
var e2 = Error('error 2');
promise.rejected(e1);
promise.rejected(e2);
return waitForAbort(flow).then(function(error) {
assert.ok(
error instanceof promise.MultipleUnhandledRejectionError);
// TODO: switch to Array.from when we drop node 0.12.x
var errors = [];
for (var e of error.errors) {
errors.push(e);
}
assert.deepEqual([e1, e2], errors);
});
});
});
describe('does not report unhandled rejection when', function() {
it('handler added before next tick', function() {
promise.rejected(new StubError).then(assert.fail, assertIsStubError);
return waitForIdle();
});
it('added async but before next tick', function() {
var called = false;
return new NativePromise(function(fulfill, reject) {
var aPromise;
NativePromise.resolve().then(function() {
aPromise.then(assert.fail, function(e) {
called = true;
assertIsStubError(e);
});
waitForIdle().then(fulfill, reject);
});
aPromise = promise.rejected(new StubError);
}).then(function() {
assert.ok(called);
})
});
});
it('testTaskThrows', function() {
return flow.execute(throwStubError).then(assert.fail, assertIsStubError);
});
it('testTaskReturnsRejectedPromise', function() {
return flow.execute(function() {
return promise.rejected(new StubError)
}).then(assert.fail, assertIsStubError);
});
it('testTaskHasUnhandledRejection', function() {
return flow.execute(function() {
promise.rejected(new StubError)
}).then(assert.fail, assertIsStubError);
});
it('testTaskfails_returnedPromiseIsUnhandled', function() {
flow.execute(throwStubError);
return waitForAbort().then(assertIsStubError);
});
it('testTaskHasUnhandledRejection_cancelsRemainingSubTasks', function() {
var seen = [];
flow.execute(function() {
promise.rejected(new StubError);
flow.execute(() => seen.push('a'))
.then(() => seen.push('b'), (e) => seen.push(e));
flow.execute(() => seen.push('c'))
.then(() => seen.push('b'), (e) => seen.push(e));
});
return waitForAbort()
.then(assertIsStubError)
.then(() => assert.deepEqual([], seen));
});
describe('nested task failures', function() {
it('returns up to paren', function() {
return flow.execute(function() {
return flow.execute(function() {
return flow.execute(throwStubError);
});
}).then(assert.fail, assertIsStubError);
});
it('task throws; uncaught error bubbles up', function() {
flow.execute(function() {
flow.execute(function() {
flow.execute(throwStubError);
});
});
return waitForAbort().then(assertIsStubError);
});
it('task throws; uncaught error bubbles up; is caught at root', function() {
flow.execute(function() {
flow.execute(function() {
flow.execute(throwStubError);
});
}).then(assert.fail, assertIsStubError);
return waitForIdle();
});
it('unhandled rejection bubbles up', function() {
flow.execute(function() {
flow.execute(function() {
flow.execute(function() {
promise.rejected(new StubError);
});
});
});
return waitForAbort().then(assertIsStubError);
});
it('unhandled rejection bubbles up; caught at root', function() {
flow.execute(function() {
flow.execute(function() {
promise.rejected(new StubError);
});
}).then(assert.fail, assertIsStubError);
return waitForIdle();
});
it('mixtureof hanging and free subtasks', function() {
flow.execute(function() {
return flow.execute(function() {
flow.execute(throwStubError);
});
});
return waitForAbort().then(assertIsStubError);
});
it('cancels remaining tasks', function() {
var seen = [];
flow.execute(function() {
flow.execute(() => promise.rejected(new StubError));
flow.execute(() => seen.push('a'))
.then(() => seen.push('b'), (e) => seen.push(e));
flow.execute(() => seen.push('c'))
.then(() => seen.push('b'), (e) => seen.push(e));
});
return waitForAbort()
.then(assertIsStubError)
.then(() => assert.deepEqual([], seen));
});
});
it('testTaskReturnsPromiseLikeObjectThatInvokesErrback', function() {
return flow.execute(function() {
return {
'then': function(_, errback) {
errback('abc123');
}
};
}).then(assert.fail, function(value) {
assert.equal('abc123', value);
});
});
describe('ControlFlow#wait();', function() {
describe('condition throws;', function() {
it('failure is caught', function() {
return flow.wait(throwStubError, 50).then(assert.fail, assertIsStubError);
});
it('failure is not caught', function() {
flow.wait(throwStubError, 50);
return waitForAbort().then(assertIsStubError);
});
});
describe('condition returns promise', function() {
it('failure is caught', function() {
return flow.wait(function() {
return promise.rejected(new StubError);
}, 50).then(assert.fail, assertIsStubError);
});
it('failure is not caught', function() {
flow.wait(function() {
return promise.rejected(new StubError);
}, 50);
return waitForAbort().then(assertIsStubError);
});
});
describe('condition has unhandled promise rejection', function() {
it('failure is caught', function() {
return flow.wait(function() {
promise.rejected(new StubError);
}, 50).then(assert.fail, assertIsStubError);
});
it('failure is not caught', function() {
flow.wait(function() {
promise.rejected(new StubError);
}, 50);
return waitForAbort().then(assertIsStubError);
});
});
describe('condition has subtask failure', function() {
it('failure is caught', function() {
return flow.wait(function() {
flow.execute(function() {
flow.execute(throwStubError);
});
}, 50).then(assert.fail, assertIsStubError);
});
it('failure is not caught', function() {
flow.wait(function() {
flow.execute(function() {
flow.execute(throwStubError);
});
}, 50);
return waitForAbort().then(assertIsStubError);
});
});
});
describe('errback throws a new error', function() {
it('start with normal promise', function() {
var error = Error('an error');
return promise.rejected(error).
catch(function(e) {
assert.equal(e, error);
throw new StubError;
}).
catch(assertIsStubError);
});
it('start with task result', function() {
var error = Error('an error');
return flow.execute(function() {
throw error;
}).
catch(function(e) {
assert.equal(e, error);
throw new StubError;
}).
catch(assertIsStubError);
});
it('start with normal promise; uncaught error', function() {
var error = Error('an error');
promise.rejected(error).
catch(function(e) {
assert.equal(e, error);
throw new StubError;
});
return waitForAbort().then(assertIsStubError);
});
it('start with task result; uncaught error', function() {
var error = Error('an error');
flow.execute(function() {
throw error;
}).
catch(function(e) {
assert.equal(e, error);
throw new StubError;
});
return waitForAbort().then(assertIsStubError);
});
});
it('thrownPromiseCausesCallbackRejection', function() {
let p = promise.fulfilled(1234);
return promise.fulfilled().then(function() {
throw p;
}).then(assert.fail, function(value) {
assert.strictEqual(p, value);
});
});
describe('task throws promise', function() {
it('promise was fulfilled', function() {
var toThrow = promise.fulfilled(1234);
flow.execute(function() {
throw toThrow;
}).then(assert.fail, function(value) {
assert.equal(toThrow, value);
return toThrow;
}).then(function(value) {
assert.equal(1234, value);
});
return waitForIdle();
});
it('promise was rejected', function() {
var toThrow = promise.rejected(new StubError);
toThrow.catch(function() {}); // For tearDown.
flow.execute(function() {
throw toThrow;
}).then(assert.fail, function(e) {
assert.equal(toThrow, e);
return e;
}).then(assert.fail, assertIsStubError);
return waitForIdle();
});
});
it('testFailsTaskIfThereIsAnUnhandledErrorWhileWaitingOnTaskResult', function() {
var d = promise.defer();
flow.execute(function() {
promise.rejected(new StubError);
return d.promise;
}).then(assert.fail, assertIsStubError);
return waitForIdle().then(function() {
return d.promise;
}).then(assert.fail, function(e) {
assert.equal('CancellationError: StubError', e.toString());
});
});
it('testFailsParentTaskIfAsyncScheduledTaskFails', function() {
var d = promise.defer();
flow.execute(function() {
flow.execute(throwStubError);
return d.promise;
}).then(assert.fail, assertIsStubError);
return waitForIdle().then(function() {
return d.promise;
}).then(assert.fail, function(e) {
assert.equal('CancellationError: StubError', e.toString());
});
});
describe('long stack traces', function() {
afterEach(() => promise.LONG_STACK_TRACES = false);
it('always includes task stacks in failures', function() {
promise.LONG_STACK_TRACES = false;
flow.execute(function() {
flow.execute(function() {
flow.execute(throwStubError, 'throw error');
}, 'two');
}, 'three').
then(assert.fail, function(e) {
assertIsStubError(e);
if (typeof e.stack !== 'string') {
return;
}
var messages = e.stack.split(/\n/).filter(function(line, index) {
return /^From: /.test(line);
});
assert.deepEqual([
'From: Task: throw error',
'From: Task: two',
'From: Task: three'
], messages);
});
return waitForIdle();
});
it('does not include completed tasks', function () {
flow.execute(function() {}, 'succeeds');
flow.execute(throwStubError, 'kaboom').then(assert.fail, function(e) {
assertIsStubError(e);
if (typeof e.stack !== 'string') {
return;
}
var messages = e.stack.split(/\n/).filter(function(line, index) {
return /^From: /.test(line);
});
assert.deepEqual(['From: Task: kaboom'], messages);
});
return waitForIdle();
});
it('does not include promise chain when disabled', function() {
promise.LONG_STACK_TRACES = false;
flow.execute(function() {
flow.execute(function() {
return promise.fulfilled().
then(function() {}).
then(function() {}).
then(throwStubError);
}, 'eventually assert.fails');
}, 'start').
then(assert.fail, function(e) {
assertIsStubError(e);
if (typeof e.stack !== 'string') {
return;
}
var messages = e.stack.split(/\n/).filter(function(line, index) {
return /^From: /.test(line);
});
assert.deepEqual([
'From: Task: eventually assert.fails',
'From: Task: start'
], messages);
});
return waitForIdle();
});
it('includes promise chain when enabled', function() {
promise.LONG_STACK_TRACES = true;
flow.execute(function() {
flow.execute(function() {
return promise.fulfilled().
then(function() {}).
then(function() {}).
then(throwStubError);
}, 'eventually assert.fails');
}, 'start').
then(assert.fail, function(e) {
assertIsStubError(e);
if (typeof e.stack !== 'string') {
return;
}
var messages = e.stack.split(/\n/).filter(function(line, index) {
return /^From: /.test(line);
});
assert.deepEqual([
'From: Promise: then',
'From: Task: eventually assert.fails',
'From: Task: start'
], messages);
});
return waitForIdle();
});
});
describe('frame cancels remaining tasks', function() {
it('on unhandled task failure', function() {
var run = false;
return flow.execute(function() {
flow.execute(throwStubError);
flow.execute(function() { run = true; });
}).then(assert.fail, function(e) {
assertIsStubError(e);
assert.ok(!run);
});
});
it('on unhandled promise rejection', function() {
var run = false;
return flow.execute(function() {
promise.rejected(new StubError);
flow.execute(function() { run = true; });
}).then(assert.fail, function(e) {
assertIsStubError(e);
assert.ok(!run);
});
});
it('if task throws', function() {
var run = false;
return flow.execute(function() {
flow.execute(function() { run = true; });
throw new StubError;
}).then(assert.fail, function(e) {
assertIsStubError(e);
assert.ok(!run);
});
});
describe('task callbacks scheduled in another frame', function() {
flow = promise.controlFlow();
function noop() {}
let subTask;
before(function() {
flow.execute(function() {
// This task will be discarded and never run because of the error below.
subTask = flow.execute(() => 'abc');
throw new StubError('stub');
}).catch(noop);
});
function assertCancellation(e) {
assert.ok(e instanceof promise.CancellationError);
assert.equal(
'Task was discarded due to a previous failure: stub', e.message);
}
it('are rejected with cancellation error', function() {
let result;
return Promise.resolve().then(function() {
return flow.execute(function() {
result = subTask.then(assert.fail);
});
})
.then(() => result)
.then(assert.fail, assertCancellation);
});
it('cancellation errors propagate through callbacks (1)', function() {
let result;
return Promise.resolve().then(function() {
return flow.execute(function() {
result = subTask
.then(assert.fail, assertCancellation)
.then(() => 'abc123');
});
})
.then(() => result)
.then(value => assert.equal('abc123', value));
});
it('cancellation errors propagate through callbacks (2)', function() {
let result;
return Promise.resolve().then(function() {
return flow.execute(function() {
result = subTask.then(assert.fail)
.then(noop, assertCancellation)
.then(() => 'fin');
});
})
// Verify result actually computed successfully all the way through.
.then(() => result)
.then(value => assert.equal('fin', value));
});
});
});
it('testRegisteredTaskCallbacksAreDroppedWhenTaskIsCancelled_return', function() {
var seen = [];
return flow.execute(function() {
flow.execute(throwStubError);
flow.execute(function() {
seen.push(1);
}).then(function() {
seen.push(2);
}, function() {
seen.push(3);
});
}).then(assert.fail, function(e) {
assertIsStubError(e);
assert.deepEqual([], seen);
});
});
it('testRegisteredTaskCallbacksAreDroppedWhenTaskIsCancelled_withReturn', function() {
var seen = [];
return flow.execute(function() {
flow.execute(throwStubError);
return flow.execute(function() {
seen.push(1);
}).then(function() {
seen.push(2);
}, function() {
seen.push(3);
});
}).then(assert.fail, function(e) {
assertIsStubError(e);
assert.deepEqual([], seen);
});
});
it('testTasksWithinACallbackAreDroppedIfContainingTaskIsAborted', function() {
var seen = [];
return flow.execute(function() {
flow.execute(throwStubError);
// None of the callbacks on this promise should execute because the
// task assert.failure above is never handled, causing the containing task to
// abort.
promise.fulfilled().then(function() {
seen.push(1);
return flow.execute(function() {
seen.push(2);
});
}).finally(function() {
seen.push(3);
});
}).then(assert.fail, function(e) {
assertIsStubError(e);
assert.deepEqual([], seen);
});
});
it('testTaskIsCancelledAfterWaitTimeout', function() {
var seen = [];
return flow.execute(function() {
flow.wait(function() {
return promise.delayed(50);
}, 5);
return flow.execute(function() {
seen.push(1);
}).then(function() {
seen.push(2);
}, function() {
seen.push(3);
});
}).then(assert.fail, function() {
assert.deepEqual([], seen);
});
});
describe('task callbacks get cancellation error if registered after task was cancelled', function() {
it('(a)', function() {
var task;
flow.execute(function() {
flow.execute(throwStubError);
task = flow.execute(function() {});
}).then(assert.fail, assertIsStubError);
return waitForIdle().then(function() {
return task.then(assert.fail, function(e) {
assert.ok(e instanceof promise.CancellationError);
});
});
});
it('(b)', function() {
var seen = [];
var task;
flow.execute(function() {
flow.execute(throwStubError);
task = flow.execute(function() {});
task.then(() => seen.push(1))
.then(() => seen.push(2));
task.then(() => seen.push(3))
.then(() => seen.push(4));
}).then(assert.fail, assertIsStubError);
return waitForIdle().then(function() {
return task.then(assert.fail, function(e) {
seen.push(5);
assert.ok(e instanceof promise.CancellationError);
});
}).then(() => assert.deepEqual([5], seen));
});
});
it('unhandledRejectionInParallelTaskQueue', function() {
var seen = [];
function schedule(name) {
return flow.execute(() => seen.push(name), name);
}
flow.async(function() {
schedule('a.1');
flow.execute(throwStubError, 'a.2 (throws)');
});
var b3;
flow.async(function() {
schedule('b.1');
schedule('b.2');
b3 = schedule('b.3');
});
var c3;
flow.async(function() {
schedule('c.1');
schedule('c.2');
c3 = schedule('c.3');
});
function assertWasCancelled(p) {
return p.then(assert.fail, function(e) {
assert.ok(e instanceof promise.CancellationError);
});
}
return waitForAbort()
.then(function() {
assert.deepEqual(['a.1', 'b.1', 'c.1', 'b.2', 'c.2'], seen);
})
.then(() => assertWasCancelled(b3))
.then(() => assertWasCancelled(c3));
});
it('errorsInAsyncFunctionsAreReportedAsUnhandledRejection', function() {
flow.removeAllListeners(); // For tearDown.
var task;
return new Promise(function(fulfill) {
flow.once('uncaughtException', fulfill);
flow.async(function() {
task = flow.execute(function() {});
throw Error('boom');
});
}).then(function(error) {
assert.ok(error instanceof promise.CancellationError);
return task.catch(function(error) {
assert.ok(error instanceof promise.CancellationError);
});
});
});
describe('does not wait for values thrown from callbacks to be resolved', function() {
it('(a)', function() {
var p1 = promise.fulfilled();
var reason = promise.fulfilled('should not see me');
return p1.then(function() {
throw reason;
}).then(assert.fail, function(e) {
assert.equal(reason, e);
});
});
it('(b)', function() {
var p1 = promise.fulfilled();
var reason = promise.rejected('should not see me');
reason.catch(function() {}); // For tearDown.
return p1.then(function() {
throw reason;
}).then(assert.fail, function(e) {
assert.equal(reason, e);
});
});
it('(c)', function() {
var p1 = promise.fulfilled();
var reason = promise.defer();
setTimeout(() => reason.fulfill('should not see me'), 100);
return p1.then(function() {
throw reason.promise;
}).then(assert.fail, function(e) {
assert.equal(reason.promise, e);
});
});
it('(d)', function() {
var p1 = promise.fulfilled();
var reason = {then: function() {}}; // A thenable like object.
return p1.then(function() {
throw reason;
}).then(assert.fail, function(e) {
assert.equal(reason, e);
});
});
});
});
});

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,310 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
'use strict';
const assert = require('assert');
const promise = require('../../lib/promise');
const {enablePromiseManager, promiseManagerSuite} = require('../../lib/test/promise');
describe('promise.consume()', function() {
promiseManagerSuite(() => {
it('requires inputs to be generator functions', function() {
assert.throws(function() {
promise.consume(function() {});
});
});
it('handles a basic generator with no yielded promises', function() {
var values = [];
return promise.consume(function* () {
var i = 0;
while (i < 4) {
i = yield i + 1;
values.push(i);
}
}).then(function() {
assert.deepEqual([1, 2, 3, 4], values);
});
});
it('handles a promise yielding generator', function() {
var values = [];
return promise.consume(function* () {
var i = 0;
while (i < 4) {
// Test that things are actually async here.
setTimeout(function() {
values.push(i * 2);
}, 10);
yield promise.delayed(10).then(function() {
values.push(i++);
});
}
}).then(function() {
assert.deepEqual([0, 0, 2, 1, 4, 2, 6, 3], values);
});
});
it('assignments to yielded promises get fulfilled value', function() {
return promise.consume(function* () {
let x = yield Promise.resolve(2);
assert.equal(2, x);
});
});
it('uses final return value as fulfillment value', function() {
return promise.consume(function* () {
yield 1;
yield 2;
return 3;
}).then(function(value) {
assert.equal(3, value);
});
});
it('throws rejected promise errors within the generator', function() {
var values = [];
return promise.consume(function* () {
values.push('a');
var e = Error('stub error');
try {
yield Promise.reject(e);
values.push('b');
} catch (ex) {
assert.equal(e, ex);
values.push('c');
}
values.push('d');
}).then(function() {
assert.deepEqual(['a', 'c', 'd'], values);
});
});
it('aborts the generator if there is an unhandled rejection', function() {
var values = [];
var e = Error('stub error');
return promise.consume(function* () {
values.push(1);
yield promise.rejected(e);
values.push(2);
}).catch(function() {
assert.deepEqual([1], values);
});
});
it('yield waits for promises', function() {
let values = [];
let blocker = promise.delayed(100).then(() => {
assert.deepEqual([1], values);
return 2;
});
return promise.consume(function* () {
values.push(1);
values.push(yield blocker, 3);
}).then(function() {
assert.deepEqual([1, 2, 3], values);
});
});
it('accepts custom scopes', function() {
return promise.consume(function* () {
return this.name;
}, {name: 'Bob'}).then(function(value) {
assert.equal('Bob', value);
});
});
it('accepts initial generator arguments', function() {
return promise.consume(function* (a, b) {
assert.equal('red', a);
assert.equal('apples', b);
}, null, 'red', 'apples');
});
});
enablePromiseManager(() => {
it('is possible to cancel promise generators', function() {
var values = [];
var p = promise.consume(function* () {
var i = 0;
while (i < 3) {
yield promise.delayed(100).then(function() {
values.push(i++);
});
}
});
return promise.delayed(75).then(function() {
p.cancel();
return p.catch(function() {
return promise.delayed(300);
});
}).then(function() {
assert.deepEqual([0], values);
});
});
it('executes generator within the control flow', function() {
var promises = [
promise.defer(),
promise.defer()
];
var values = [];
setTimeout(function() {
assert.deepEqual([], values);
promises[0].fulfill(1);
}, 100);
setTimeout(function() {
assert.deepEqual([1], values);
promises[1].fulfill(2);
}, 200);
return promise.controlFlow().execute(function* () {
values.push(yield promises[0].promise);
values.push(yield promises[1].promise);
values.push('fin');
}).then(function() {
assert.deepEqual([1, 2, 'fin'], values);
});
});
it('handles tasks scheduled in generator', function() {
var flow = promise.controlFlow();
return flow.execute(function* () {
var x = yield flow.execute(function() {
return promise.delayed(10).then(function() {
return 1;
});
});
var y = yield flow.execute(function() {
return 2;
});
return x + y;
}).then(function(value) {
assert.equal(3, value);
});
});
it('blocks the control flow while processing generator', function() {
var values = [];
return promise.controlFlow().wait(function* () {
yield values.push(1);
values.push(yield promise.delayed(10).then(function() {
return 2;
}));
yield values.push(3);
return values.length === 6;
}, 250).then(function() {
assert.deepEqual([1, 2, 3, 1, 2, 3], values);
});
});
it('ControlFlow.wait() will timeout on long generator', function() {
var values = [];
return promise.controlFlow().wait(function* () {
var i = 0;
while (i < 3) {
yield promise.delayed(100).then(function() {
values.push(i++);
});
}
}, 75).catch(function() {
assert.deepEqual(
[0, 1, 2], values, 'Should complete one loop of wait condition');
});
});
describe('generators in promise callbacks', function() {
it('works with no initial value', function() {
var promises = [
promise.defer(),
promise.defer()
];
var values = [];
setTimeout(function() {
promises[0].fulfill(1);
}, 50);
setTimeout(function() {
promises[1].fulfill(2);
}, 100);
return promise.fulfilled().then(function*() {
values.push(yield promises[0].promise);
values.push(yield promises[1].promise);
values.push('fin');
}).then(function() {
assert.deepEqual([1, 2, 'fin'], values);
});
});
it('starts the generator with promised value', function() {
var promises = [
promise.defer(),
promise.defer()
];
var values = [];
setTimeout(function() {
promises[0].fulfill(1);
}, 50);
setTimeout(function() {
promises[1].fulfill(2);
}, 100);
return promise.fulfilled(3).then(function*(value) {
var p1 = yield promises[0].promise;
var p2 = yield promises[1].promise;
values.push(value + p1);
values.push(value + p2);
values.push('fin');
}).then(function() {
assert.deepEqual([4, 5, 'fin'], values);
});
});
it('throws yielded rejections within the generator callback', function() {
var d = promise.defer();
var e = Error('stub');
setTimeout(function() {
d.reject(e);
}, 50);
return promise.fulfilled().then(function*() {
var threw = false;
try {
yield d.promise;
} catch (ex) {
threw = true;
assert.equal(e, ex);
}
assert.ok(threw);
});
});
});
});
});

1109
node_modules/selenium-webdriver/test/lib/promise_test.js generated vendored Normal file

File diff suppressed because it is too large Load diff

90
node_modules/selenium-webdriver/test/lib/testutil.js generated vendored Normal file
View file

@ -0,0 +1,90 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
'use strict';
const assert = require('assert');
const sinon = require('sinon');
class StubError extends Error {
constructor(opt_msg) {
super(opt_msg);
this.name = this.constructor.name;
}
}
exports.StubError = StubError;
exports.throwStubError = function() {
throw new StubError;
};
exports.assertIsStubError = function(value) {
assert.ok(value instanceof StubError, value + ' is not a ' + StubError.name);
};
exports.assertIsInstance = function(ctor, value) {
assert.ok(value instanceof ctor, 'Not a ' + ctor.name + ': ' + value);
};
function callbackPair(cb, eb) {
if (cb && eb) {
throw new TypeError('can only specify one of callback or errback');
}
let callback = cb ? sinon.spy(cb) : sinon.spy();
let errback = eb ? sinon.spy(eb) : sinon.spy();
function assertCallback() {
assert.ok(callback.called, 'callback not called');
assert.ok(!errback.called, 'errback called');
if (callback.threw()) {
throw callback.exceptions[0];
}
}
function assertErrback() {
assert.ok(!callback.called, 'callback called');
assert.ok(errback.called, 'errback not called');
if (errback.threw()) {
throw errback.exceptions[0];
}
}
function assertNeither() {
assert.ok(!callback.called, 'callback called');
assert.ok(!errback.called, 'errback called');
}
return {
callback,
errback,
assertCallback,
assertErrback,
assertNeither
};
}
exports.callbackPair = callbackPair;
exports.callbackHelper = function(cb) {
let pair = callbackPair(cb);
let wrapped = pair.callback.bind(null);
wrapped.assertCalled = () => pair.assertCallback();
wrapped.assertNotCalled = () => pair.assertNeither();
return wrapped;
};

478
node_modules/selenium-webdriver/test/lib/until_test.js generated vendored Normal file
View file

@ -0,0 +1,478 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
'use strict';
const assert = require('assert');
const By = require('../../lib/by').By;
const CommandName = require('../../lib/command').Name;
const error = require('../../lib/error');
const promise = require('../../lib/promise');
const until = require('../../lib/until');
const webdriver = require('../../lib/webdriver'),
WebElement = webdriver.WebElement;
describe('until', function() {
let driver, executor;
class TestExecutor {
constructor() {
this.handlers_ = {};
}
on(cmd, handler) {
this.handlers_[cmd] = handler;
return this;
}
execute(cmd) {
let self = this;
return Promise.resolve().then(function() {
if (!self.handlers_[cmd.getName()]) {
throw new error.UnknownCommandError(cmd.getName());
}
return self.handlers_[cmd.getName()](cmd);
});
}
}
function fail(opt_msg) {
throw new assert.AssertionError({message: opt_msg});
}
beforeEach(function setUp() {
executor = new TestExecutor();
driver = new webdriver.WebDriver('session-id', executor);
});
describe('ableToSwitchToFrame', function() {
it('failsFastForNonSwitchErrors', function() {
let e = Error('boom');
executor.on(CommandName.SWITCH_TO_FRAME, function() {
throw e;
});
return driver.wait(until.ableToSwitchToFrame(0), 100)
.then(fail, (e2) => assert.strictEqual(e2, e));
});
const ELEMENT_ID = 'some-element-id';
const ELEMENT_INDEX = 1234;
function onSwitchFrame(expectedId) {
if (typeof expectedId === 'string') {
expectedId = WebElement.buildId(expectedId);
} else {
assert.equal(typeof expectedId, 'number', 'must be string or number');
}
return cmd => {
assert.deepEqual(
cmd.getParameter('id'), expectedId, 'frame ID not specified');
return true;
};
}
it('byIndex', function() {
executor.on(CommandName.SWITCH_TO_FRAME, onSwitchFrame(ELEMENT_INDEX));
return driver.wait(until.ableToSwitchToFrame(ELEMENT_INDEX), 100);
});
it('byWebElement', function() {
executor.on(CommandName.SWITCH_TO_FRAME, onSwitchFrame(ELEMENT_ID));
var el = new webdriver.WebElement(driver, ELEMENT_ID);
return driver.wait(until.ableToSwitchToFrame(el), 100);
});
it('byWebElementPromise', function() {
executor.on(CommandName.SWITCH_TO_FRAME, onSwitchFrame(ELEMENT_ID));
var el = new webdriver.WebElementPromise(driver,
Promise.resolve(new webdriver.WebElement(driver, ELEMENT_ID)));
return driver.wait(until.ableToSwitchToFrame(el), 100);
});
it('byLocator', function() {
executor.on(CommandName.FIND_ELEMENTS, () => [WebElement.buildId(ELEMENT_ID)]);
executor.on(CommandName.SWITCH_TO_FRAME, onSwitchFrame(ELEMENT_ID));
return driver.wait(until.ableToSwitchToFrame(By.id('foo')), 100);
});
it('byLocator_elementNotInitiallyFound', function() {
let foundResponses = [[], [], [WebElement.buildId(ELEMENT_ID)]];
executor.on(CommandName.FIND_ELEMENTS, () => foundResponses.shift());
executor.on(CommandName.SWITCH_TO_FRAME, onSwitchFrame(ELEMENT_ID));
return driver.wait(until.ableToSwitchToFrame(By.id('foo')), 2000)
.then(() => assert.deepEqual(foundResponses, []));
});
it('timesOutIfNeverAbletoSwitchFrames', function() {
var count = 0;
executor.on(CommandName.SWITCH_TO_FRAME, function() {
count += 1;
throw new error.NoSuchFrameError;
});
return driver.wait(until.ableToSwitchToFrame(0), 100)
.then(fail, function(e) {
assert.ok(count > 0);
assert.ok(
e.message.startsWith('Waiting to be able to switch to frame'),
'Wrong message: ' + e.message);
});
});
});
describe('alertIsPresent', function() {
it('failsFastForNonAlertSwitchErrors', function() {
return driver.wait(until.alertIsPresent(), 100).then(fail, function(e) {
assert.ok(e instanceof error.UnknownCommandError);
assert.equal(e.message, CommandName.GET_ALERT_TEXT);
});
});
it('waitsForAlert', function() {
var count = 0;
executor.on(CommandName.GET_ALERT_TEXT, function() {
if (count++ < 3) {
throw new error.NoSuchAlertError;
} else {
return true;
}
}).on(CommandName.DISMISS_ALERT, () => true);
return driver.wait(until.alertIsPresent(), 1000).then(function(alert) {
assert.equal(count, 4);
return alert.dismiss();
});
});
// TODO: Remove once GeckoDriver doesn't throw this unwanted error.
// See https://github.com/SeleniumHQ/selenium/pull/2137
describe('workaround for GeckoDriver', function() {
it('doesNotFailWhenCannotConvertNullToObject', function() {
var count = 0;
executor.on(CommandName.GET_ALERT_TEXT, function() {
if (count++ < 3) {
throw new error.WebDriverError(`can't convert null to object`);
} else {
return true;
}
}).on(CommandName.DISMISS_ALERT, () => true);
return driver.wait(until.alertIsPresent(), 1000).then(function(alert) {
assert.equal(count, 4);
return alert.dismiss();
});
});
it('keepsRaisingRegularWebdriverError', function() {
var webDriverError = new error.WebDriverError;
executor.on(CommandName.GET_ALERT_TEXT, function() {
throw webDriverError;
});
return driver.wait(until.alertIsPresent(), 1000).then(function() {
throw new Error('driver did not fail against WebDriverError');
}, function(error) {
assert.equal(error, webDriverError);
});
})
});
});
it('testUntilTitleIs', function() {
var titles = ['foo', 'bar', 'baz'];
executor.on(CommandName.GET_TITLE, () => titles.shift());
return driver.wait(until.titleIs('bar'), 3000).then(function() {
assert.deepStrictEqual(titles, ['baz']);
});
});
it('testUntilTitleContains', function() {
var titles = ['foo', 'froogle', 'google'];
executor.on(CommandName.GET_TITLE, () => titles.shift());
return driver.wait(until.titleContains('oogle'), 3000).then(function() {
assert.deepStrictEqual(titles, ['google']);
});
});
it('testUntilTitleMatches', function() {
var titles = ['foo', 'froogle', 'aaaabc', 'aabbbc', 'google'];
executor.on(CommandName.GET_TITLE, () => titles.shift());
return driver.wait(until.titleMatches(/^a{2,3}b+c$/), 3000)
.then(function() {
assert.deepStrictEqual(titles, ['google']);
});
});
it('testUntilUrlIs', function() {
var urls = ['http://www.foo.com', 'https://boo.com', 'http://docs.yes.com'];
executor.on(CommandName.GET_CURRENT_URL, () => urls.shift());
return driver.wait(until.urlIs('https://boo.com'), 3000).then(function() {
assert.deepStrictEqual(urls, ['http://docs.yes.com']);
});
});
it('testUntilUrlContains', function() {
var urls =
['http://foo.com', 'https://groups.froogle.com', 'http://google.com'];
executor.on(CommandName.GET_CURRENT_URL, () => urls.shift());
return driver.wait(until.urlContains('oogle.com'), 3000).then(function() {
assert.deepStrictEqual(urls, ['http://google.com']);
});
});
it('testUntilUrlMatches', function() {
var urls = ['foo', 'froogle', 'aaaabc', 'aabbbc', 'google'];
executor.on(CommandName.GET_CURRENT_URL, () => urls.shift());
return driver.wait(until.urlMatches(/^a{2,3}b+c$/), 3000)
.then(function() {
assert.deepStrictEqual(urls, ['google']);
});
});
it('testUntilElementLocated', function() {
var responses = [
[],
[WebElement.buildId('abc123'), WebElement.buildId('foo')],
['end']
];
executor.on(CommandName.FIND_ELEMENTS, () => responses.shift());
let element = driver.wait(until.elementLocated(By.id('quux')), 2000);
assert.ok(element instanceof webdriver.WebElementPromise);
return element.getId().then(function(id) {
assert.deepStrictEqual(responses, [['end']]);
assert.equal(id, 'abc123');
});
});
describe('untilElementLocated, elementNeverFound', function() {
function runNoElementFoundTest(locator, locatorStr) {
executor.on(CommandName.FIND_ELEMENTS, () => []);
function expectedFailure() {
fail('expected condition to timeout');
}
return driver.wait(until.elementLocated(locator), 100)
.then(expectedFailure, function(error) {
var expected = 'Waiting for element to be located ' + locatorStr;
var lines = error.message.split(/\n/, 2);
assert.equal(lines[0], expected);
let regex = /^Wait timed out after \d+ms$/;
assert.ok(regex.test(lines[1]),
`Lines <${lines[1]}> does not match ${regex}`);
});
}
it('byLocator', function() {
return runNoElementFoundTest(
By.id('quux'), 'By(css selector, *[id="quux"])');
});
it('byHash', function() {
return runNoElementFoundTest(
{id: 'quux'}, 'By(css selector, *[id="quux"])');
});
it('byFunction', function() {
return runNoElementFoundTest(function() {}, 'by function()');
});
});
it('testUntilElementsLocated', function() {
var responses = [
[],
[WebElement.buildId('abc123'), WebElement.buildId('foo')],
['end']
];
executor.on(CommandName.FIND_ELEMENTS, () => responses.shift());
return driver.wait(until.elementsLocated(By.id('quux')), 2000)
.then(function(els) {
return Promise.all(els.map(e => e.getId()));
}).then(function(ids) {
assert.deepStrictEqual(responses, [['end']]);
assert.equal(ids.length, 2);
assert.equal(ids[0], 'abc123');
assert.equal(ids[1], 'foo');
});
});
describe('untilElementsLocated, noElementsFound', function() {
function runNoElementsFoundTest(locator, locatorStr) {
executor.on(CommandName.FIND_ELEMENTS, () => []);
function expectedFailure() {
fail('expected condition to timeout');
}
return driver.wait(until.elementsLocated(locator), 100)
.then(expectedFailure, function(error) {
var expected =
'Waiting for at least one element to be located ' + locatorStr;
var lines = error.message.split(/\n/, 2);
assert.equal(lines[0], expected);
let regex = /^Wait timed out after \d+ms$/;
assert.ok(regex.test(lines[1]),
`Lines <${lines[1]}> does not match ${regex}`);
});
}
it('byLocator', function() {
return runNoElementsFoundTest(
By.id('quux'), 'By(css selector, *[id="quux"])');
});
it('byHash', function() {
return runNoElementsFoundTest(
{id: 'quux'}, 'By(css selector, *[id="quux"])');
});
it('byFunction', function() {
return runNoElementsFoundTest(function() {}, 'by function()');
});
});
it('testUntilStalenessOf', function() {
let count = 0;
executor.on(CommandName.GET_ELEMENT_TAG_NAME, function() {
while (count < 3) {
count += 1;
return 'body';
}
throw new error.StaleElementReferenceError('now stale');
});
var el = new webdriver.WebElement(driver, {ELEMENT: 'foo'});
return driver.wait(until.stalenessOf(el), 2000)
.then(() => assert.equal(count, 3));
});
describe('element state conditions', function() {
function runElementStateTest(predicate, command, responses, var_args) {
let original = new webdriver.WebElement(driver, 'foo');
let predicateArgs = [original];
if (arguments.length > 3) {
predicateArgs = predicateArgs.concat(arguments[1]);
command = arguments[2];
responses = arguments[3];
}
assert.ok(responses.length > 1);
responses = responses.concat(['end']);
executor.on(command, () => responses.shift());
let result = driver.wait(predicate.apply(null, predicateArgs), 2000);
assert.ok(result instanceof webdriver.WebElementPromise);
return result.then(function(value) {
assert.ok(value instanceof webdriver.WebElement);
assert.ok(!(value instanceof webdriver.WebElementPromise));
return value.getId();
}).then(function(id) {
assert.equal('foo', id);
assert.deepStrictEqual(responses, ['end']);
});
}
it('elementIsVisible', function() {
return runElementStateTest(
until.elementIsVisible,
CommandName.IS_ELEMENT_DISPLAYED, [false, false, true]);
});
it('elementIsNotVisible', function() {
return runElementStateTest(
until.elementIsNotVisible,
CommandName.IS_ELEMENT_DISPLAYED, [true, true, false]);
});
it('elementIsEnabled', function() {
return runElementStateTest(
until.elementIsEnabled,
CommandName.IS_ELEMENT_ENABLED, [false, false, true]);
});
it('elementIsDisabled', function() {
return runElementStateTest(
until.elementIsDisabled,
CommandName.IS_ELEMENT_ENABLED, [true, true, false]);
});
it('elementIsSelected', function() {
return runElementStateTest(
until.elementIsSelected,
CommandName.IS_ELEMENT_SELECTED, [false, false, true]);
});
it('elementIsNotSelected', function() {
return runElementStateTest(
until.elementIsNotSelected,
CommandName.IS_ELEMENT_SELECTED, [true, true, false]);
});
it('elementTextIs', function() {
return runElementStateTest(
until.elementTextIs, 'foobar',
CommandName.GET_ELEMENT_TEXT,
['foo', 'fooba', 'foobar']);
});
it('elementTextContains', function() {
return runElementStateTest(
until.elementTextContains, 'bar',
CommandName.GET_ELEMENT_TEXT,
['foo', 'foobaz', 'foobarbaz']);
});
it('elementTextMatches', function() {
return runElementStateTest(
until.elementTextMatches, /fo+bar{3}/,
CommandName.GET_ELEMENT_TEXT,
['foo', 'foobar', 'fooobarrr']);
});
});
describe('WebElementCondition', function() {
it('fails if wait completes with a non-WebElement value', function() {
let result = driver.wait(
new webdriver.WebElementCondition('testing', () => 123), 1000);
return result.then(
() => assert.fail('expected to fail'),
function(e) {
assert.ok(e instanceof TypeError);
assert.equal(
'WebElementCondition did not resolve to a WebElement: '
+ '[object Number]',
e.message);
});
});
});
});

File diff suppressed because it is too large Load diff