← Back to Discord Forum

Idiomatic way of calling actions

Hi, coming from Playwright node to Playwright Java, I am puzzled about which way is the idiomatic way and pros/cons: page.fill(this.username, username); vs page.locator(this.username).fill(username); Is there an "official" option?

This thread is trying to answer question "What is the idiomatic way to call actions in Playwright Java, `page.fill(this.username, username);` or `page.locator(this.username).fill(username);`?"

2 replies

I'd personally say that the page.locator is the more idiomatic way as the page.fill option is discouraged by playwright docs.


Filling Input Fields in Playwright Java

When you're working with Playwright Java, you'll often need to fill in input fields. The most idiomatic way to do this is by using the page.fill() method. Here's how you do it:

page.fill(this.username, username);

This method takes two parameters. The first one is the locator or selector for the input field, and the second one is the value you want to fill in.

This method is super convenient because it combines locating and filling actions into a single line of code. It's a straightforward way to interact with elements on the page, and it reduces code complexity.

There's also an alternative approach where you can create a locator using page.locator(). Here's how it looks:


This method allows you to create a locator based on various attributes. Once you've created a locator, you can perform actions on it by calling .fill().

Both approaches achieve similar results, but using page.fill() is generally considered more idiomatic. It provides a cleaner syntax and achieves the same result without requiring explicit locators.

So, if you're looking to fill in an input field with a value in Playwright Java, I recommend using page.fill(this.username, username). It's a simpler and more straightforward approach. Happy coding!

Related Discord Threads

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.