...

Source file src/github.com/playwright-community/playwright-go/element_handle.go

Documentation: github.com/playwright-community/playwright-go

     1  package playwright
     2  
     3  import (
     4  	"encoding/base64"
     5  	"fmt"
     6  	"io/ioutil"
     7  )
     8  
     9  type elementHandleImpl struct {
    10  	jsHandleImpl
    11  }
    12  
    13  func (e *elementHandleImpl) AsElement() ElementHandle {
    14  	return e
    15  }
    16  
    17  func (e *elementHandleImpl) OwnerFrame() (Frame, error) {
    18  	channel, err := e.channel.Send("ownerFrame")
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	channelOwner := fromNullableChannel(channel)
    23  	if channelOwner == nil {
    24  		return nil, nil
    25  	}
    26  	return channelOwner.(*frameImpl), nil
    27  }
    28  
    29  func (e *elementHandleImpl) ContentFrame() (Frame, error) {
    30  	channel, err := e.channel.Send("contentFrame")
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	channelOwner := fromNullableChannel(channel)
    35  	if channelOwner == nil {
    36  		return nil, nil
    37  	}
    38  	return channelOwner.(*frameImpl), nil
    39  }
    40  
    41  func (e *elementHandleImpl) GetAttribute(name string) (string, error) {
    42  	attribute, err := e.channel.Send("getAttribute", map[string]interface{}{
    43  		"name": name,
    44  	})
    45  	if err != nil {
    46  		return "", err
    47  	}
    48  	if attribute == nil {
    49  		return "", nil
    50  	}
    51  	return attribute.(string), nil
    52  }
    53  
    54  func (e *elementHandleImpl) TextContent() (string, error) {
    55  	textContent, err := e.channel.Send("textContent")
    56  	if err != nil {
    57  		return "", err
    58  	}
    59  	return textContent.(string), nil
    60  }
    61  
    62  func (e *elementHandleImpl) InnerText() (string, error) {
    63  	innerText, err := e.channel.Send("innerText")
    64  	if err != nil {
    65  		return "", err
    66  	}
    67  	return innerText.(string), nil
    68  }
    69  
    70  func (e *elementHandleImpl) InnerHTML() (string, error) {
    71  	innerHTML, err := e.channel.Send("innerHTML")
    72  	if err != nil {
    73  		return "", err
    74  	}
    75  	return innerHTML.(string), nil
    76  }
    77  
    78  func (e *elementHandleImpl) DispatchEvent(typ string, initObjects ...interface{}) error {
    79  	var initObject interface{}
    80  	if len(initObjects) == 1 {
    81  		initObject = initObjects[0]
    82  	}
    83  	_, err := e.channel.Send("dispatchEvent", map[string]interface{}{
    84  		"type":      typ,
    85  		"eventInit": serializeArgument(initObject),
    86  	})
    87  	return err
    88  }
    89  
    90  func (e *elementHandleImpl) Hover(options ...ElementHandleHoverOptions) error {
    91  	_, err := e.channel.Send("hover", options)
    92  	return err
    93  }
    94  
    95  func (e *elementHandleImpl) Click(options ...ElementHandleClickOptions) error {
    96  	_, err := e.channel.Send("click", options)
    97  	return err
    98  }
    99  
   100  func (e *elementHandleImpl) Dblclick(options ...ElementHandleDblclickOptions) error {
   101  	_, err := e.channel.Send("dblclick", options)
   102  	return err
   103  }
   104  
   105  func (e *elementHandleImpl) QuerySelector(selector string) (ElementHandle, error) {
   106  	channel, err := e.channel.Send("querySelector", map[string]interface{}{
   107  		"selector": selector,
   108  	})
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  	if channel == nil {
   113  		return nil, nil
   114  	}
   115  	return fromChannel(channel).(*elementHandleImpl), nil
   116  }
   117  
   118  func (e *elementHandleImpl) QuerySelectorAll(selector string) ([]ElementHandle, error) {
   119  	channels, err := e.channel.Send("querySelectorAll", map[string]interface{}{
   120  		"selector": selector,
   121  	})
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  	elements := make([]ElementHandle, 0)
   126  	for _, channel := range channels.([]interface{}) {
   127  		elements = append(elements, fromChannel(channel).(*elementHandleImpl))
   128  	}
   129  	return elements, nil
   130  }
   131  
   132  func (e *elementHandleImpl) EvalOnSelector(selector string, expression string, options ...interface{}) (interface{}, error) {
   133  	var arg interface{}
   134  	forceExpression := false
   135  	if !isFunctionBody(expression) {
   136  		forceExpression = true
   137  	}
   138  	if len(options) == 1 {
   139  		arg = options[0]
   140  	} else if len(options) == 2 {
   141  		arg = options[0]
   142  		forceExpression = options[1].(bool)
   143  	}
   144  	result, err := e.channel.Send("evalOnSelector", map[string]interface{}{
   145  		"selector":   selector,
   146  		"expression": expression,
   147  		"isFunction": !forceExpression,
   148  		"arg":        serializeArgument(arg),
   149  	})
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  	return parseResult(result), nil
   154  }
   155  
   156  func (e *elementHandleImpl) EvalOnSelectorAll(selector string, expression string, options ...interface{}) (interface{}, error) {
   157  	var arg interface{}
   158  	forceExpression := false
   159  	if !isFunctionBody(expression) {
   160  		forceExpression = true
   161  	}
   162  	if len(options) == 1 {
   163  		arg = options[0]
   164  	} else if len(options) == 2 {
   165  		arg = options[0]
   166  		forceExpression = options[1].(bool)
   167  	}
   168  	result, err := e.channel.Send("evalOnSelectorAll", map[string]interface{}{
   169  		"selector":   selector,
   170  		"expression": expression,
   171  		"isFunction": !forceExpression,
   172  		"arg":        serializeArgument(arg),
   173  	})
   174  	if err != nil {
   175  		return nil, err
   176  	}
   177  	return parseResult(result), nil
   178  }
   179  
   180  func (e *elementHandleImpl) ScrollIntoViewIfNeeded(options ...ElementHandleScrollIntoViewIfNeededOptions) error {
   181  	_, err := e.channel.Send("scrollIntoViewIfNeeded", options)
   182  	if err != nil {
   183  		return err
   184  	}
   185  	return err
   186  }
   187  
   188  func (e *elementHandleImpl) SetInputFiles(files []InputFile, options ...ElementHandleSetInputFilesOptions) error {
   189  	_, err := e.channel.Send("setInputFiles", map[string]interface{}{
   190  		"files": normalizeFilePayloads(files),
   191  	}, options)
   192  	return err
   193  }
   194  
   195  // Rect is the return structure for ElementHandle.BoundingBox()
   196  type Rect struct {
   197  	Width  int `json:"width"`
   198  	Height int `json:"height"`
   199  	X      int `json:"x"`
   200  	Y      int `json:"y"`
   201  }
   202  
   203  func (e *elementHandleImpl) BoundingBox() (*Rect, error) {
   204  	boundingBox, err := e.channel.Send("boundingBox")
   205  	if err != nil {
   206  		return nil, err
   207  	}
   208  	out := &Rect{}
   209  	remapMapToStruct(boundingBox, out)
   210  	return out, nil
   211  }
   212  
   213  func (e *elementHandleImpl) Check(options ...ElementHandleCheckOptions) error {
   214  	_, err := e.channel.Send("check", options)
   215  	return err
   216  }
   217  
   218  func (e *elementHandleImpl) Uncheck(options ...ElementHandleUncheckOptions) error {
   219  	_, err := e.channel.Send("uncheck", options)
   220  	return err
   221  }
   222  
   223  func (e *elementHandleImpl) Press(key string, options ...ElementHandlePressOptions) error {
   224  	_, err := e.channel.Send("press", map[string]interface{}{
   225  		"key": key,
   226  	}, options)
   227  	return err
   228  }
   229  
   230  func (e *elementHandleImpl) Fill(value string, options ...ElementHandleFillOptions) error {
   231  	_, err := e.channel.Send("fill", map[string]interface{}{
   232  		"value": value,
   233  	}, options)
   234  	return err
   235  }
   236  
   237  func (e *elementHandleImpl) Type(value string, options ...ElementHandleTypeOptions) error {
   238  	_, err := e.channel.Send("type", map[string]interface{}{
   239  		"text": value,
   240  	}, options)
   241  	return err
   242  }
   243  
   244  func (e *elementHandleImpl) Focus() error {
   245  	_, err := e.channel.Send("focus")
   246  	return err
   247  }
   248  
   249  func (e *elementHandleImpl) SelectText(options ...ElementHandleSelectTextOptions) error {
   250  	_, err := e.channel.Send("selectText", options)
   251  	return err
   252  }
   253  
   254  func (e *elementHandleImpl) Screenshot(options ...ElementHandleScreenshotOptions) ([]byte, error) {
   255  	var path *string
   256  	if len(options) > 0 {
   257  		path = options[0].Path
   258  	}
   259  	data, err := e.channel.Send("screenshot", options)
   260  	if err != nil {
   261  		return nil, fmt.Errorf("could not send message :%w", err)
   262  	}
   263  	image, err := base64.StdEncoding.DecodeString(data.(string))
   264  	if err != nil {
   265  		return nil, fmt.Errorf("could not decode base64 :%w", err)
   266  	}
   267  	if path != nil {
   268  		if err := ioutil.WriteFile(*path, image, 0644); err != nil {
   269  			return nil, err
   270  		}
   271  	}
   272  	return image, nil
   273  }
   274  
   275  func (e *elementHandleImpl) Tap(options ...ElementHandleTapOptions) error {
   276  	_, err := e.channel.Send("tap", options)
   277  	return err
   278  }
   279  
   280  func (e *elementHandleImpl) SelectOption(values SelectOptionValues, options ...ElementHandleSelectOptionOptions) ([]string, error) {
   281  	opts := convertSelectOptionSet(values)
   282  	selected, err := e.channel.Send("selectOption", opts, options)
   283  	if err != nil {
   284  		return nil, err
   285  	}
   286  
   287  	return transformToStringList(selected), nil
   288  }
   289  
   290  func (e *elementHandleImpl) IsChecked() (bool, error) {
   291  	checked, err := e.channel.Send("isChecked")
   292  	if err != nil {
   293  		return false, err
   294  	}
   295  	return checked.(bool), nil
   296  }
   297  
   298  func (e *elementHandleImpl) IsDisabled() (bool, error) {
   299  	disabled, err := e.channel.Send("isDisabled")
   300  	if err != nil {
   301  		return false, err
   302  	}
   303  	return disabled.(bool), nil
   304  }
   305  
   306  func (e *elementHandleImpl) IsEditable() (bool, error) {
   307  	editable, err := e.channel.Send("isEditable")
   308  	if err != nil {
   309  		return false, err
   310  	}
   311  	return editable.(bool), nil
   312  }
   313  
   314  func (e *elementHandleImpl) IsEnabled() (bool, error) {
   315  	enabled, err := e.channel.Send("isEnabled")
   316  	if err != nil {
   317  		return false, err
   318  	}
   319  	return enabled.(bool), nil
   320  }
   321  
   322  func (e *elementHandleImpl) IsHidden() (bool, error) {
   323  	hidden, err := e.channel.Send("isHidden")
   324  	if err != nil {
   325  		return false, err
   326  	}
   327  	return hidden.(bool), nil
   328  }
   329  
   330  func (e *elementHandleImpl) IsVisible() (bool, error) {
   331  	visible, err := e.channel.Send("isVisible")
   332  	if err != nil {
   333  		return false, err
   334  	}
   335  	return visible.(bool), nil
   336  }
   337  
   338  func (e *elementHandleImpl) WaitForElementState(state string, options ...ElementHandleWaitForElementStateOptions) error {
   339  	_, err := e.channel.Send("waitForElementState", map[string]interface{}{
   340  		"state": state,
   341  	}, options)
   342  	if err != nil {
   343  		return err
   344  	}
   345  	return nil
   346  }
   347  
   348  func (e *elementHandleImpl) WaitForSelector(selector string, options ...ElementHandleWaitForSelectorOptions) (ElementHandle, error) {
   349  	ch, err := e.channel.Send("waitForSelector", map[string]interface{}{
   350  		"selector": selector,
   351  	}, options)
   352  	if err != nil {
   353  		return nil, err
   354  	}
   355  
   356  	channelOwner := fromNullableChannel(ch)
   357  	if channelOwner == nil {
   358  		return nil, nil
   359  	}
   360  	return channelOwner.(*elementHandleImpl), nil
   361  }
   362  
   363  func (e *elementHandleImpl) InputValue(options ...ElementHandleInputValueOptions) (string, error) {
   364  	result, err := e.channel.Send("inputValue", options)
   365  	return result.(string), err
   366  }
   367  
   368  func (e *elementHandleImpl) SetChecked(checked bool, options ...ElementHandleSetCheckedOptions) error {
   369  	if checked {
   370  		_, err := e.channel.Send("check", options)
   371  		return err
   372  	} else {
   373  		_, err := e.channel.Send("uncheck", options)
   374  		return err
   375  	}
   376  }
   377  
   378  func newElementHandle(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *elementHandleImpl {
   379  	bt := &elementHandleImpl{}
   380  	bt.createChannelOwner(bt, parent, objectType, guid, initializer)
   381  	return bt
   382  }
   383  
   384  func normalizeFilePayloads(files []InputFile) []map[string]string {
   385  	out := make([]map[string]string, 0)
   386  	for _, file := range files {
   387  		out = append(out, map[string]string{
   388  			"name":     file.Name,
   389  			"mimeType": file.MimeType,
   390  			"buffer":   base64.StdEncoding.EncodeToString(file.Buffer),
   391  		})
   392  	}
   393  	return out
   394  }
   395  
   396  func transformToStringList(in interface{}) []string {
   397  	s := in.([]interface{})
   398  
   399  	var out []string
   400  	for _, v := range s {
   401  		out = append(out, v.(string))
   402  	}
   403  	return out
   404  }
   405  

View as plain text