You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nightwatch自动化测试React+Redux应用:日期选择器等操作咨询

Hey there! Let's tackle each of your Nightwatch testing questions one by one—these are super common scenarios when testing React/Redux apps, so I've got some practical, actionable approaches for you:

1. How to set a value for a date picker in Nightwatch?

The approach depends on whether you're dealing with a native HTML date input or a custom React date picker (like those from Ant Design, Material-UI, etc.):

  • Native input[type="date"]: This is straightforward—just use Nightwatch's setValue method directly:

    browser.setValue('input[type="date"]', '2024-05-20'); // Format must be YYYY-MM-DD
    
  • Custom React date picker: For components that hide the native input or use a custom UI, you have two reliable options:

    1. Simulate user interaction: Click the picker to open the calendar, then select the target date:
      // Open the date picker
      browser.click('#your-date-picker-id');
      // Wait for the calendar panel to load
      browser.waitForElementVisible('.calendar-panel', 5000);
      // Click the specific date (adjust selector based on your component's structure)
      browser.click('.calendar-day[data-date="2024-05-20"]');
      
    2. Directly update state (Redux/React state): Since your app uses Redux, you can dispatch an action to set the date directly—this avoids flaky UI interactions:
      browser.executeScript(function() {
        window.store.dispatch({
          type: 'SET_SELECTED_DATE',
          payload: '2024-05-20' // Match your action's expected payload format
        });
      });
      
2. How to set a value for a location field with Google Maps autocomplete (and select an option)?

Google Maps autocomplete fields require a sequence of input + wait + select actions to mimic real user behavior:

// 1. Type your location query into the input field
browser.setValue('#location-input', 'San Francisco, CA');

// 2. Wait for the autocomplete dropdown (pac-container is Google's default class) to appear
browser.waitForElementVisible('.pac-container', 5000);

// 3. Select the desired option—you can target by index or text:
// Option A: Select the first result
browser.click('.pac-item:first-child');

// Option B: Select a specific result by text (use XPath for precise matching)
browser.click('//div[@class="pac-item" and contains(text(), "San Francisco, California, USA")]');

Note: If your app wraps the Google autocomplete in a React component, ensure the dropdown elements are fully rendered before clicking—adding a short pause (or better, a waitForElementVisible) prevents flaky tests.

3. How to set a value for a dropdown list in your React/Redux app?

Again, the method varies based on whether it's a native <select> or a custom React dropdown:

  • Native <select> element: Use setValue with the option's value attribute:

    browser.setValue('select#my-dropdown', 'option-2-value');
    
  • Custom React dropdown (e.g., Ant Design Select, Material-UI Select):

    1. Simulate user clicks:
      // Open the dropdown
      browser.click('#custom-dropdown-trigger');
      // Wait for the options menu to be visible
      browser.waitForElementVisible('.dropdown-options-list', 3000);
      // Select the desired option (adjust selector to match your component's classes)
      browser.click('.dropdown-option:contains("Option 2")');
      
    2. Redux state update (most reliable for complex components):
      browser.executeScript(function() {
        window.store.dispatch({
          type: 'SET_SELECTED_DROPDOWN_OPTION',
          payload: 'option-2-value'
        });
      });
      

This bypasses UI interactions entirely and directly updates your app's state, which is great for avoiding issues with dynamic dropdown rendering.


内容的提问来源于stack exchange,提问作者Durga Prasad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:17:29