Rod provides lots of methods to simulate human inputs, such as the mouse click or keyboard press.
To simulate the mouse click an element:
// left click
page.MustElement("button").MustClick()
// right click
_ = page.MustElement("button").Click(proto.InputMouseButtonRight, 1)
To simulate the input:
el := page.MustElement(`[type="text"]`)
el.MustInput("Jack")
fmt.Println(el.MustText()) // use MustText to get the text
Just simulate how a human does it, select all the text and replace it with an empty string:
page.MustElement(`[type="text"]`).MustSelectAllText().MustInput("")
You can use SelectText
to replace a part of the text.
The supported input types are date, datetime-local, month, and time.
page.MustElement(`[type="date"]`).MustInputTime(time.Now())
For example, the complete actions to input the uppercase 'a' like a human include:
- press and hold a
Shift
key - press and release the
A
key - release the
Shift
key
You can use the Page.KeyActions
or Element.KeyActions
helpers to do it:
page.KeyActions().Press(input.ShiftLeft).Type('A').MustDo()
The KeyActions
helper will automatically release all pressed keys, here the input.ShiftLeft
will be released automatically.
To simulate shortcuts input like CTRL + Enter
, you can do like this:
page.KeyActions().Press(input.ControlLeft).Type(input.Enter).MustDo()
Just click it like a human:
el := page.MustElement(`[type="checkbox"]`)
// check it if not checked
if !el.MustProperty("checked").Bool() {
el.MustClick()
}
Select options in <select>
.
The code below will select options that contains text "B" or "C":
page.MustElement("select").MustSelect("B", "C")
You can also use regex or css selector to select options:
_ = page.MustElement("select").Select([]string{`^B$`}, true, rod.SelectorTypeRegex)
// set false to deselect
_ = page.MustElement("select").Select([]string{`[value="c"]`}, false, rod.SelectorTypeCSSSector)
Use SetFiles
to set files for the file input:
page.MustElement(`[type=file]`).MustSetFiles("a.jpg", "b.pdf")
You can also use the page.Mouse
, page.Keyboard
, or page.Touch
to simulate low-level inputs.
Such as you can search the unit test for dragging to learn how to simulate dragging.