| package human |
|
|
| import ( |
| "context" |
| "math/rand" |
| "testing" |
|
|
| "github.com/chromedp/chromedp" |
| ) |
|
|
| func TestSetHumanRandSeed(t *testing.T) { |
| |
| SetHumanRandSeed(12345) |
| } |
|
|
| func TestType(t *testing.T) { |
| text := "hello" |
|
|
| |
| actions := Type(text, false) |
| if len(actions) < len(text) { |
| t.Errorf("expected at least %d actions, got %d", len(text), len(actions)) |
| } |
|
|
| |
| fastActions := Type(text, true) |
| if len(fastActions) < len(text) { |
| t.Errorf("expected at least %d actions, got %d", len(text), len(fastActions)) |
| } |
| } |
|
|
| func TestTypeWithCorrections(t *testing.T) { |
| |
| SetHumanRandSeed(1) |
| text := "this is a very long string to increase the chance of a simulated typo correction" |
| actions := Type(text, false) |
|
|
| |
| if len(actions) < len(text)*2 { |
| t.Errorf("expected many actions for long string, got %d", len(actions)) |
| } |
| } |
|
|
| func TestMouseMove(t *testing.T) { |
| |
| ctx, _ := chromedp.NewContext(context.Background()) |
|
|
| |
| |
| _ = MouseMove(ctx, 0, 0, 100, 100) |
| } |
|
|
| func TestClick(t *testing.T) { |
| ctx, _ := chromedp.NewContext(context.Background()) |
| _ = Click(ctx, 50, 50) |
| } |
|
|
| func TestTypeWithConfig(t *testing.T) { |
| |
| cfg := &Config{ |
| Rand: rand.New(rand.NewSource(12345)), |
| } |
|
|
| |
| actions1 := TypeWithConfig("hello", false, cfg) |
|
|
| |
| cfg.Rand = rand.New(rand.NewSource(12345)) |
| actions2 := TypeWithConfig("hello", false, cfg) |
|
|
| if len(actions1) != len(actions2) { |
| t.Errorf("expected same number of actions with same seed, got %d and %d", len(actions1), len(actions2)) |
| } |
|
|
| |
| if len(actions1) < 10 { |
| t.Errorf("expected at least 10 actions, got %d", len(actions1)) |
| } |
| } |
|
|
| func TestClickElement_RequiresMinContentLength(t *testing.T) { |
| |
| |
| |
| |
| ctx, _ := chromedp.NewContext(context.Background()) |
| err := ClickElement(ctx, 0) |
| if err == nil { |
| t.Error("expected error without browser connection") |
| } |
| } |
|
|