flutterflutter-test

Flutter Driver select date from date picker


I would like to select a date through date picker as part of a flutter driver test. however, I can't seem to figure out exactly how I would do this?

I've tried using a find.textandfind.bySemanticsLabel, and have tried but have had no luck thus far.

my code :

Future<void> executeStep() async {
  await FlutterDriverUtils.waitForFlutter(world.driver);
  NewOrderForm newOrderForm = NewOrderForm(world.driver);
  await newOrderForm.setFieldKontrak();
  //Open Date Picker
  await newOrderForm.setDateKontrak();
  //Select date 24
  await driver.waitFor(find.bySemanticsLabel('24'));
  await driver.tap(find.text('24'),timeout: Duration(seconds: 15));
  await driver.waitFor(find.text('OK'));
  await driver.tap(find.text('OK'));
  await newOrderForm.setProyekField();

}

Screen capture :

enter image description here


Solution

  • I ran a sample test that selects a date from datepicker and it worked well. Below is what I did:

    main.dart has a simple RaisedButton upon clicking on which DatePicker opens:

    return Scaffold(
          body: Center(
            child: RaisedButton(
              onPressed: () {
                _showDatePicker();
              },
              child: Text('Click')
            )
          )
        );
    
    void _showDatePicker() async {
        DateTime picked = await showDatePicker(
            context: context,
            initialDate: new DateTime.now(),
            firstDate: new DateTime(2019),
            lastDate: new DateTime(2021)
        );
        if(picked != null) setState(() => _value = picked.toString());
      }
    

    Below is the flutter driver test that first identifies the RaisedButton -> taps on it -> finds the date to be selected -> taps on it -> taps OK

    test('datepicker test', () async {
            final dateButton = find.text('Click');
            await driver.waitFor(dateButton);
            await driver.tap(dateButton);
            await driver.tap(find.text('15'));
            await driver.tap(find.text('OK'));
            print('October 15 selected and tapped OK');
          });
    

    Test result:

    enter image description here

    In the code you provided, you may try below snippet, ie, tap on 24 and directly tap on OK instead of telling driver to wait for OK button to find.

    await driver.tap(find.text('24'));
      await driver.tap(find.text('OK'));
    

    Hope this helps you to resolve the issue.