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

Issue 3578 expected conditions #4006

Merged
merged 2 commits into from
Jan 27, 2017
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
33 changes: 9 additions & 24 deletions lib/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {ElementHelper, ProtractorBrowser} from './browser';
import {IError} from './exitCodes';
import {Locator} from './locators';
import {Logger} from './logger';
import {falseIfMissing} from './util';

let clientSideScripts = require('./clientsidescripts');

Expand Down Expand Up @@ -1071,30 +1072,14 @@ export class ElementFinder extends WebdriverWebElement {
* the element is present on the page.
*/
isPresent(): wdpromise.Promise<boolean> {
return this.parentElementArrayFinder.getWebElements().then(
(arr: any[]) => {
if (arr.length === 0) {
return false;
}
return arr[0].isEnabled().then(
() => {
return true; // is present, whether it is enabled or not
},
(err: any) => {
if (err instanceof wderror.StaleElementReferenceError) {
return false;
} else {
throw err;
}
});
},
(err: Error) => {
if (err instanceof wderror.NoSuchElementError) {
return false;
} else {
throw err;
}
});
return this.parentElementArrayFinder.getWebElements().then((arr: any[]) => {
if (arr.length === 0) {
return false;
}
return arr[0].isEnabled().then(() => {
return true; // is present, whether it is enabled or not
}, falseIfMissing);
}, falseIfMissing);
}

/**
Expand Down
21 changes: 10 additions & 11 deletions lib/expectedConditions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {error as wderror} from 'selenium-webdriver';
import {ProtractorBrowser} from './browser';
import {ElementFinder} from './element';
import {falseIfMissing, passBoolean} from './util';

/**
* Represents a library of canned expected conditions that are useful for
Expand Down Expand Up @@ -185,7 +186,9 @@ export class ProtractorExpectedConditions {
* representing whether the element is clickable.
*/
elementToBeClickable(elementFinder: ElementFinder): Function {
return this.and(this.visibilityOf(elementFinder), elementFinder.isEnabled.bind(elementFinder));
return this.and(this.visibilityOf(elementFinder), () => {
return elementFinder.isEnabled().then(passBoolean, falseIfMissing);
});
}

/**
Expand All @@ -210,7 +213,7 @@ export class ProtractorExpectedConditions {
// MSEdge does not properly remove newlines, which causes false
// negatives
return actualText.replace(/\r?\n|\r/g, '').indexOf(text) > -1;
});
}, falseIfMissing);
};
return this.and(this.presenceOf(elementFinder), hasText);
}
Expand All @@ -235,7 +238,7 @@ export class ProtractorExpectedConditions {
let hasText = () => {
return elementFinder.getAttribute('value').then((actualText: string): boolean => {
return actualText.indexOf(text) > -1;
});
}, falseIfMissing);
};
return this.and(this.presenceOf(elementFinder), hasText);
}
Expand Down Expand Up @@ -389,13 +392,7 @@ export class ProtractorExpectedConditions {
*/
visibilityOf(elementFinder: ElementFinder): Function {
return this.and(this.presenceOf(elementFinder), () => {
return elementFinder.isDisplayed().then((displayed: boolean) => displayed, (err: any) => {
if (err instanceof wderror.NoSuchElementError) {
return false;
} else {
throw err;
}
});
return elementFinder.isDisplayed().then(passBoolean, falseIfMissing);
});
}

Expand Down Expand Up @@ -433,6 +430,8 @@ export class ProtractorExpectedConditions {
* representing whether the element is selected.
*/
elementToBeSelected(elementFinder: ElementFinder): Function {
return this.and(this.presenceOf(elementFinder), elementFinder.isSelected.bind(elementFinder));
return this.and(this.presenceOf(elementFinder), () => {
return elementFinder.isSelected().then(passBoolean, falseIfMissing);
});
}
}
29 changes: 29 additions & 0 deletions lib/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {resolve} from 'path';
import {Promise, when} from 'q';
import {error as wderror} from 'selenium-webdriver';

let STACK_SUBSTRINGS_TO_FILTER = [
'node_modules/jasmine/', 'node_modules/selenium-webdriver', 'at Module.', 'at Object.Module.',
Expand Down Expand Up @@ -75,3 +76,31 @@ export function joinTestLogs(log1: any, log2: any): any {
specResults: (log1.specResults || []).concat(log2.specResults || [])
};
}

/**
* Returns false if an error indicates a missing or stale element, re-throws
* the error otherwise
*
* @param {*} The error to check
* @throws {*} The error it was passed if it doesn't indicate a missing or stale
* element
* @return {boolean} false, if it doesn't re-throw the error
*/
export function falseIfMissing(error: any) {
if ((error instanceof wderror.NoSuchElementError) ||
(error instanceof wderror.StaleElementReferenceError)) {
return false;
} else {
throw error;
}
}

/**
* Return a boolean given boolean value.
*
* @param {boolean} value
* @returns {boolean} given value
*/
export function passBoolean(value: boolean) {
return value;
}
77 changes: 57 additions & 20 deletions spec/basic/expected_conditions_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,6 @@ describe('expected conditions', function() {
expect(visibilityOfHideable.call()).toBe(false);
});

it('should have visibilityOf (handling race conditions)', function() {
var disabledButton = $('#disabledButton[disabled="disabled"]');

// toggle presence (of .ng-hide) between visibility evaluation to simulate race condition
var originalIsDisplayedFn = disabledButton.isDisplayed;
disabledButton.isDisplayed = function () {
element(by.model('disabled')).click();
return originalIsDisplayedFn.call(this);
};

var visibilityOfDisabledButtonWithInterceptor = EC.visibilityOf(disabledButton);

element(by.model('disabled')).click();

expect(originalIsDisplayedFn.call(disabledButton)).toBe(true);
expect(disabledButton.isPresent()).toBe(true);

expect(visibilityOfDisabledButtonWithInterceptor.call()).toBe(false);
});

it('should have invisibilityOf', function() {
var invisibilityOfInvalid = EC.invisibilityOf($('#INVALID'));
var invisibilityOfHideable = EC.invisibilityOf($('#shower'));
Expand Down Expand Up @@ -215,4 +195,61 @@ describe('expected conditions', function() {
browser2.switchTo().alert().accept();
});
});

describe('race condition handling', function () {

var disabledButton;

beforeEach(function () {
disabledButton = $('#disabledButton[disabled="disabled"]');
});

function enableButtonBeforeCallToUnmatchSelector(testElement, fnName) {
var originalFn = testElement[fnName];

testElement[fnName] = function () {
element(by.model('disabled')).click();
return originalFn.apply(this, arguments);
};

// save original fn with _ prefix
testElement['_' + fnName] = originalFn;
}

it('can deal with missing elements in visibilityOf', function() {
enableButtonBeforeCallToUnmatchSelector(disabledButton, 'isDisplayed');

element(by.model('disabled')).click();

expect(disabledButton._isDisplayed()).toBe(true);
expect(EC.visibilityOf(disabledButton).call()).toBe(false);
});

it('can deal with missing elements in textToBePresentInElement', function() {
enableButtonBeforeCallToUnmatchSelector(disabledButton, 'getText');

element(by.model('disabled')).click();

expect(disabledButton._getText()).toBe('Dummy');
expect(EC.textToBePresentInElement(disabledButton, 'Dummy').call()).toBe(false);
});

it('can deal with missing elements in textToBePresentInValue', function() {
enableButtonBeforeCallToUnmatchSelector(disabledButton, 'getAttribute');

element(by.model('disabled')).click();

expect(disabledButton._getAttribute('value')).toBe('');
expect(EC.textToBePresentInElementValue(disabledButton, '').call()).toBe(false);
});

it('can deal with missing elements in elementToBeClickable', function() {
enableButtonBeforeCallToUnmatchSelector(disabledButton, 'isEnabled');

element(by.model('disabled')).click();

expect(disabledButton._isEnabled()).toBe(false);
expect(EC.elementToBeClickable(disabledButton).call()).toBe(false);
});
});
});