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' ;
2
2
3
3
import { ElementHelper , ProtractorBrowser } from './browser' ;
4
4
import { IError } from './exitCodes' ;
@@ -11,11 +11,13 @@ let logger = new Logger('element');
11
11
12
12
let WEB_ELEMENT_FUNCTIONS = [
13
13
'click' , 'sendKeys' , 'getTagName' , 'getCssValue' , 'getAttribute' , 'getText' , 'getSize' ,
14
- 'getLocation' , 'isEnabled' , 'isSelected' , 'submit' , 'clear' , ' isDisplayed', 'getOuterHtml' ,
14
+ 'getLocation' , 'isEnabled' , 'isSelected' , 'submit' , 'isDisplayed' , 'getOuterHtml' ,
15
15
'getInnerHtml' , 'getId' , 'getRawId' , 'serialize' , 'takeScreenshot'
16
16
] ;
17
17
18
- export class WebdriverWebElement { }
18
+ export class WebdriverWebElement {
19
+ clear_ : ( ) => wdpromise . Promise < void > = WebElement . prototype . clear ;
20
+ }
19
21
export interface WebdriverWebElement extends WebElement { }
20
22
21
23
/**
@@ -98,6 +100,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
98
100
}
99
101
[ key : string ] : any ;
100
102
103
+
101
104
/**
102
105
* Create a shallow copy of ElementArrayFinder.
103
106
*
@@ -735,6 +738,27 @@ export class ElementArrayFinder extends WebdriverWebElement {
735
738
} ;
736
739
return this . applyAction_ ( allowAnimationsTestFn ) ;
737
740
}
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
+ }
738
762
}
739
763
740
764
/**
@@ -1150,6 +1174,35 @@ export class ElementFinder extends WebdriverWebElement {
1150
1174
( element as any ) . getWebElement ? ( element as ElementFinder ) . getWebElement ( ) :
1151
1175
element as WebElement ) ;
1152
1176
}
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
+ }
1153
1206
}
1154
1207
1155
1208
/**
0 commit comments