Skip to content

Latest commit

 

History

History
114 lines (74 loc) · 2.89 KB

input.md

File metadata and controls

114 lines (74 loc) · 2.89 KB

Input

Rod provides lots of methods to simulate human inputs, such as the mouse click or keyboard press.

Mouse click

To simulate the mouse click an element:

// left click
page.MustElement("button").MustClick()

// right click
_ = page.MustElement("button").Click(proto.InputMouseButtonRight, 1)

Text input

To simulate the input:

el := page.MustElement(`[type="text"]`)
el.MustInput("Jack")

fmt.Println(el.MustText()) // use MustText to get the text

Remove text from an input

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.

Time input

The supported input types are date, datetime-local, month, and time.

page.MustElement(`[type="date"]`).MustInputTime(time.Now())

Input key combinations

For example, the complete actions to input the uppercase 'a' like a human include:

  1. press and hold a Shift key
  2. press and release the A key
  3. 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()

Checkbox

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

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)

Set files

Use SetFiles to set files for the file input:

page.MustElement(`[type=file]`).MustSetFiles("a.jpg", "b.pdf")

Mouse, keyboard, and touch

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.