Skip to content

Commit

Permalink
Add Element.Disabled() and Element.MustDisabled() (#799)
Browse files Browse the repository at this point in the history
This change allows users to check if an element is disabled. More
information about the disabled attribute can be found at:
  https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled

Similar methods already exist in other browser automation frameworks.
For example, Playwright has `elementHandle.isDisabled()`:
 https://playwright.dev/docs/api/class-elementhandle#element-handle-is-disabled

This commit is based on discussions in #798
  • Loading branch information
Jacobinski committed Feb 6, 2023
1 parent 70a7a73 commit 0d0e59e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
9 changes: 9 additions & 0 deletions element.go
Expand Up @@ -365,6 +365,15 @@ func (el *Element) Property(name string) (gson.JSON, error) {
return prop.Value, nil
}

// Disabled checks if the element is disabled.
func (el *Element) Disabled() (bool, error) {
prop, err := el.Property("disabled")
if err != nil {
return false, err
}
return prop.Bool(), nil
}

// SetFiles of the current file input element
func (el *Element) SetFiles(paths []string) error {
absPaths := utils.AbsolutePaths(paths)
Expand Down
15 changes: 15 additions & 0 deletions element_test.go
Expand Up @@ -490,6 +490,21 @@ func TestProperty(t *testing.T) {
})
}

func TestDisabled(t *testing.T) {
g := setup(t)

p := g.page.MustNavigate(g.srcFile("fixtures/input.html"))

g.False(p.MustElement("#EnabledButton").MustDisabled())
g.True(p.MustElement("#DisabledButton").MustDisabled())

g.Panic(func() {
el := p.MustElement("#EnabledButton")
g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})
el.MustDisabled()
})
}

func TestSetFiles(t *testing.T) {
g := setup(t)

Expand Down
8 changes: 8 additions & 0 deletions fixtures/input.html
Expand Up @@ -66,6 +66,14 @@

<hr />

<button type="button" id="EnabledButton">Enabled Button</button>

<button type="button" id="DisabledButton" disabled>
Disabled Button
</button>

<hr />

<input type="submit" value="submit" />
</form>
</body>
Expand Down
7 changes: 7 additions & 0 deletions must.go
Expand Up @@ -834,6 +834,13 @@ func (el *Element) MustProperty(name string) gson.JSON {
return prop
}

// MustDisabled is similar to Element.Disabled
func (el *Element) MustDisabled() bool {
disabled, err := el.Disabled()
el.e(err)
return disabled
}

// MustContainsElement is similar to Element.ContainsElement
func (el *Element) MustContainsElement(target *Element) bool {
contains, err := el.ContainsElement(target)
Expand Down

0 comments on commit 0d0e59e

Please sign in to comment.