Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support querying numeric HTML properties from elements::Element::prop() #289

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

johnDeSilencio
Copy link

@johnDeSilencio johnDeSilencio commented Mar 2, 2025

Preface

Hi, Jon! Longtime fan of yours. Your Crust of Rust series was HUGE for me going from a beginner Rust developer to more intermediate (I've got a long way to go before the rank of "master" 😄). Thank you for all the amazing work you do and how you give back to the community!

Problem

I'm building my own personal portfolio website. One of the features of the application is a terminal emulator, letting you run fake commands on the website.

One of the commands is clear, which clears the screen and the history of all previous commands. To write a functional test for this feature, I'd like to be able to tell that my <div id="previous-commands> has no children which I check through the childElementCount property.

However, when I try use fantoccini::elements::Element::prop("childElementCount"), I get the following error:

...webdriver returned non-conforming response: Number(0)

2025-03-01-162717_hyprshot

Proposed Solution

It appears that the fantoccini::elements::Element::prop method currently only handles Strings, Bools, and Null values from serde_json and returns an error for everything else. This PR also supports matching serde_json::Number to support getting numeric properties like childElementCount.

Tests

I've also added another assert_eq!() to the element_test function. Let me know if you'd like me to add more test coverage or to find another way to support numeric HTML properties.

Functional Tests

I used this fork in the E2E tests for my website and now my functional tests pass:

image

@@ -48,6 +48,9 @@ async fn element_prop(c: Client, port: u16) -> Result<(), error::CmdError> {
assert_eq!(elem.prop("id").await?.unwrap(), "checkbox-option-1");
assert_eq!(elem.prop("checked").await?.unwrap(), "false");
assert!(elem.attr("invalid-property").await?.is_none());

let elem = c.find(Locator::Id("content")).await?;
assert_eq!(elem.prop("childElementCount").await?.unwrap(), "5");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the number of children the <div> with ID content has in the example HTML page. If the example changes, this test will have to change as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant