Best Rod code snippet using rod.MustKeyActions
bot.go
Source:bot.go
...166 elem := b.GetElem(selector, ElemIndex(opt.ElemIndex))167 if elem != nil {168 log.Debug().Str("popover", selector).Msg("Found Popover")169 b.Highlight(elem)170 elem.Timeout(time.Second * b.NapTo).MustKeyActions().Press(input.Escape).MustDo()171 }172}173func (b *Bot) MustPressEscape(sel string, opts ...BotOptFunc) {174 err := b.PressEscape(sel, opts...)175 xutil.PanicIfErr(err)176}177func (b *Bot) PressEscape(sel string, opts ...BotOptFunc) (err error) {178 if elem := b.GetElem(sel, opts...); elem != nil {179 elem.MustKeyActions().Press(input.Escape).MustDo()180 return nil181 }182 return183}184func (b *Bot) PressTab(sel string, opts ...BotOptFunc) (err error) {185 if elem := b.GetElem(sel, opts...); elem != nil {186 xutil.RandSleep(0.5, 0.51)187 elem.MustKeyActions().Press(input.Tab).MustDo()188 return nil189 }190 return191}192// EnsureAndHighlight193//194// Deprecated: for some unknown reasons this sometimes cause program hung up195func (b *Bot) EnsureAndHighlight(elem *rod.Element) error {196 err := rod.Try(func() {197 b.ensureHighlight(elem)198 })199 return err200}201func (b *Bot) ensureHighlight(elem *rod.Element) {202 b.ScrollToElem(elem)203 if !elem.MustInteractable() {204 b.CloseIfHasPopovers()205 }206 b.Highlight(elem)207}208// EnsureAnyElem return the match elem209func (b *Bot) EnsureAnyElem(selectors ...string) (sel string, err error) {210 err = rod.Try(func() {211 r := b.Pg.Timeout(time.Second * b.MediumTo).Race()212 for _, s := range selectors {213 b.appendToRace(s, &sel, r)214 }215 r.MustDo()216 })217 return218}219// appendToRace:220// if directly add race.Element in EnsureAnyElem, will always return the221// last of the selectors222func (b *Bot) appendToRace(s string, sel *string, race *rod.RaceContext) {223 if funk.Contains(s, SEP) {224 ss := strings.Split(s, SEP)225 txt := strings.Join(ss[1:], SEP)226 race.ElementR(ss[0], txt).MustHandle(func(_ *rod.Element) {227 *sel = s228 })229 } else {230 race.Element(s).MustHandle(func(_ *rod.Element) {231 *sel = s232 })233 }234}235func (b *Bot) MustEnsureAnyElem(selectors ...string) string {236 start := time.Now()237 s, err := b.EnsureAnyElem(selectors...)238 xutil.PanicIfErr(err)239 cost := mathutil.ElapsedTime(start)240 log.Trace().Str("selector", s).Str("cost", cost).Msg("Ensure")241 return s242}243func (b *Bot) MustEnsureUrlHas(s string, opts ...BotOptFunc) {244 e := b.EnsureUrlHas(s, opts...)245 xutil.PanicIfErr(e)246}247func (b *Bot) EnsureUrlHas(s string, opts ...BotOptFunc) (err error) {248 opt := BotOpts{Timeout: b.MediumTo}249 BindBotOpts(&opt, opts...)250 script := fmt.Sprintf(`decodeURIComponent(window.location.href).includes("%s")`, s)251 err = rod.Try(func() {252 b.Pg.Timeout(time.Second * opt.Timeout).MustWait(script).CancelTimeout()253 })254 if err != nil {255 log.Error().Err(err).Msg(xpretty.Yellowf("Fail: %s", script))256 }257 return err258}259// MustEval260//261// a wrapper with MediumTo to rod.Page.MustEval262//263// if you want get error, please use rod.Page.Eval instead264func (b *Bot) MustEval(script string) (res string) {265 res = b.Pg.Timeout(time.Second * b.MediumTo).MustEval(script).String()266 return res267}268func (b *Bot) MustFillBar(sel, text string, opts ...BotOptFunc) (txt string) {269 txt, err := b.FillBar(sel, text, opts...)270 xutil.PanicIfErr(err)271 return txt272}273func (b *Bot) FillBar(sel, text string, opts ...BotOptFunc) (txt string, err error) {274 opt := BotOpts{Submit: false}275 BindBotOpts(&opt, opts...)276 // elem := b.Pg.Timeout(time.Second * b.ShortTo).MustElement(sel).CancelTimeout()277 elem := b.GetElem(sel, opts...)278 if elem == nil {279 return "", ErrorSelNotFound280 }281 b.CloseIfHasPopovers()282 b.Highlight(elem)283 // elem = elem.Timeout(time.Second * b.ShortTo).MustSelectAllText().MustInput(text)284 elem = b.FillAsHuman(elem, text)285 if opt.Submit {286 xutil.RandSleep(0.1, 0.15)287 // elem = elem.MustPress(input.Enter)288 elem.MustKeyActions().Press(input.Enter).MustDo()289 // return nil290 }291 // just try to get text, won't matter if fails292 txt, _ = elem.Text()293 elem.CancelTimeout()294 return295}296// FillAsHuman297//298// each time before enter (n=args[0] or 5) chars, we wait (to=args[1]/10 or 0.1) seconds299//300// @return *rod.Element301func (b *Bot) FillAsHuman(elem *rod.Element, text string, args ...int) *rod.Element {302 elem.MustSelectAllText().MustInput("")303 n := xutil.FirstOrDefaultArgs(0, args...)304 if n == 0 {305 n = xutil.AorB(b.Config.PerInputLength, 5)306 }307 arr := xutil.NewStringSlice(text, n, true)308 for _, str := range arr {309 e := elem.Input(str)310 xutil.PanicIfErr(e)311 }312 to := 0.1313 if len(args) >= 2 {314 to = cast.ToFloat64(args[1]) / 10315 }316 xutil.RandSleep(to-0.01, to+0.01)317 return elem318}319func (b *Bot) FillCharsOneByOne(elem *rod.Element, value string) {320 elem.MustKeyActions().Type([]input.Key(fmt.Sprintf("%v", value))...).MustDo()321}322// MGetElems323//324// get all elems if found by selectors325func (b *Bot) MGetElems(selectors []string, opts ...BotOptFunc) (elems []*rod.Element) {326 for _, sel := range selectors {327 b.GetElem(sel, opts...)328 e1 := b.GetElems(sel)329 elems = append(elems, e1...)330 }331 return332}333// MGetElemsAllAttr334//...
input_test.go
Source:input_test.go
...16â "Enter" Enter 13 modifiers(ctrl)17â "Control" ControlLeft 17 modifiers()18`)19 body.MustEval("() => this.innerText = ''")20 body.MustKeyActions().21 Press(input.ShiftLeft).Type('A', 'X').Release(input.ShiftLeft).22 Type('a').MustDo()23 g.Eq(body.MustText(), `â "Shift" ShiftLeft 16 modifiers(shift)24â "A" KeyA 65 modifiers(shift)25â "A" KeyA 65 modifiers(shift)26â "X" KeyX 88 modifiers(shift)27â "X" KeyX 88 modifiers(shift)28â "Shift" ShiftLeft 16 modifiers()29â "a" KeyA 65 modifiers()30â "a" KeyA 65 modifiers()31`)32 g.Nil(p.Keyboard.Release('a'))33}34func TestKeyType(t *testing.T) {35 g := setup(t)36 p := g.page.MustNavigate(g.srcFile("fixtures/input.html"))37 el := p.MustElement("[type=text]")38 el.MustKeyActions().Type('1', '2', input.Backspace, ' ').MustDo()39 el.MustKeyActions().Type('A', ' ', 'b').MustDo()40 p.MustInsertText(" test")41 p.Keyboard.MustType(input.Tab)42 g.Eq("1 A b test", el.MustText())43}44func TestKeyTypeErr(t *testing.T) {45 g := setup(t)46 p := g.page.MustNavigate(g.srcFile("fixtures/keys.html"))47 body := p.MustElement("body")48 g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})49 g.Err(body.Type('a'))50 g.mc.stubErr(1, proto.InputDispatchKeyEvent{})51 g.Err(p.Keyboard.Type('a'))52 g.mc.stubErr(2, proto.InputDispatchKeyEvent{})53 g.Err(p.Keyboard.Type('a'))...
MustKeyActions
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4 defer browser.MustClose()5 page := browser.MustPage("")6 defer page.MustClose()7 page.MustElement("input[name=q]").MustInput("rod")8 page.MustElement("input[name=btnK]").MustClick()9 page.MustElement("div#search").MustWaitVisible()10 page.MustElement("div#search").MustWaitStable()11 actions := page.MustKeyActions()12 actions.MoveTo(100, 100)13 actions.Click()14 actions.SendKeys("Hello World!")15 actions.Perform()16 fmt.Println("Done")17}18import (19func main() {20 browser := rod.New().MustConnect()21 defer browser.MustClose()22 page := browser.MustPage("")23 defer page.MustClose()24 page.MustElement("input[name=q]").MustInput("rod")25 page.MustElement("input[name=btnK]").MustClick()26 page.MustElement("div#search").MustWaitVisible()27 page.MustElement("div#search").MustWaitStable()28 actions := page.MustKeyActions()29 actions.MoveTo(100, 100)30 actions.Click()31 actions.SendKeys("Hello World!")32 actions.Perform()33 fmt.Println("Done")34}35import (36func main() {37 browser := rod.New().MustConnect()38 defer browser.MustClose()39 page := browser.MustPage("")40 defer page.MustClose()41 page.MustElement("input[name=q]").MustInput("rod")42 page.MustElement("input[name=btnK]").MustClick()43 page.MustElement("div#search").MustWaitVisible()44 page.MustElement("div#search").MustWaitStable()45 actions := page.MustKeyActions()46 actions.MoveTo(100, 100)47 actions.Click()48 actions.SendKeys("Hello World!")49 actions.Perform()50 fmt.Println("Done")51}
MustKeyActions
Using AI Code Generation
1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch())4 defer browser.MustClose()5 input := page.MustElement("#lst-ib")6 button := page.MustElement(".lsb")
MustKeyActions
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4 defer browser.MustClose()5 page.MustElement("input[name=q]").MustInput("rod").MustPress("Enter")6 page.MustWaitLoad()7 fmt.Println(page.MustElement("body").MustText())8}9import (10func main() {11 browser := rod.New().MustConnect()12 defer browser.MustClose()13 page.MustElement("input[name=q]").MustInput("rod").MustPress("Enter")14 page.MustWaitLoad()15 fmt.Println(page.MustElement("body").MustText())16}17import (18func main() {19 browser := rod.New().MustConnect()20 defer browser.MustClose()21 page.MustElement("input[name=q]").MustInput("rod").MustPress("Enter")22 page.MustWaitLoad()23 fmt.Println(page.MustElement("body").MustText())24}25import (26func main() {27 browser := rod.New().MustConnect()28 defer browser.MustClose()29 page.MustElement("input[name=q]").MustInput("rod").MustPress("Enter")30 page.MustWaitLoad()31 fmt.Println(page.MustElement("body").MustText())32}33import (34func main() {35 browser := rod.New().MustConnect()36 defer browser.MustClose()37 page.MustElement("input[name=q]").MustInput("rod").MustPress("Enter")38 page.MustWaitLoad()39 fmt.Println(page.MustElement("body").MustText())40}
MustKeyActions
Using AI Code Generation
1import (2func main() {3 x, y := robotgo.GetMousePos()4 fmt.Println("pos:", x, y)5 robotgo.MoveMouse(100, 200)6 robotgo.MoveMouseSmooth(100, 200)7 robotgo.MoveMouseSmooth(100, 200, 1.0, 100.0)8 robotgo.MoveMouseSmooth(100, 200, 1.0, 100.0, true)9 fmt.Println(robotgo.GetMousePos())10 fmt.Println(robotgo.GetMouseX())11 fmt.Println(robotgo.GetMouseY())12 robotgo.ScrollMouse(10, "up")13 robotgo.ScrollMouse(10, "down")14 robotgo.MouseToggle("down")15 robotgo.MouseToggle("up")16 robotgo.MouseDown()17 robotgo.MouseUp()18 robotgo.MouseClick()19 robotgo.MouseClick("left", false)20 robotgo.MouseClick("left", true)21 robotgo.MouseClick("right", false)22 robotgo.MouseClick("right", true)23 robotgo.MouseClick("middle", false)24 robotgo.MouseClick("middle", true")25 robotgo.MouseClick("left", true, 2)26 robotgo.MouseToggle("down", "left")
MustKeyActions
Using AI Code Generation
1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 page.MustWaitLoad()5 input := page.MustElement("#lst-ib")6 input.MustInput("rod")7 button := page.MustElement("button[name=btnK]")8 button.MustClick()9 page.MustWaitLoad()10 result := page.MustElement(".rc .r a")11 text := result.MustText()12 println(text)13 browser.MustClose()14}15import (16func main() {17 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()18 page.MustWaitLoad()19 input := page.MustElement("#lst-ib")20 input.MustInput("rod")21 button := page.MustElement("button[name=btnK]")22 button.MustClick()23 page.MustWaitLoad()24 result := page.MustElement(".rc .r a")25 text := result.MustText()26 println(text)27 browser.MustClose()28}
MustKeyActions
Using AI Code Generation
1import (2func main() {3 robotgo.MustKeyActions("enter", "a", "b", "c")4 fmt.Println("done")5}6import (7func main() {8 robotgo.MustKeyTap("enter")9 fmt.Println("done")10}11import (12func main() {13 robotgo.MustKeyToggle("enter", "up")14 fmt.Println("done")15}16import (17func main() {18 robotgo.MustKeyToggle("enter", "up")19 fmt.Println("done")20}21import (22func main() {23 robotgo.MustKeyToggle("enter", "up")24 fmt.Println("done")25}26import (27func main() {28 robotgo.MustKeyToggle("enter", "up")29 fmt.Println("done")30}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!