Progress at getting SendKeys to send arrow keys

This commit is contained in:
Kenneth Shaw 2017-01-28 12:07:34 +07:00
parent 4f81916537
commit 6c17327b42
3 changed files with 51 additions and 16 deletions

View File

@ -23,8 +23,8 @@ func main() {
}
// run task list
var val1, val2, val3 string
err = c.Run(ctxt, sendkeys(&val1, &val2, &val3))
var val1, val2, val3, val4 string
err = c.Run(ctxt, sendkeys(&val1, &val2, &val3, &val4))
if err != nil {
log.Fatal(err)
}
@ -44,18 +44,21 @@ func main() {
log.Printf("#input1 value: %s", val1)
log.Printf("#textarea1 value: %s", val2)
log.Printf("#input2 value: %s", val3)
log.Printf("#select1 value: %s", val4)
}
func sendkeys(val1, val2, val3 *string) cdp.Tasks {
func sendkeys(val1, val2, val3, val4 *string) cdp.Tasks {
return cdp.Tasks{
cdp.Navigate("file:" + os.Getenv("GOPATH") + "/src/github.com/knq/chromedp/testdata/visible.html"),
cdp.WaitVisible(`#input1`, cdp.ByID),
cdp.WaitVisible(`#textarea1`, cdp.ByID),
cdp.SendKeys(`#textarea1`, "\b\b\n\naoeu\n\ntest1\n\nblah2\n\n\t\b\bother box!\t\ntest4", cdp.ByID),
cdp.SendKeys(`#textarea1`, "\b\b\n\naoeu\n\ntest1\n\nblah2\n\n\t\t\t\b\bother box!\t\ntest4", cdp.ByID),
cdp.Value(`#input1`, val1, cdp.ByID),
cdp.Value(`#textarea1`, val2, cdp.ByID),
cdp.SetValue(`#input2`, "test3", cdp.ByID),
cdp.Value(`#input2`, val3, cdp.ByID),
cdp.SendKeys(`#select1`, cdp.KeyCodeDown+cdp.KeyCodeDown, cdp.ByID),
cdp.Value(`#select1`, val4, cdp.ByID),
cdp.Sleep(30 * time.Second),
}
}

View File

@ -96,11 +96,36 @@ type KeyAction struct {
opts []KeyOption
}
var keyNames = map[rune]string{
'\b': "Backspace",
'\t': "Tab",
'\r': "Enter",
'\n': "Enter",
// KeyCode are known system key codes.
type KeyCode string
// KeyCode values.
const (
KeyCodeBackspace = "\b"
KeyCodeTab = "\t"
KeyCodeCR = "\r"
KeyCodeLF = "\n"
KeyCodeLeft = "\x25"
KeyCodeUp = "\x26"
KeyCodeRight = "\x27"
KeyCodeDown = "\x28"
)
const (
keyRuneCR = '\r'
)
// keyCodeNames is the map of key code values to their respective named
// identifiers.
var keyCodeNames = map[KeyCode]string{
KeyCodeBackspace: "Backspace",
KeyCodeTab: "Tab",
KeyCodeCR: "Enter",
KeyCodeLF: "Enter",
KeyCodeLeft: "Left",
KeyCodeUp: "Up",
KeyCodeRight: "Right",
KeyCodeDown: "Down",
}
// Do satisfies Action interface.
@ -117,18 +142,19 @@ func (ka *KeyAction) Do(ctxt context.Context, h cdp.FrameHandler) error {
for _, r := range ka.v {
s := string(r)
if n, ok := keyNames[r]; ok {
keyS := KeyCode(r)
if n, ok := keyCodeNames[keyS]; ok {
kc := int64(r)
if r == '\n' {
s = string('\r')
kc = int64('\r')
if keyS == KeyCodeLF {
s = string(keyRuneCR)
kc = int64(keyRuneCR)
}
err = sysP.WithKey(n).
WithNativeVirtualKeyCode(kc).
WithWindowsVirtualKeyCode(kc).
WithUnmodifiedText(s).
WithText(s).
WithKeyIdentifier(s).
WithIsSystemKey(true).
Do(ctxt, h)
if err != nil {
return err
@ -141,7 +167,7 @@ func (ka *KeyAction) Do(ctxt context.Context, h cdp.FrameHandler) error {
}
// FIXME
time.Sleep(5 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
}
return nil

View File

@ -16,6 +16,12 @@
<input id="input1" value="some value"><br><br>
<textarea id="textarea1" style="width:500px;height:400px">textarea</textarea><br><br>
<input id="input2" type="submit" value="Next">
<select id="select1">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
</select>
</p>
</div>
</body>