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

chore(test): clean up no control flow typescript tests #5023

Merged
merged 1 commit into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ var passingTests = [
// 'node built/cli.js spec/noGlobalsConf.js',
// 'node built/cli.js spec/angular2Conf.js',
// 'node built/cli.js spec/hybridConf.js',
// 'node built/cli.js spec/built/noCFBasicConf.js',
// 'node built/cli.js spec/built/noCFBasicConf.js --useBlockingProxy',
// 'node built/cli.js spec/built/noCFPluginConf.js',
'node built/cli.js spec/built/noCFBasicConf.js',
'node built/cli.js spec/built/noCFBasicConf.js --useBlockingProxy',
'node built/cli.js spec/built/noCFPluginConf.js',
// //'node scripts/driverProviderAttachSession.js',
// 'node built/cli.js spec/driverProviderUseExistingWebDriver.js',
// 'node built/cli.js spec/driverProviderUseExistingWebDriver.js --useBlockingProxy',
Expand Down
115 changes: 60 additions & 55 deletions spec/ts/basic/element_spec.ts
Original file line number Diff line number Diff line change
@@ -1,186 +1,191 @@
// Based off of spec/basic/elements_spec.js
import * as q from 'q';

import {$, $$, browser, by, By, element, ElementArrayFinder, ElementFinder, ExpectedConditions, promise as ppromise, WebElement} from '../../..';
import {$, browser, by, element, ElementArrayFinder, ElementFinder, promise as ppromise, WebElement} from '../../..';

describe('ElementFinder', function() {
it('should return the same result as browser.findElement', async function() {
describe('ElementFinder', () => {
it('should return the same result as browser.findElement', async() => {
await browser.get('index.html#/form');
const nameByElement = element(by.binding('username'));

await expect(nameByElement.getText())
.toEqual(browser.findElement(by.binding('username')).getText());
expect(await nameByElement.getText())
.toEqual(await browser.findElement(by.binding('username')).getText());
});

it('should wait to grab the WebElement until a method is called', async function() {
it('should wait to grab the WebElement until a method is called', async() => {
// These should throw no error before a page is loaded.
const usernameInput = element(by.model('username'));
const name = element(by.binding('username'));

await browser.get('index.html#/form');

await expect(name.getText()).toEqual('Anon');
expect(await name.getText()).toEqual('Anon');

await usernameInput.clear();
await usernameInput.sendKeys('Jane');
await expect(name.getText()).toEqual('Jane');
expect(await name.getText()).toEqual('Jane');
});

it('should chain element actions', async function() {
it('should chain element actions', async() => {
await browser.get('index.html#/form');

const usernameInput = element(by.model('username'));
const name = element(by.binding('username'));

await expect(name.getText()).toEqual('Anon');
expect(await name.getText()).toEqual('Anon');

await((usernameInput.clear() as any) as ElementFinder).sendKeys('Jane');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wat? Can't this at least be (usernameInput.clear() as ElementFinder)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. wat is right. I might need to fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting it just directly to ElementFinder does not work as shown above. Per the documentation, the return type is webdriver.promise.Promise.<void> so I'm not sure how this actually works. Shouldn't it return a promise for an ElementFinder? That would make more sense.

Copy link
Contributor

@heathkit heathkit Nov 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, yeah it should be as Promise<ElementFinder>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a TODO to fix this and it is logged as an issue.

await expect(name.getText()).toEqual('Jane');
expect(await name.getText()).toEqual('Jane');
});

it('should run chained element actions in sequence', function(done: any) {
it('should run chained element actions in sequence', async(done: any) => {
// Testing private methods is bad :(
let els = new ElementArrayFinder(browser, () => {
return ppromise.when([null as WebElement]);
return Promise.resolve([null as WebElement]);
});
let applyAction_: (actionFn: (value: WebElement, index: number, array: WebElement[]) => any) =>
ElementArrayFinder = (ElementArrayFinder as any).prototype.applyAction_;
let order: string[] = [];

let deferredA = q.defer<void>();
let deferredA: Promise<void>;
els = applyAction_.call(els, () => {
return deferredA.promise.then(() => {
deferredA = new Promise<void>(resolve => {
order.push('a');
resolve();
});
});
let deferredB = q.defer<void>();
let deferredB: Promise<void>;
els = applyAction_.call(els, () => {
return deferredB.promise.then(() => {
deferredB = new Promise<void>(resolve => {
order.push('b');
resolve();
});
});

deferredB.resolve();
await deferredB;
setTimeout(async function() {
deferredA.resolve();
await deferredA;
await els;
expect(order).toEqual(['a', 'b']);
done();
}, 100);
});

it('chained call should wait to grab the WebElement until a method is called', async function() {
it('chained call should wait to grab the WebElement until a method is called',
async() => {
// These should throw no error before a page is loaded.
const reused = element(by.id('baz')).element(by.binding('item.reusedBinding'));
const reused = element(by.id('baz'))
.element(by.binding('item.reusedBinding'));

await browser.get('index.html#/conflict');

await expect(reused.getText()).toEqual('Inner: inner');
await expect(reused.isPresent()).toBe(true);
expect(await reused.getText()).toEqual('Inner: inner');
expect(await reused.isPresent()).toBe(true);
});

it('should differentiate elements with the same binding by chaining', async function() {
it('should differentiate elements with the same binding by chaining',
async() => {
await browser.get('index.html#/conflict');

const outerReused = element(by.binding('item.reusedBinding'));
const innerReused = element(by.id('baz')).element(by.binding('item.reusedBinding'));

await expect(outerReused.getText()).toEqual('Outer: outer');
await expect(innerReused.getText()).toEqual('Inner: inner');
expect(await outerReused.getText()).toEqual('Outer: outer');
expect(await innerReused.getText()).toEqual('Inner: inner');
});

it('should chain deeper than 2', async function() {
it('should chain deeper than 2', async() => {
// These should throw no error before a page is loaded.
const reused =
element(by.css('body')).element(by.id('baz')).element(by.binding('item.reusedBinding'));
const reused = element(by.css('body')).element(by.id('baz'))
.element(by.binding('item.reusedBinding'));

await browser.get('index.html#/conflict');

await expect(reused.getText()).toEqual('Inner: inner');
expect(await reused.getText()).toEqual('Inner: inner');
});

it('should allow handling errors', async function() {
it('should allow handling errors', async() => {
await browser.get('index.html#/form');
try {
await $('.nopenopenope').getText();

// The above line should have throw an error. Fail.
await expect(true).toEqual(false);
expect(true).toEqual(false);
} catch (e) {
await expect(true).toEqual(true);
expect(true).toEqual(true);
}
});

it('should allow handling chained errors', async function() {
it('should allow handling chained errors', async() => {
await browser.get('index.html#/form');
try {
await $('.nopenopenope').$('furthernope').getText();

// The above line should have throw an error. Fail.
await expect(true).toEqual(false);
expect(true).toEqual(false);
} catch (e) {
await expect(true).toEqual(true);
expect(true).toEqual(true);
}
});

it('should keep a reference to the original locator', async function() {
it('should keep a reference to the original locator', async() => {
await browser.get('index.html#/form');

const byCss = by.css('body');
const byBinding = by.binding('greet');

await expect(element(byCss).locator()).toEqual(byCss);
await expect(element(byBinding).locator()).toEqual(byBinding);
expect(await element(byCss).locator()).toEqual(byCss);
expect(await element(byBinding).locator()).toEqual(byBinding);
});

it('should propagate exceptions', async function() {
it('should propagate exceptions', async() => {
await browser.get('index.html#/form');

const invalidElement = element(by.binding('INVALID'));
const successful = invalidElement.getText().then(
function() {
return true;
} as any as (() => ppromise.Promise<void>),
} as any as (() => Promise<boolean>),
function() {
return false;
} as any as (() => ppromise.Promise<void>));
await expect(successful).toEqual(false);
} as any as (() => Promise<boolean>));
expect(await successful).toEqual(false);
});

it('should be returned from a helper without infinite loops', async function() {
it('should be returned from a helper without infinite loops', async() => {
await browser.get('index.html#/form');
const helperPromise = ppromise.when(true).then(function() {
const helperPromise = Promise.resolve(true).then(() => {
return element(by.binding('greeting'));
});

await helperPromise.then(async function(finalResult: ElementFinder) {
await expect(finalResult.getText()).toEqual('Hiya');
} as any as (() => ppromise.Promise<void>));
await helperPromise.then(async(finalResult: ElementFinder) => {
expect(await finalResult.getText()).toEqual('Hiya');
});
});

it('should be usable in WebDriver functions', async function() {
it('should be usable in WebDriver functions', async() => {
await browser.get('index.html#/form');
const greeting = element(by.binding('greeting'));
await browser.executeScript('arguments[0].scrollIntoView', greeting);
});

it('should allow null as success handler', async function() {
it('should allow null as success handler', async() => {
await browser.get('index.html#/form');

const name = element(by.binding('username'));

await expect(name.getText()).toEqual('Anon');
await expect(name.getText().then(null, function() {})).toEqual('Anon');
expect(await name.getText()).toEqual('Anon');
expect(await name.getText().then(null, function() {})).toEqual('Anon');

});

it('should check equality correctly', async function() {
it('should check equality correctly', async() => {
await browser.get('index.html#/form');

const usernameInput = element(by.model('username'));
const name = element(by.binding('username'));

await expect(usernameInput.equals(usernameInput)).toEqual(true);
await expect(usernameInput.equals(name)).toEqual(false);
expect(await usernameInput.equals(usernameInput)).toEqual(true);
expect(await usernameInput.equals(name)).toEqual(false);
});
});
4 changes: 2 additions & 2 deletions spec/ts/basic/is_disabled_spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {browser, promise as ppromise} from '../../..';

describe('verify control flow is off', function() {
describe('verify control flow is off', () => {
it('should have set webdriver.promise.USE_PROMISE_MANAGER', () => {
expect((ppromise as any).USE_PROMISE_MANAGER).toBe(false);
});

it('should not wait on one command before starting another', async function() {
it('should not wait on one command before starting another', async() => {
// Wait forever
browser.controlFlow().wait(
(browser.controlFlow() as any).promise((): void => undefined) as ppromise.Promise<void>);
Expand Down
10 changes: 4 additions & 6 deletions spec/ts/noCFPluginConf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as q from 'q';
import {Config, protractor} from '../..';
import {promise as wdpromise} from 'selenium-webdriver';
const env = require('../environment.js');

export let config: Config = {
Expand All @@ -20,11 +18,11 @@ export let config: Config = {

plugins: [{
inline: {
onPageLoad: function() {
//TODO: remove cast when @types/selenium-webdriver understands disabling the control flow
return (q.delay(100) as any as wdpromise.Promise<void>).then(function() {
(protractor as any).ON_PAGE_LOAD = true;
onPageLoad: async function() {
await new Promise(resolve => {
setTimeout(resolve, 100);
});
(protractor as any).ON_PAGE_LOAD = true;
}
}
}]
Expand Down
6 changes: 3 additions & 3 deletions spec/ts/plugin/plugin_spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {browser, protractor} from '../../..';

describe('plugins', function() {
it('should have run the onPageLoad hook', async function() {
describe('plugins', () => {
it('should have run the onPageLoad hook', async() => {
await browser.get('index.html');
await expect((protractor as any).ON_PAGE_LOAD).toBe(true);
expect((protractor as any).ON_PAGE_LOAD).toBe(true);
});
});