Skip to content

Conversation

pallavigitwork
Copy link

@pallavigitwork pallavigitwork commented Aug 13, 2025

Added clear method to the server file.

Summary by CodeRabbit

  • New Features
    • Added a “clear” tool that resets the content/value of a targeted element in the Selenium server.
    • Supports existing locator inputs with an optional timeout to wait for the element before clearing.
    • Returns clear success and error messages to confirm the reset or indicate failures.

Copy link

coderabbitai bot commented Aug 13, 2025

Walkthrough

Adds a new public "clear" tool to the MCP Selenium server in src/lib/server.js. It locates an element, waits up to a timeout, and clears its content via element.clear(). Uses locatorSchema plus a text field in input, returns success/error content messages.

Changes

Cohort / File(s) Summary
MCP Selenium: add clear tool
src/lib/server.js
Introduces server.tool("clear") that clears a located element’s value after waiting (timeout default 10s); input extends locatorSchema with text string; returns success or error content messages; positioned between send_keys and get_element_text.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant MCP Server
  participant WebDriver
  participant Element

  Client->>MCP Server: tool("clear", {by, value, text, timeout})
  MCP Server->>WebDriver: findElement(by, value) with wait(timeout)
  WebDriver-->>MCP Server: Element reference
  MCP Server->>Element: clear()
  Element-->>MCP Server: result
  MCP Server-->>Client: content: "Element value reset"
  alt on error
    MCP Server-->>Client: content: "Error in reset of the element: <message>"
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

I nibble code like clover leaves,
A tidy “clear” is what it weaves—
Tap the keys, the fields reset,
Empty cups for inputs set.
With whiskers twitching, I declare:
Selenium’s clean, with one new hare-care! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/lib/server.js (2)

236-236: Add a fallback for cases where element.clear() is unreliable.

Some elements (e.g., contenteditable divs, certain custom inputs) don’t clear reliably with clear(). Provide a key-chord fallback to select-all + backspace.

-      await element.clear();
+      try {
+        await element.clear();
+      } catch (clearErr) {
+        // Fallback for elements where clear() is unreliable
+        await element.sendKeys(Key.chord(Key.CONTROL, 'a'), Key.BACK_SPACE);
+      }

226-226: Tighten wording for clarity and consistency.

Use consistent, action-oriented phrasing.

-  "resets content of the element",
+  "clears the value of an element",
@@
-        content: [{ type: 'text', text: 'Element value reset' }]
+        content: [{ type: 'text', text: 'Element cleared' }]
@@
-        content: [{ type: 'text', text: `Error in reset of the element: ${e.message}` }]
+        content: [{ type: 'text', text: `Error clearing element: ${e.message}` }]

Also applies to: 238-238, 242-242

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9c1d066 and c1bbc85.

📒 Files selected for processing (1)
  • src/lib/server.js (1 hunks)

Comment on lines +228 to +232
...locatorSchema,
text: z.string().describe("Resets content of the element")
},
async ({ by, value, text, timeout = 10000 }) => {
try {
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Remove unused required "text" input and param from the clear tool.

The tool ignores the provided "text" but still requires it via zod, forcing clients to supply meaningless data and creating API friction.

Apply this diff to drop the unused field and param:

 server.tool(
   "clear",
   "resets content of the element",
   {
-    ...locatorSchema,
-    text: z.string().describe("Resets content of the element")
+    ...locatorSchema
   },
-  async ({ by, value, text, timeout = 10000 }) => {
+  async ({ by, value, timeout = 10000 }) => {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
...locatorSchema,
text: z.string().describe("Resets content of the element")
},
async ({ by, value, text, timeout = 10000 }) => {
try {
...locatorSchema
},
async ({ by, value, timeout = 10000 }) => {
try {
🤖 Prompt for AI Agents
In src/lib/server.js around lines 228 to 232, the clear tool currently declares
and requires a "text" field in the zod schema and function signature but never
uses it; remove the unused "text" from the schema, its .describe(), and from the
async function parameter list (including defaulting and any references), and
update any callsites and TypeScript types/usages that expect that parameter so
clients are no longer forced to provide meaningless data.

Comment on lines +235 to +236
const element = await driver.wait(until.elementLocated(locator), timeout);
await element.clear();
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Wait for element to be visible and enabled before clearing.

elementLocated only ensures presence in the DOM; clear() can fail on hidden/disabled elements. Guard with visibility/enabled waits for robustness.

-      const element = await driver.wait(until.elementLocated(locator), timeout);
-      await element.clear();
+      const element = await driver.wait(until.elementLocated(locator), timeout);
+      await driver.wait(until.elementIsVisible(element), timeout);
+      await driver.wait(until.elementIsEnabled(element), timeout);
+      await element.clear();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const element = await driver.wait(until.elementLocated(locator), timeout);
await element.clear();
const element = await driver.wait(until.elementLocated(locator), timeout);
await driver.wait(until.elementIsVisible(element), timeout);
await driver.wait(until.elementIsEnabled(element), timeout);
await element.clear();
🤖 Prompt for AI Agents
In src/lib/server.js around lines 235-236, the code locates an element then
calls clear() but only ensures presence in the DOM; change the flow to wait for
the element to be visible and enabled before clearing. After obtaining the
element with driver.wait(until.elementLocated(locator), timeout), call
driver.wait(until.elementIsVisible(element), timeout) and
driver.wait(until.elementIsEnabled(element), timeout) (or an equivalent combined
visibility/enabled check) and only then call element.clear(); this makes clear()
robust against hidden/disabled elements.

@pallavigitwork
Copy link
Author

@angiejones , requesting PR for merge. Kindly consider.

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