Reputation: 13
I have tried testing in flutter with flutter_test and flutter_driver. Able to achieve UI testing with tap functions. But my requirement is to test forms end to end(i.e automatically fill forms). Facing issue with driver.enterText(), its not getting executed and the test cases stops executing from here.
Below is the code that i have tried :
test('Going back to customers tab', () async {
// First, tap the button.
final customerTabFinder = find.text('Customers');
await driver.waitFor(customerTabFinder);
await driver.tap(customerTabFinder);
await driver.getText(find.text("Customer"));
await takeScreenshot(driver, 'screenshots/incremented1.png');
});
test("Adding customers", () async {
final pressAddCustomerButton = find.byTooltip("Increment");
await driver.waitFor(pressAddCustomerButton);
await driver.tap(pressAddCustomerButton);
print("Add customer page is opened");
});
test("Adding text in textfield", () async {
await driver.tap(find.byValueKey("CustomerBusinessName"));
await sleep(Duration(seconds: 5));
await driver.enterText('Hello !');
await driver.tap(find.byValueKey("CustomerPhoneNumber"));
await driver.enterText('1234567890');
});
Upvotes: 1
Views: 3510
Reputation: 442
Now you should approach this problem slightly differently. You can use tester for both creating the element and interacting with it. Docs
await tester.pumpWidget(testWidget);
await tester.enterText(find.byKey(Key("titleInput")), "title");
Upvotes: 1
Reputation: 11669
You'll first need to tell driver
to wait till the element ie CustomerBusinessName
textField is located and then tap on it and then directly enter text. No need to wait or sleep for 5 seconds between tap and enter text actions, because driver first needs to locate the element and then perform actions on it. I tried by rendering two TextFields
and was able to enter text in both of them properly. Here's working sample code:
main.dart
:
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Form(
key: Key('inputTextField'),
child: TextField(
decoration: InputDecoration(
hintText: 'Enter name'
),
)
),
Form(
key: Key('inputTextField1'),
child: TextField(
decoration: InputDecoration(
hintText: 'Enter phone'
),
)
)
],
)
)
driver test
:
test('enter text', () async {
final formFinder = find.byValueKey('inputTextField');
final formFinder1 = find.byValueKey('inputTextField1');
await driver.waitFor(formFinder);
await driver.tap(formFinder);
await driver.enterText('Hello');
print('entered text');
await driver.waitFor(formFinder1);
await driver.tap(formFinder1);
await driver.enterText('123456');
print('entered number');
});
test result:
Hope this answers your question.
Upvotes: 2