Skip to content

Instantly share code, notes, and snippets.

@bartekpacia
Last active June 5, 2023 13:20
Show Gist options
  • Save bartekpacia/40aba128bcbfac15018e2a39c5b097c0 to your computer and use it in GitHub Desktop.
Save bartekpacia/40aba128bcbfac15018e2a39c5b097c0 to your computer and use it in GitHub Desktop.
Fluttercon snippets
testWidgets('signs up', (WidgetTester tester) async {
await tester.pumpWidget(ExampleApp());
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(Key('emailTextField')),
'charlie@root.me',
);
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(Key('nameTextField')),
'Charlie',
);
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(Key('passwordTextField')),
'ny4ncat',
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(Key('termsCheckbox')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(Key('signUpButton')));
await tester.pumpAndSettle();
expect(find.text('Welcome, Charlie!'), findsOneWidget);
});
await tester.tap(
find.ancestor(
of: find.text('Activated'),
matching: find.descendant(
of: find.byType(ListTile),
matching: find.byKey(Key('learnMore')),
),
).first,
);
import 'alpha_test.dart' as alpha_test;
import 'bravo_test.dart' as bravo_test;
import 'delta_test.dart' as delta_test;
import 'zulu_test.dart' as zulu_test;
void main(){
group('bundled tests', () {
alpha_test.main();
bravo_test.main();
delta_test.main();
zulu_test.main();
});
}
patrolTest('demo', nativeAutomation: true, (PatrolTester $) async {
await $.pumpWidgetAndSettle(ExampleApp());
// prepare network conditions
await $.native.enableCellular();
await $.native.disableWifi();
// toggle system theme
await $.native.enableDarkMode();
// handle permission request dialog about location
await $.native.selectFineLocation();
await $.native.grantPermissionWhenInUse();
// open drawer and tap on the first notification
await $.native.openNotifications();
await $.native.tapOnNotificationByIndex(0) ;
});
patrolTest(
'grants various permissions',
nativeAutomation: true,
(PatrolTester $) async {
await $.pumpWidgetAndSettle(ExampleApp());
await $('Open permissions screen').scrollTo().tap();
await $('Request camera permission').tap();
await $.native.grantPermissionWhenInUse();
await $('Request microphone permission').tap();
await $.native.grantPermissionWhenInUse();
await $('Request contacts permission').tap();
await $.native.denyPermission();
},
);
patrolTest(
'taps on notification',
nativeAutomation: true,
(PatrolTester $) async {
await $.pumpWidgetAndSettle(ExampleApp());
await $('Open notifications screen').tap();
await $.native.grantPermissionWhenInUse();
await $('Show in a few seconds').tap();
await $.native.pressHome();
await $.native.openNotifications();
await $.native.tapOnNotificationBySelector(
Selector(textContains: 'Someone liked'),
);
await $(RegExp('ID: 1')).waitUntilVisible();
},
);
patrolTest(
'accepts location permission',
nativeAutomation: true,
(PatrolTester $) async {
await $.pumpWidgetAndSettle(ExampleApp());
await $('Open location screen').scrollTo().tap();
await $('Grant permission').tap() ;
await $.native.selectCoarseLocation();
await $.native.selectFineLocation();
await $.native.selectCoarseLocation();
await $.native.selectFineLocation();
await $.native.grantPermission0nlyThisTime();
await $(RegExp('lat')).waitUntilVisible();
await $(RegExp('Ing')).waitUntilVisible();
},
);
await $('Sign in').tap();
await $('Email').enterText('charlie@root.me');
$(ListView) // -> find.byType(ListView)
$('Subscribe') // -> find.text('Subscribe')
$(#signInButton) // -> find.byKey(Key('signInButton'))
$(Icons.add) // -> find.byIcon(Icons.add)
$(RegExp(r'\d+ USD')) // -> find.textContaining(RegExp(r'\d+ USD'))
testWidgets('signs up', (WidgetTester tester) async {
await tester.pumpWidget(ExampleApp());
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(Key('emailTextField')),
'charlie@root.me',
);
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(Key('nameTextField')),
'Charlie',
);
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(Key('passwordTextField')),
'ny4ncat',
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(Key('termsCheckbox')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(Key('signUpButton')));
await tester.pumpAndSettle();
expect(find.text('Welcome, Charlie!'), findsOneWidget);
});
patrolTest('signs up', (PatrolTester $) async {
await $.pumpWidgetAndSettle(ExampleApp());
await $(#emailTextField).enterText('charlie@root.me');
await $(#nameTextField).enterText('Charlie');
await $(#passwordTextField).enterText('ny4ncat');
await $(#termsCheckbox).tap();
await $(#signUpButton).tap();
await $('Welcome, Charlie!').waitUntilVisible();
});
// TODO: Wait until below is visible
await tester.tap(
find.descendant(
of: find.byType(ListView),
matching: find.text('Subscribe'),
).first,
);
await tester.pumpAndSettle();
await $(ListView).$('Subscribe').tap();
// TODO: Wait until below is visible
await tester.tap(
find.ancestor(
of: find.text('Activated'),
matching: find.descendant(
of: find.byType(ListTile),
matching: find.byKey(Key('learnMore')),
),
).first,
);
await tester.pumpAndSettle();
await $(ListTile).containing('Activated').$(#learnMore).tap();
patrolTest('adds comment', (PatrolTester $) async {
final WidgetTester tester = $.tester;
await tester.enterText(
find.byKey(Key('commentTextField')),
'Everything is a widget (not really)',
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment