Skip to content

Commit

Permalink
Merge pull request #2 from eliastor/develop
Browse files Browse the repository at this point in the history
release 0.0.2
  • Loading branch information
eliastor authored Aug 30, 2023
2 parents 116eff5 + 5a40490 commit b9db971
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ make test
- [x] Focus
- [x] Cookies (setting cookies)
- [x] Screenshots
- [x] Text into fields

## Roadmap

- [ ] browserless.io support (untested, but should work)
- [ ] Attribute with cookie
- [ ] Support of selects and other form elements
- [ ] Downloads
- [ ] Uploads
- [ ] Send keys
Expand Down
4 changes: 4 additions & 0 deletions docs/data-sources/recipe.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ Supported actions:
> ["value", "#example-After textarea", "text"]

in values["text"] one can find caught value.

- **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.

- **text**: retrieves the visible text of the first element node matching the selector. Last argument places caught value into "values" attribute under specified key

Expand Down
14 changes: 14 additions & 0 deletions internal/provider/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/kb"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand Down Expand Up @@ -124,6 +125,19 @@ func actionBuilder(actionArgs []types.String) (*Action, error) {
}
return nil
})
case "set_value":
if len(args) != 2 {
return nil, fmt.Errorf("set_value action expects 2 arguments (selector and value), got %d: %v", len(args), args)
}
selector := args[0].ValueString()
value := args[1].ValueString()
dpAction = chromedp.SetValue(selector, value)
case "press_enter":
if len(args) != 0 {
return nil, fmt.Errorf("press_enter action expects 0 arguments, got %d: %v", len(args), args)
}
selector := args[0].ValueString()
dpAction = chromedp.SendKeys(selector, kb.Enter)
default:
return nil, fmt.Errorf("unknown action: %s", verb)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/provider/ctx_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var (

func chromedpCtxWithLocalChrome() ctxCreatorFunc {
return func(parentCtx context.Context) (context.Context, context.CancelFunc) {
return chromedp.NewContext(parentCtx)
ctx, _ := chromedp.NewExecAllocator(parentCtx, append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Env("POWEREDBY=eliastor"))...)
return chromedp.NewContext(ctx)
}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/provider/data_recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ Supported actions:
> ["value", "#example-After textarea", "text"]
in values["text"] one can find caught value.
- **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.
- **text**: retrieves the visible text of the first element node matching the selector. Last argument places caught value into "values" attribute under specified key
Expand Down
15 changes: 15 additions & 0 deletions internal/provider/data_recipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,24 @@ func TestAccRecipeDataSource(t *testing.T) {

resource.TestCheckResourceAttr("data.chromedp_recipe.test", "actions.0.0", "navigate"),
resource.TestCheckResourceAttr("data.chromedp_recipe.test", "values.text", "package main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n)\n\nvar c chan int\n\nfunc handle(int) {}\n\nfunc main() {\n\tselect {\n\tcase m := <-c:\n\t\thandle(m)\n\tcase <-time.After(10 * time.Second):\n\t\tfmt.Println(\"timed out\")\n\t}\n}\n"),
resource.TestCheckResourceAttr("data.chromedp_recipe.test", "values.runtext2", "hehe\n"),
),
},
},
})
}

const testRecipeDataSourceConfig = `
locals {
hehe_program=<<-EOT
package main
import "fmt"
func main() {
fmt.Println("hehe")
}
EOT
}
data "chromedp_recipe" "test" {
screenshot_filename = "test.png"
actions = [
Expand All @@ -40,6 +51,10 @@ 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],
["click", "#example-After div.Documentation-exampleButtonsContainer button.Documentation-exampleRunButton"],
["sleep", "3s"],
["text", "#example-After div.Documentation-exampleDetailsBody pre span.Documentation-exampleOutput", "runtext2"],
]
}
output "test" {
Expand Down

0 comments on commit b9db971

Please sign in to comment.