141 lines
3.6 KiB
Go
141 lines
3.6 KiB
Go
package webdriver
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.milar.in/milarin/slices"
|
|
)
|
|
|
|
// docs: https://w3c.github.io/webdriver/
|
|
|
|
func (d *Driver) Navigate(url string) error {
|
|
_, err := d.DoSessionRequest(http.MethodPost, "/url", map[string]interface{}{"url": url})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *Driver) GetUrl() (string, error) {
|
|
data, err := d.DoSessionRequest(http.MethodGet, "/url", nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return data["value"].(string), nil
|
|
}
|
|
|
|
func (d *Driver) GetTitle() (string, error) {
|
|
data, err := d.DoSessionRequest(http.MethodGet, "/title", nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return data["value"].(string), nil
|
|
}
|
|
|
|
func (d *Driver) FindElement(selector string) (string, error) {
|
|
req := map[string]interface{}{
|
|
"using": "css selector",
|
|
"value": selector,
|
|
}
|
|
|
|
data, err := d.DoSessionRequest(http.MethodPost, "/element", req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return data["value"].(map[string]interface{})[webElementIdentifier].(string), nil
|
|
}
|
|
|
|
func (d *Driver) FindElements(selector string) ([]string, error) {
|
|
req := map[string]interface{}{
|
|
"using": "css selector",
|
|
"value": selector,
|
|
}
|
|
|
|
data, err := d.DoSessionRequest(http.MethodPost, "/elements", req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return slices.Map(data["value"].([]interface{}), func(e interface{}) string {
|
|
return e.(map[string]interface{})[webElementIdentifier].(string)
|
|
}), nil
|
|
}
|
|
|
|
func (d *Driver) GetElementTagName(elementID string) (string, error) {
|
|
data, err := d.DoSessionRequest(http.MethodGet, fmt.Sprintf("/element/%s/name", elementID), nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return data["value"].(string), nil
|
|
}
|
|
|
|
func (d *Driver) GetElementText(elementID string) (string, error) {
|
|
data, err := d.DoSessionRequest(http.MethodGet, fmt.Sprintf("/element/%s/text", elementID), nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return data["value"].(string), nil
|
|
}
|
|
|
|
func (d *Driver) GetElementRect(elementID string) (*Rect, error) {
|
|
data, err := d.DoSessionRequest(http.MethodGet, fmt.Sprintf("/element/%s/rect", elementID), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rect := data["value"].(map[string]interface{})
|
|
return &Rect{
|
|
X: rect["x"].(float64),
|
|
Y: rect["y"].(float64),
|
|
Width: rect["width"].(float64),
|
|
Height: rect["height"].(float64),
|
|
}, nil
|
|
}
|
|
|
|
func (d *Driver) GetElementCssValue(elementID, property string) (string, error) {
|
|
data, err := d.DoSessionRequest(http.MethodGet, fmt.Sprintf("/element/%s/css/%s", elementID, property), nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return data["value"].(string), nil
|
|
}
|
|
|
|
func (d *Driver) GetElementProperty(elementID, property string) (string, error) {
|
|
data, err := d.DoSessionRequest(http.MethodGet, fmt.Sprintf("/element/%s/property/%s", elementID, property), nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return data["value"].(string), nil
|
|
}
|
|
|
|
func (d *Driver) GetElementAttribute(elementID, attribute string) (string, error) {
|
|
data, err := d.DoSessionRequest(http.MethodGet, fmt.Sprintf("/element/%s/attribute/%s", elementID, attribute), nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return data["value"].(string), nil
|
|
}
|
|
|
|
func (d *Driver) ClickElement(elementID string) error {
|
|
data, err := d.DoSessionRequest(http.MethodPost, fmt.Sprintf("/element/%s/click", elementID), nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("%#v\n", data)
|
|
return nil
|
|
}
|
|
|
|
func (d *Driver) SendKeysToElement(elementID, text string) error {
|
|
req := map[string]interface{}{
|
|
"text": text,
|
|
}
|
|
|
|
data, err := d.DoSessionRequest(http.MethodPost, fmt.Sprintf("/element/%s/value", elementID), req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("%#v\n", data)
|
|
return nil
|
|
}
|