forked from Dk2014/selenium
/
action_builder.go
70 lines (62 loc) · 2.05 KB
/
action_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package actions
import (
"github.com/cndark/selenium"
"reflect"
)
type ActionBuilder struct {
devices []interface{}
_pointer_action *PointerActions
_key_action *KeyActions
driver selenium.WebDriver
}
func NewActionBuilder(driver selenium.WebDriver) *ActionBuilder {
mouse := NewPointerInput(POINTER_MOUSE, "mouse")
keyboard := NewKeyInput(KEY)
input_device := []interface{}{mouse, keyboard}
return &ActionBuilder{
devices: input_device,
_pointer_action: NewPointerActions(mouse),
_key_action: NewKeyActions(keyboard),
driver: driver,
}
}
func (ab *ActionBuilder) add_key_input(name string) *KeyInput {
new_input := NewKeyInput(name)
ab._add_input(new_input)
return new_input
}
func (ab *ActionBuilder) add_pointer_input(kind, name string) *PointerInput {
new_input := NewPointerInput(kind, name)
ab._add_input(new_input)
return new_input
}
func (ab *ActionBuilder) perform() {
enc := map[string]interface{}{"actions": []interface{}{}}
for _, device := range ab.devices {
reflectEncode := reflect.ValueOf(device).MethodByName("Encode")
args := make([]reflect.Value, 0)
encoded := reflectEncode.Call(args)
c := encoded[0].Interface().(map[string]interface{})
if _, ok := c["actions"]; ok {
var list []interface{} = enc["actions"].([]interface{})
list = append(list, c)
enc["actions"] = list
}
}
//This is done by adding a VoidCommand interface to the selenium.go and adding a VoidCommand func to the remote.go file.
ab.driver.VoidCommand("/session/%s/actions", enc)
}
// This is done by adding a ClearActions func to the remote.go file.
func (ab *ActionBuilder) clear_actions() {
reflectClearActions := reflect.ValueOf(ab.driver).MethodByName("ClearActions")
args := make([]reflect.Value, 0)
reflectClearActions.Call(args)
}
func (ab *ActionBuilder) _add_input(input interface{}) {
if reflect.TypeOf(input).Name() == "*PointerInput" {
input = input.(*PointerInput)
} else if reflect.TypeOf(input).Name() == "*KeyInput" {
input = input.(*KeyInput)
}
ab.devices = append(ab.devices, input)
}