codeant-ai-for-open-source[bot] commented on code in PR #37529:
URL: https://github.com/apache/superset/pull/37529#discussion_r2739301270


##########
superset-frontend/playwright/components/core/Menu.ts:
##########
@@ -0,0 +1,216 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { Locator, Page } from '@playwright/test';
+
+/**
+ * Menu component for Ant Design dropdown menus.
+ * Uses hover as primary approach (most natural user interaction).
+ * Falls back to keyboard navigation, then dispatchEvent if hover fails.
+ *
+ * This component handles menu content only - not the trigger that opens the 
menu.
+ * The calling page object should open the menu first, then use this component.
+ *
+ * @example
+ * // In a page object
+ * async selectDownloadOption(optionText: string): Promise<void> {
+ *   await this.openHeaderActionsMenu();
+ *   const menu = new Menu(this.page, '[data-test="header-actions-menu"]');
+ *   await menu.selectSubmenuItem('Download', optionText);
+ * }
+ */
+export class Menu {
+  private readonly page: Page;
+  private readonly locator: Locator;
+
+  private static readonly SELECTORS = {
+    SUBMENU: '.ant-dropdown-menu-submenu',
+    SUBMENU_POPUP: '.ant-dropdown-menu-submenu-popup',
+    SUBMENU_TITLE: '.ant-dropdown-menu-submenu-title',
+  } as const;
+
+  private static readonly TIMEOUTS = {
+    SUBMENU_OPEN: 5000,
+  } as const;
+
+  constructor(page: Page, selector: string);
+  constructor(page: Page, locator: Locator);
+  constructor(page: Page, selectorOrLocator: string | Locator) {
+    this.page = page;
+    if (typeof selectorOrLocator === 'string') {
+      this.locator = page.locator(selectorOrLocator);
+    } else {
+      this.locator = selectorOrLocator;
+    }
+  }
+
+  /**
+   * Opens a submenu and selects an item within it.
+   * Uses hover as primary approach, falls back to keyboard then dispatchEvent.
+   *
+   * @param submenuText - The text of the submenu to open (e.g., "Download")
+   * @param itemText - The text of the item to select (e.g., "Export YAML")
+   * @param options - Optional timeout settings
+   */
+  async selectSubmenuItem(
+    submenuText: string,
+    itemText: string,
+    options?: { timeout?: number },
+  ): Promise<void> {
+    const timeout = options?.timeout ?? Menu.TIMEOUTS.SUBMENU_OPEN;
+
+    // Try hover first (most natural user interaction)
+    let popup = await this.openSubmenuWithHover(submenuText, itemText, 
timeout);
+
+    // Fallback to keyboard navigation
+    if (!popup) {
+      popup = await this.openSubmenuWithKeyboard(
+        submenuText,
+        itemText,
+        timeout,
+      );
+    }
+
+    // Last resort: dispatchEvent
+    if (!popup) {
+      popup = await this.openSubmenuWithDispatchEvent(
+        submenuText,
+        itemText,
+        timeout,
+      );
+    }
+
+    if (!popup) {
+      throw new Error(
+        `Failed to open submenu "${submenuText}". Tried hover, keyboard, and 
dispatchEvent.`,
+      );
+    }
+
+    // Use dispatchEvent instead of click to bypass viewport and pointer 
interception
+    // issues. Ant Design renders submenu popups in a portal that can be 
positioned
+    // outside the viewport or behind chart content (e.g., large tables with 
z-index).
+    await popup
+      .getByText(itemText, { exact: true })
+      .dispatchEvent('click', { timeout });

Review Comment:
   **Suggestion:** The `timeout` option passed to `dispatchEvent` is 
incorrectly being treated as Playwright options, but for locators it is 
actually the event init object, so this value is ignored by Playwright and does 
not constrain how long it waits, which can lead to tests not timing out as 
intended when the item is slow to become actionable. [logic error]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Dashboard export E2E tests may be flaky.
   - ⚠️ Playwright menu selection may mask timing failures.
   - ⚠️ Tests for Download → Export as Example affected.
   ```
   </details>
   
   ```suggestion
         .dispatchEvent('click');
   ```
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Open the Dashboard page test flow that uses the Menu helper:
   
      - Caller: superset-frontend/playwright/pages/DashboardPage.ts at the call 
site found by
      Grep:
   
        "await menu.selectSubmenuItem('Download', optionText);" (file: 
DashboardPage.ts,
        around line 125).
   
   2. Execution enters the Menu.selectSubmenuItem implementation in:
   
      - superset-frontend/playwright/components/core/Menu.ts at the method 
starting on line
      71 (selectSubmenuItem) and reaches the final selection code at lines 
108-110 where
      dispatchEvent is invoked.
   
   3. The code calls popup.getByText(...).dispatchEvent('click', { timeout }) 
(Menu.ts lines
   108-110).
   
      - Playwright's Locator.dispatchEvent accepts an event init object 
(MouseEventInit) and
      does not interpret the passed object as a Playwright timeout; therefore 
the { timeout }
      argument becomes event data, not a timeout for Playwright's action.
   
   4. Reproduce failure: run the Dashboard Export Playwright E2E that triggers 
Download →
   Export as Example:
   
      - Command: PLAYWRIGHT_ADMIN_PASSWORD=admin INCLUDE_EXPERIMENTAL=true npx 
playwright
      test --grep "Dashboard Export" (per PR testing instructions).
   
      - If the submenu popup becomes visible but the item is not yet actionable 
(e.g., due to
      animation, overlay, or slow rendering), dispatchEvent('click', { timeout 
}) will not be
      subject to a Playwright timeout and will not wait or fail as intended. 
The test may
      either:
   
        a) Appear to succeed locally because dispatchEvent returns quickly (no 
waiting for
        actionable element), or
   
        b) Mask flakiness: the intended Playwright timeout isn't applied so 
failures that
        should be caught (e.g., element not attached/visible) may be hidden or 
tests may hang
        elsewhere.
   
   5. Evidence from Grep: the exact call exists in Menu.ts (found at
   superset-frontend/playwright/components/core/Menu.ts:110 by the search), and
   Menu.selectSubmenuItem is actively used by DashboardPage.ts
   (superset-frontend/playwright/pages/DashboardPage.ts:125), so this is on the 
real E2E path
   for dashboard export tests.
   
   6. Conclusion / why the suggested change is correct:
   
      - Removing the { timeout } parameter makes the dispatchEvent call 
semantically correct
      (it passes only the event type and eventInit if needed). If a 
Playwright-style
      waiting/timeout is required, it must be done with Playwright waiting APIs 
(e.g.,
      waitFor, waitForSelector) before calling dispatchEvent. The suggested 
improved_code
      removes the misleading timeout parameter to avoid silently passing it as 
event data.
   ```
   </details>
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset-frontend/playwright/components/core/Menu.ts
   **Line:** 110:110
   **Comment:**
        *Logic Error: The `timeout` option passed to `dispatchEvent` is 
incorrectly being treated as Playwright options, but for locators it is 
actually the event init object, so this value is ignored by Playwright and does 
not constrain how long it waits, which can lead to tests not timing out as 
intended when the item is slow to become actionable.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   ```
   </details>



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to