From 119097b1540a40d3f54ae2a19fda2f51f60d53f8 Mon Sep 17 00:00:00 2001 From: eliastor Date: Wed, 30 Aug 2023 20:11:35 +0300 Subject: [PATCH] action "send_keys added" --- docs/data-sources/recipe.md | 4 ++++ internal/provider/action.go | 7 +++++++ internal/provider/data_recipe.go | 4 ++++ internal/provider/data_recipe_test.go | 3 ++- 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/docs/data-sources/recipe.md b/docs/data-sources/recipe.md index dd6eb6b..1472e3b 100644 --- a/docs/data-sources/recipe.md +++ b/docs/data-sources/recipe.md @@ -69,6 +69,10 @@ Supported actions: - **set_value**: sets value of form, input, textarea or any other element with a ".value" field. > ["set_value", "#example-After textarea", "text"]. "text" will be set in the text area. + + - **send_keys**: synthesizes the key up, char, and down events. + + > ["send_keys", "#example-After textarea", "text"] - types "text" in textarea - **text**: retrieves the visible text of the first element node matching the selector. Last argument places caught value into "values" attribute under specified key diff --git a/internal/provider/action.go b/internal/provider/action.go index 04a96f2..000e769 100644 --- a/internal/provider/action.go +++ b/internal/provider/action.go @@ -138,6 +138,13 @@ func actionBuilder(actionArgs []types.String) (*Action, error) { } selector := args[0].ValueString() dpAction = chromedp.SendKeys(selector, kb.Enter) + case "send_keys": + if len(args) != 2 { + return nil, fmt.Errorf("send_keys action expects 2 arguments (selector and value), got %d: %v", len(args), args) + } + selector := args[0].ValueString() + value := args[1].ValueString() + dpAction = chromedp.SendKeys(selector, value) default: return nil, fmt.Errorf("unknown action: %s", verb) } diff --git a/internal/provider/data_recipe.go b/internal/provider/data_recipe.go index 9217894..292d00a 100644 --- a/internal/provider/data_recipe.go +++ b/internal/provider/data_recipe.go @@ -81,6 +81,10 @@ Supported actions: - **set_value**: sets value of form, input, textarea or any other element with a ".value" field. > ["set_value", "#example-After textarea", "text"]. "text" will be set in the text area. + + - **send_keys**: synthesizes the key up, char, and down events. + + > ["send_keys", "#example-After textarea", "text"] - types "text" in textarea - **text**: retrieves the visible text of the first element node matching the selector. Last argument places caught value into "values" attribute under specified key diff --git a/internal/provider/data_recipe_test.go b/internal/provider/data_recipe_test.go index a2f32cb..099bfbd 100644 --- a/internal/provider/data_recipe_test.go +++ b/internal/provider/data_recipe_test.go @@ -51,7 +51,8 @@ data "chromedp_recipe" "test" { ["click", "#example-After div.Documentation-exampleButtonsContainer button.Documentation-exampleRunButton"], ["sleep", "3s"], ["text", "#example-After div.Documentation-exampleDetailsBody pre span.Documentation-exampleOutput", "runtext"], - ["set_value", "#example-After div.Documentation-exampleDetailsBody textarea", local.hehe_program], + ["set_value", "#example-After div.Documentation-exampleDetailsBody textarea", ""], # clear textarea + ["send_keys", "#example-After div.Documentation-exampleDetailsBody textarea", local.hehe_program], # type our hee program ["click", "#example-After div.Documentation-exampleButtonsContainer button.Documentation-exampleRunButton"], ["sleep", "3s"], ["text", "#example-After div.Documentation-exampleDetailsBody pre span.Documentation-exampleOutput", "runtext2"],