@@ -610,3 +610,79 @@ describe('ElementHandle.focus', function() {
610
610
expect ( await button . evaluate ( button => document . activeElement === button ) ) . toBe ( true ) ;
611
611
} ) ;
612
612
} ) ;
613
+
614
+ describe ( 'ElementHandle.type' , function ( ) {
615
+ it ( 'should work' , async ( { page} ) => {
616
+ await page . setContent ( `<input type='text' />` ) ;
617
+ await page . type ( 'input' , 'hello' ) ;
618
+ expect ( await page . $eval ( 'input' , input => input . value ) ) . toBe ( 'hello' ) ;
619
+ } ) ;
620
+ it ( 'should not select existing value' , async ( { page} ) => {
621
+ await page . setContent ( `<input type='text' value='hello' />` ) ;
622
+ await page . type ( 'input' , 'world' ) ;
623
+ expect ( await page . $eval ( 'input' , input => input . value ) ) . toBe ( 'worldhello' ) ;
624
+ } ) ;
625
+ it ( 'should reset selection when not focused' , async ( { page} ) => {
626
+ await page . setContent ( `<input type='text' value='hello' /><div tabIndex=2>text</div>` ) ;
627
+ await page . $eval ( 'input' , input => {
628
+ input . selectionStart = 2 ;
629
+ input . selectionEnd = 4 ;
630
+ document . querySelector ( 'div' ) . focus ( ) ;
631
+ } ) ;
632
+ await page . type ( 'input' , 'world' ) ;
633
+ expect ( await page . $eval ( 'input' , input => input . value ) ) . toBe ( 'worldhello' ) ;
634
+ } ) ;
635
+ it ( 'should not modify selection when focused' , async ( { page} ) => {
636
+ await page . setContent ( `<input type='text' value='hello' />` ) ;
637
+ await page . $eval ( 'input' , input => {
638
+ input . focus ( ) ;
639
+ input . selectionStart = 2 ;
640
+ input . selectionEnd = 4 ;
641
+ } ) ;
642
+ await page . type ( 'input' , 'world' ) ;
643
+ expect ( await page . $eval ( 'input' , input => input . value ) ) . toBe ( 'heworldo' ) ;
644
+ } ) ;
645
+ it ( 'should work with number input' , async ( { page} ) => {
646
+ await page . setContent ( `<input type='number' value=2 />` ) ;
647
+ await page . type ( 'input' , '13' ) ;
648
+ expect ( await page . $eval ( 'input' , input => input . value ) ) . toBe ( '132' ) ;
649
+ } ) ;
650
+ } ) ;
651
+
652
+ describe ( 'ElementHandle.press' , function ( ) {
653
+ it ( 'should work' , async ( { page} ) => {
654
+ await page . setContent ( `<input type='text' />` ) ;
655
+ await page . press ( 'input' , 'h' ) ;
656
+ expect ( await page . $eval ( 'input' , input => input . value ) ) . toBe ( 'h' ) ;
657
+ } ) ;
658
+ it ( 'should not select existing value' , async ( { page} ) => {
659
+ await page . setContent ( `<input type='text' value='hello' />` ) ;
660
+ await page . press ( 'input' , 'w' ) ;
661
+ expect ( await page . $eval ( 'input' , input => input . value ) ) . toBe ( 'whello' ) ;
662
+ } ) ;
663
+ it ( 'should reset selection when not focused' , async ( { page} ) => {
664
+ await page . setContent ( `<input type='text' value='hello' /><div tabIndex=2>text</div>` ) ;
665
+ await page . $eval ( 'input' , input => {
666
+ input . selectionStart = 2 ;
667
+ input . selectionEnd = 4 ;
668
+ document . querySelector ( 'div' ) . focus ( ) ;
669
+ } ) ;
670
+ await page . press ( 'input' , 'w' ) ;
671
+ expect ( await page . $eval ( 'input' , input => input . value ) ) . toBe ( 'whello' ) ;
672
+ } ) ;
673
+ it ( 'should not modify selection when focused' , async ( { page} ) => {
674
+ await page . setContent ( `<input type='text' value='hello' />` ) ;
675
+ await page . $eval ( 'input' , input => {
676
+ input . focus ( ) ;
677
+ input . selectionStart = 2 ;
678
+ input . selectionEnd = 4 ;
679
+ } ) ;
680
+ await page . press ( 'input' , 'w' ) ;
681
+ expect ( await page . $eval ( 'input' , input => input . value ) ) . toBe ( 'hewo' ) ;
682
+ } ) ;
683
+ it ( 'should work with number input' , async ( { page} ) => {
684
+ await page . setContent ( `<input type='number' value=2 />` ) ;
685
+ await page . press ( 'input' , '1' ) ;
686
+ expect ( await page . $eval ( 'input' , input => input . value ) ) . toBe ( '12' ) ;
687
+ } ) ;
688
+ } ) ;
0 commit comments