feat: funciones Chrome CDP para automatización de navegador

10 funciones Go en infra/ para controlar Chrome via Chrome DevTools Protocol:
chrome_launch, cdp_connect, cdp_navigate, cdp_evaluate, cdp_screenshot,
cdp_click, cdp_type_text, cdp_wait_element, cdp_get_html, cdp_close.
WebSocket RFC 6455 implementado sin dependencias externas.
Incluye tests de integración con Chrome real.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-29 17:30:56 +02:00
parent a4df450d41
commit 73bf619191
22 changed files with 1541 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
package infra
import (
"fmt"
"time"
)
// CdpWaitElement espera hasta que un selector CSS exista en el DOM.
// Hace polling con Runtime.evaluate hasta que el elemento aparezca o se agote el timeout.
// Retorna error si el timeout se agota sin encontrar el elemento.
func CdpWaitElement(c *CDPConn, selector string, timeout time.Duration) error {
if c == nil {
return fmt.Errorf("cdp wait element: conexion nula")
}
if timeout <= 0 {
timeout = 10 * time.Second
}
deadline := time.Now().Add(timeout)
interval := 200 * time.Millisecond
js := fmt.Sprintf(`document.querySelector(%q) !== null`, selector)
for time.Now().Before(deadline) {
result, err := CdpEvaluate(c, js)
if err != nil {
// Error en evaluate — puede que la pagina aun no este lista
time.Sleep(interval)
continue
}
if result == "true" {
return nil
}
time.Sleep(interval)
}
return fmt.Errorf("cdp wait element: selector %q no encontrado despues de %s", selector, timeout)
}