@@ -49,8 +49,9 @@ var remoteErrors = map[int]string{
4949type remoteWD struct {
5050 id , urlPrefix string
5151 capabilities Capabilities
52-
53- w3cCompatible bool
52+ w3cCompatible bool
53+ // storedActions stores KeyActions and PointerActions for later execution.
54+ storedActions Actions
5455 browser string
5556 browserVersion semver.Version
5657}
@@ -230,6 +231,7 @@ func NewRemote(capabilities Capabilities, urlPrefix string) (WebDriver, error) {
230231 if b := capabilities ["browserName" ]; b != nil {
231232 wd .browser = b .(string )
232233 }
234+
233235 if _ , err := wd .NewSession (); err != nil {
234236 return nil , err
235237 }
@@ -1095,8 +1097,104 @@ func (wd *remoteWD) KeyUp(keys string) error {
10951097 return wd .keyAction ("keyUp" , keys )
10961098}
10971099
1098- // TODO(minusnine): Implement PerformActions and ReleaseActions, for more
1099- // direct access to the W3C specification.
1100+ // KeyPauseAction builds a KeyAction which pauses for the supplied duration.
1101+ func KeyPauseAction (duration time.Duration ) KeyAction {
1102+ return KeyAction {
1103+ "type" : "pause" ,
1104+ "duration" : uint (duration / time .Millisecond ),
1105+ }
1106+ }
1107+
1108+ // KeyUpAction builds a KeyAction press.
1109+ func KeyUpAction (key string ) KeyAction {
1110+ return KeyAction {
1111+ "type" : "keyUp" ,
1112+ "value" : key ,
1113+ }
1114+ }
1115+
1116+ // KeyDownAction builds a KeyAction which presses and holds
1117+ // the specified key.
1118+ func KeyDownAction (key string ) KeyAction {
1119+ return KeyAction {
1120+ "type" : "keyDown" ,
1121+ "value" : key ,
1122+ }
1123+ }
1124+
1125+ // PointerPause builds a PointerAction which pauses for the supplied duration.
1126+ func PointerPauseAction (duration time.Duration ) PointerAction {
1127+ return PointerAction {
1128+ "type" : "pause" ,
1129+ "duration" : uint (duration / time .Millisecond ),
1130+ }
1131+ }
1132+
1133+ // PointerMove builds a PointerAction which moves the pointer.
1134+ func PointerMoveAction (duration time.Duration , offset Point , origin PointerMoveOrigin ) PointerAction {
1135+ return PointerAction {
1136+ "type" : "pointerMove" ,
1137+ "duration" : uint (duration / time .Millisecond ),
1138+ "origin" : origin ,
1139+ "x" : offset .X ,
1140+ "y" : offset .Y ,
1141+ }
1142+ }
1143+
1144+ // PointerUp builds an action which releases the specified pointer key.
1145+ func PointerUpAction (button MouseButton ) PointerAction {
1146+ return PointerAction {
1147+ "type" : "pointerUp" ,
1148+ "button" : button ,
1149+ }
1150+ }
1151+
1152+ // PointerDown builds a PointerAction which presses
1153+ // and holds the specified pointer key.
1154+ func PointerDownAction (button MouseButton ) PointerAction {
1155+ return PointerAction {
1156+ "type" : "pointerDown" ,
1157+ "button" : button ,
1158+ }
1159+ }
1160+
1161+ func (wd * remoteWD ) StoreKeyActions (inputID string , actions ... KeyAction ) {
1162+ rawActions := []map [string ]interface {}{}
1163+ for _ , action := range actions {
1164+ rawActions = append (rawActions , action )
1165+ }
1166+ wd .storedActions = append (wd .storedActions , map [string ]interface {}{
1167+ "type" : "key" ,
1168+ "id" : inputID ,
1169+ "actions" : rawActions ,
1170+ })
1171+ }
1172+
1173+ func (wd * remoteWD ) StorePointerActions (inputID string , pointer PointerType , actions ... PointerAction ) {
1174+ rawActions := []map [string ]interface {}{}
1175+ for _ , action := range actions {
1176+ rawActions = append (rawActions , action )
1177+ }
1178+ wd .storedActions = append (wd .storedActions , map [string ]interface {}{
1179+ "type" : "pointer" ,
1180+ "id" : inputID ,
1181+ "parameters" : map [string ]string {"pointerType" : string (pointer )},
1182+ "actions" : rawActions ,
1183+ })
1184+ }
1185+
1186+ func (wd * remoteWD ) PerformActions () error {
1187+ err := wd .voidCommand ("/session/%s/actions" , map [string ]interface {}{
1188+ "actions" : wd .storedActions ,
1189+ })
1190+ wd .storedActions = nil
1191+ return err
1192+ }
1193+
1194+ func (wd * remoteWD ) ReleaseActions () error {
1195+ return voidCommand ("DELETE" , wd .requestURL ("/session/%s/actions" , wd .id ), nil )
1196+ }
1197+
11001198func (wd * remoteWD ) DismissAlert () error {
11011199 return wd .voidCommand ("/session/%s/alert/dismiss" , nil )
11021200}
0 commit comments