Skip to content

Commit

Permalink
Added INPUT_CLEAR function (#366)
Browse files Browse the repository at this point in the history
* Added INPUT_CLEAR function

* Fixed linting issue

* Fixed formatting
  • Loading branch information
ziflex authored Sep 1, 2019
1 parent 2a81356 commit af1125c
Show file tree
Hide file tree
Showing 17 changed files with 1,704 additions and 87 deletions.
File renamed without changes.
12 changes: 12 additions & 0 deletions e2e/tests/dynamic/element/clear/clear.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
LET url = @dynamic + "?redirect=/forms"
LET doc = DOCUMENT(url, true)

WAIT_ELEMENT(doc, "form")

LET input = ELEMENT(doc, "#text_input")

INPUT(input, "Foo", 100)

INPUT_CLEAR(input)

RETURN EXPECT("", INNER_TEXT(doc, "#text_output"))
14 changes: 14 additions & 0 deletions e2e/tests/dynamic/element/clear/clear_by_selector.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
LET url = @dynamic + "?redirect=/forms"
LET doc = DOCUMENT(url, true)

WAIT_ELEMENT(doc, "form")

LET form = ELEMENT(doc, "#page-form")

INPUT(form, "#text_input", "foo")
INPUT_CLEAR(form, "#text_input")

LET input = ELEMENT(doc, "#text_input")
LET output = ELEMENT(doc, "#text_output")

RETURN EXPECT("", output.innerText)
File renamed without changes.
12 changes: 12 additions & 0 deletions e2e/tests/dynamic/element/input/input_by_selector.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
LET url = @dynamic + "?redirect=/forms"
LET doc = DOCUMENT(url, true)

WAIT_ELEMENT(doc, "form")

LET form = ELEMENT(doc, "#page-form")

INPUT(form, "#text_input", "foo")

LET output = ELEMENT(doc, "#text_output")

RETURN EXPECT(output.innerText, "foo")
4 changes: 0 additions & 4 deletions pkg/drivers/cdp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,6 @@ func (doc *HTMLDocument) ClickBySelectorAll(ctx context.Context, selector values
return doc.element.ClickBySelectorAll(ctx, selector)
}

func (doc *HTMLDocument) InputBySelector(ctx context.Context, selector values.String, value core.Value, delay values.Int) error {
return doc.input.TypeBySelector(ctx, doc.element.id.nodeID, selector, value, delay)
}

func (doc *HTMLDocument) SelectBySelector(ctx context.Context, selector values.String, value *values.Array) (*values.Array, error) {
return doc.input.SelectBySelector(ctx, doc.element.id.nodeID, selector, value)
}
Expand Down
48 changes: 44 additions & 4 deletions pkg/drivers/cdp/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,27 @@ func (el *HTMLElement) Input(ctx context.Context, value core.Value, delay values
return core.Error(core.ErrInvalidOperation, "element is not an <input> element.")
}

return el.input.Type(ctx, el.id.objectID, value, delay)
return el.input.Type(ctx, el.id.objectID, input.TypeParams{
Text: value,
Clear: false,
Delay: delay,
})
}

func (el *HTMLElement) InputBySelector(ctx context.Context, selector values.String, value core.Value, delay values.Int) error {
return el.input.TypeBySelector(ctx, el.id.nodeID, selector, input.TypeParams{
Text: value,
Clear: false,
Delay: delay,
})
}

func (el *HTMLElement) Clear(ctx context.Context) error {
return el.input.Clear(ctx, el.id.objectID)
}

func (el *HTMLElement) ClearBySelector(ctx context.Context, selector values.String) error {
return el.input.ClearBySelector(ctx, el.id.nodeID, selector)
}

func (el *HTMLElement) Select(ctx context.Context, value *values.Array) (*values.Array, error) {
Expand Down Expand Up @@ -1215,6 +1235,10 @@ func (el *HTMLElement) handleAttrModified(ctx context.Context, message interface
return
}

if el.IsDetached() {
return
}

el.attributes.Mutate(ctx, func(v core.Value, err error) {
if err != nil {
el.logError(err).Msg("failed to update element")
Expand Down Expand Up @@ -1255,6 +1279,10 @@ func (el *HTMLElement) handleAttrRemoved(ctx context.Context, message interface{
return
}

if el.IsDetached() {
return
}

el.attributes.Mutate(ctx, func(v core.Value, err error) {
if err != nil {
el.logError(err).Msg("failed to update element")
Expand Down Expand Up @@ -1287,6 +1315,13 @@ func (el *HTMLElement) handleChildrenCountChanged(ctx context.Context, message i
return
}

if el.IsDetached() {
return
}

el.mu.Lock()
defer el.mu.Unlock()

node, err := el.client.DOM.DescribeNode(
ctx,
dom.NewDescribeNodeArgs().SetObjectID(el.id.objectID),
Expand All @@ -1298,9 +1333,6 @@ func (el *HTMLElement) handleChildrenCountChanged(ctx context.Context, message i
return
}

el.mu.Lock()
defer el.mu.Unlock()

el.children = createChildrenArray(node.Node.Children)
}

Expand All @@ -1319,6 +1351,10 @@ func (el *HTMLElement) handleChildInserted(ctx context.Context, message interfac
prevID := reply.PreviousNodeID
nextID := reply.Node.NodeID

if el.IsDetached() {
return
}

el.mu.Lock()
defer el.mu.Unlock()

Expand Down Expand Up @@ -1375,6 +1411,10 @@ func (el *HTMLElement) handleChildRemoved(ctx context.Context, message interface
targetIDx := -1
targetID := reply.NodeID

if el.IsDetached() {
return
}

el.mu.Lock()
defer el.mu.Unlock()

Expand Down
69 changes: 65 additions & 4 deletions pkg/drivers/cdp/input/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,45 @@ package input

import (
"context"
"github.com/pkg/errors"
"time"

"github.com/mafredri/cdp"
"github.com/mafredri/cdp/protocol/input"
)

type Keyboard struct {
client *cdp.Client
}
const DefaultDelay = 25

type (
KeyboardModifier int

KeyboardLocation int

KeyboardKey struct {
KeyCode int
Key string
Code string
Modifier KeyboardModifier
Location KeyboardLocation
}

Keyboard struct {
client *cdp.Client
}
)

const (
KeyboardModifierNone KeyboardModifier = 0
KeyboardModifierAlt KeyboardModifier = 1
KeyboardModifierCtrl KeyboardModifier = 2
KeyboardModifierCmd KeyboardModifier = 4
KeyboardModifierShift KeyboardModifier = 8

// 1=Left, 2=Right
KeyboardLocationNone KeyboardLocation = 0
KeyboardLocationLeft KeyboardLocation = 1
KeyboardLocationRight KeyboardLocation = 2
)

func NewKeyboard(client *cdp.Client) *Keyboard {
return &Keyboard{client}
Expand Down Expand Up @@ -40,7 +70,7 @@ func (k *Keyboard) Type(ctx context.Context, text string, delay int) error {
return err
}

releaseDelay := randomDuration(delay)
releaseDelay := randomDuration(delay) * time.Millisecond
time.Sleep(releaseDelay)

if err := k.Up(ctx, ch); err != nil {
Expand All @@ -50,3 +80,34 @@ func (k *Keyboard) Type(ctx context.Context, text string, delay int) error {

return nil
}

func (k *Keyboard) Press(ctx context.Context, name string) error {
key, found := usKeyboardLayout[name]

if !found {
return errors.New("invalid key")
}

err := k.client.Input.DispatchKeyEvent(
ctx,
input.NewDispatchKeyEventArgs("keyDown").
SetCode(key.Code).
SetKey(key.Key).
SetWindowsVirtualKeyCode(key.KeyCode),
)

if err != nil {
return err
}

releaseDelay := randomDuration(DefaultDelay)
time.Sleep(releaseDelay)

return k.client.Input.DispatchKeyEvent(
ctx,
input.NewDispatchKeyEventArgs("keyUp").
SetCode(key.Code).
SetKey(key.Key).
SetWindowsVirtualKeyCode(key.KeyCode),
)
}
Loading

0 comments on commit af1125c

Please sign in to comment.