Skip to content

Commit 5a12045

Browse files
committed
feat(element): clearing inputs for type date, time, week, month, and datetime-local
closes angular#562
1 parent 9764ea4 commit 5a12045

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

lib/element.ts

+56-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {By, error as wderror, ILocation, ISize, promise as wdpromise, WebDriver, WebElement, WebElementPromise} from 'selenium-webdriver';
1+
import {By, error as wderror, ILocation, ISize, Key, promise as wdpromise, WebDriver, WebElement, WebElementPromise} from 'selenium-webdriver';
22

33
import {ElementHelper, ProtractorBrowser} from './browser';
44
import {IError} from './exitCodes';
@@ -11,11 +11,13 @@ let logger = new Logger('element');
1111

1212
let WEB_ELEMENT_FUNCTIONS = [
1313
'click', 'sendKeys', 'getTagName', 'getCssValue', 'getAttribute', 'getText', 'getSize',
14-
'getLocation', 'isEnabled', 'isSelected', 'submit', 'clear', 'isDisplayed', 'getOuterHtml',
14+
'getLocation', 'isEnabled', 'isSelected', 'submit', 'isDisplayed', 'getOuterHtml',
1515
'getInnerHtml', 'getId', 'getRawId', 'serialize', 'takeScreenshot'
1616
];
1717

18-
export class WebdriverWebElement {}
18+
export class WebdriverWebElement {
19+
clear_: () => wdpromise.Promise<void> = WebElement.prototype.clear;
20+
}
1921
export interface WebdriverWebElement extends WebElement {}
2022

2123
/**
@@ -98,6 +100,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
98100
}
99101
[key: string]: any;
100102

103+
101104
/**
102105
* Create a shallow copy of ElementArrayFinder.
103106
*
@@ -735,6 +738,27 @@ export class ElementArrayFinder extends WebdriverWebElement {
735738
};
736739
return this.applyAction_(allowAnimationsTestFn);
737740
}
741+
742+
clear(): wdpromise.Promise<void> {
743+
return this.getWebElements().then((arr: WebElement[]) => {
744+
arr.forEach((we: WebElement) => {
745+
we.getAttribute('type').then(att => {
746+
if (att === 'date' || att === 'time') {
747+
return this.sendKeys(Key.BACK_SPACE + Key.TAB + Key.BACK_SPACE +
748+
Key.TAB + Key.BACK_SPACE);
749+
} else if (att === 'month' || att === 'week') {
750+
return this.sendKeys(Key.BACK_SPACE + Key.TAB + Key.BACK_SPACE);
751+
} else if (att === 'datetime-local' ) {
752+
return this.sendKeys(Key.BACK_SPACE + Key.TAB + Key.BACK_SPACE +
753+
Key.TAB + Key.BACK_SPACE + Key.TAB + Key.BACK_SPACE +
754+
Key.TAB + Key.BACK_SPACE + Key.TAB + Key.BACK_SPACE);
755+
} else {
756+
return this.clear_();
757+
}
758+
});
759+
});
760+
});
761+
}
738762
}
739763

740764
/**
@@ -1150,6 +1174,35 @@ export class ElementFinder extends WebdriverWebElement {
11501174
(element as any).getWebElement ? (element as ElementFinder).getWebElement() :
11511175
element as WebElement);
11521176
}
1177+
1178+
/**
1179+
* Schedules a command to clear the {@code value} of this element. This command
1180+
* has no effect if the underlying DOM element is neither a text INPUT element
1181+
* nor a TEXTAREA element.
1182+
*
1183+
* @view
1184+
* <input id="foo" value="Default Text">
1185+
* <input id="datetime" type="datetime">
1186+
*
1187+
* @example
1188+
* var foo = element(by.id('foo'));
1189+
*
1190+
* expect(foo.getAttribute('value')).toEqual('Default Text');
1191+
* foo.clear();
1192+
* expect(foo.getAttribute('value')).toEqual('');
1193+
*
1194+
* var datetime = element(by.id('datetime'));
1195+
* datetime.sendKeys('01/01/1970 ' + protractor.Key.TAB + '02:30AM');
1196+
* expect(datetime.getAttribute('value')).toEqual('1970-01-01T02:30');
1197+
* datetime.clear();
1198+
* expect(datetime.getAttribute('value')).toequal('');
1199+
*
1200+
* @returns {!webdriver.promise.Promise.<void>} A promise that will be resolved
1201+
* when the element has been cleared.
1202+
*/
1203+
clear() {
1204+
return this.elementArrayFinder_.clear();
1205+
}
11531206
}
11541207

11551208
/**

lib/selenium-webdriver/webdriver.js

-21
Original file line numberDiff line numberDiff line change
@@ -679,27 +679,6 @@ webdriver.WebElement.prototype.isSelected = function() {};
679679
*/
680680
webdriver.WebElement.prototype.submit = function() {};
681681

682-
683-
/**
684-
* Schedules a command to clear the {@code value} of this element. This command
685-
* has no effect if the underlying DOM element is neither a text INPUT element
686-
* nor a TEXTAREA element.
687-
*
688-
* @view
689-
* <input id="foo" value="Default Text">
690-
*
691-
* @example
692-
* var foo = element(by.id('foo'));
693-
* expect(foo.getAttribute('value')).toEqual('Default Text');
694-
* foo.clear();
695-
* expect(foo.getAttribute('value')).toEqual('');
696-
*
697-
* @returns {!webdriver.promise.Promise.<void>} A promise that will be resolved
698-
* when the element has been cleared.
699-
*/
700-
webdriver.WebElement.prototype.clear = function() {};
701-
702-
703682
/**
704683
* Schedules a command to test whether this element is currently displayed.
705684
*

0 commit comments

Comments
 (0)