1+ /*[email protected] #browser/actions*/ 2+ define ( function ( require , exports , module ) {
3+ var $ = require ( './jquery' ) ;
4+ var FuncUnit = require ( './core' ) ;
5+ var syn = window . syn = require ( 'syn' ) ;
6+ var clicks = [
7+ 'click' ,
8+ 'dblclick' ,
9+ 'rightClick'
10+ ] , makeClick = function ( name ) {
11+ FuncUnit . prototype [ name ] = function ( options , success ) {
12+ this . _addExists ( ) ;
13+ if ( typeof options == 'function' ) {
14+ success = options ;
15+ options = { } ;
16+ }
17+ var selector = this . selector ;
18+ FuncUnit . add ( {
19+ method : function ( success , error ) {
20+ options = options || { } ;
21+ syn ( '_' + name , this . bind [ 0 ] , options , success ) ;
22+ } ,
23+ success : success ,
24+ error : 'Could not ' + name + ' \'' + this . selector + '\'' ,
25+ bind : this ,
26+ type : 'action'
27+ } ) ;
28+ return this ;
29+ } ;
30+ } ;
31+ for ( var i = 0 ; i < clicks . length ; i ++ ) {
32+ makeClick ( clicks [ i ] ) ;
33+ }
34+ $ . extend ( FuncUnit . prototype , {
35+ _addExists : function ( ) {
36+ this . exists ( false ) ;
37+ } ,
38+ type : function ( text , success ) {
39+ this . _addExists ( ) ;
40+ this . click ( ) ;
41+ var selector = this . selector ;
42+ if ( text === '' ) {
43+ text = '[ctrl]a[ctrl-up]\b' ;
44+ }
45+ FuncUnit . add ( {
46+ method : function ( success , error ) {
47+ syn ( '_type' , this . bind [ 0 ] , text , success ) ;
48+ } ,
49+ success : success ,
50+ error : 'Could not type ' + text + ' into ' + this . selector ,
51+ bind : this ,
52+ type : 'action'
53+ } ) ;
54+ return this ;
55+ } ,
56+ sendKeys : function ( keys , success ) {
57+ this . _addExists ( ) ;
58+ var selector = this . selector ;
59+ if ( keys === '' ) {
60+ keys = '[ctrl]a[ctrl-up]\b' ;
61+ }
62+ FuncUnit . add ( {
63+ method : function ( success , error ) {
64+ syn ( '_type' , this . bind [ 0 ] , keys , success ) ;
65+ } ,
66+ success : success ,
67+ error : 'Could not send the keys ' + keys + ' into ' + this . selector ,
68+ bind : this ,
69+ type : 'action'
70+ } ) ;
71+ return this ;
72+ } ,
73+ trigger : function ( evName , success ) {
74+ this . _addExists ( ) ;
75+ FuncUnit . add ( {
76+ method : function ( success , error ) {
77+ if ( ! FuncUnit . win . jQuery ) {
78+ throw 'Can not trigger custom event, no jQuery found on target page.' ;
79+ }
80+ FuncUnit . win . jQuery ( this . bind . selector ) . trigger ( evName ) ;
81+ success ( ) ;
82+ } ,
83+ success : success ,
84+ error : 'Could not trigger ' + evName ,
85+ bind : this ,
86+ type : 'action'
87+ } ) ;
88+ return this ;
89+ } ,
90+ drag : function ( options , success ) {
91+ this . _addExists ( ) ;
92+ if ( typeof options == 'string' ) {
93+ options = { to : options } ;
94+ }
95+ options . from = this . selector ;
96+ var selector = this . selector ;
97+ FuncUnit . add ( {
98+ method : function ( success , error ) {
99+ syn ( '_drag' , this . bind [ 0 ] , options , success ) ;
100+ } ,
101+ success : success ,
102+ error : 'Could not drag ' + this . selector ,
103+ bind : this ,
104+ type : 'action'
105+ } ) ;
106+ return this ;
107+ } ,
108+ move : function ( options , success ) {
109+ this . _addExists ( ) ;
110+ if ( typeof options == 'string' ) {
111+ options = { to : options } ;
112+ }
113+ options . from = this . selector ;
114+ var selector = this . selector ;
115+ FuncUnit . add ( {
116+ method : function ( success , error ) {
117+ syn ( '_move' , this . bind [ 0 ] , options , success ) ;
118+ } ,
119+ success : success ,
120+ error : 'Could not move ' + this . selector ,
121+ bind : this ,
122+ type : 'action'
123+ } ) ;
124+ return this ;
125+ } ,
126+ scroll : function ( direction , amount , success ) {
127+ this . _addExists ( ) ;
128+ var selector = this . selector , direction ;
129+ if ( direction == 'left' || direction == 'right' ) {
130+ direction = 'Left' ;
131+ } else if ( direction == 'top' || direction == 'bottom' ) {
132+ direction = 'Top' ;
133+ }
134+ FuncUnit . add ( {
135+ method : function ( success , error ) {
136+ this . bind . each ( function ( i , el ) {
137+ this [ 'scroll' + direction ] = amount ;
138+ } ) ;
139+ success ( ) ;
140+ } ,
141+ success : success ,
142+ error : 'Could not scroll ' + this . selector ,
143+ bind : this ,
144+ type : 'action'
145+ } ) ;
146+ return this ;
147+ }
148+ } ) ;
149+ module . exports = FuncUnit ;
150+ } ) ;
0 commit comments