Skip to content

Setup Code

Add package

TestSweets comes with its own Flutter package that allows all the magic to happen. Open up your pubspec.yaml file and add the testsweets package.

dependencies:
...
testsweets: ^1.15.0

Initialise TestSweets

After adding the package above we will do an internal TestSweets initialise. We do this by calling the setupTestSweets function before we run the app.

If your main function is not async then you can make it async so we can call the setup function and await. Update your main function in main.dart to look like this.

Future<void> main() async {
await setupTestSweets();
runApp(MyApp());
}

Using TestSweets Overlay UI

The Material and Cupertino app has a builder property you can supply. This allows us to add our testing tools ontop of your app.

return MaterialApp(
title: 'TestSweets Demo',
builder: (context, child) => TestSweetsOverlayView(
projectId: '[project-id]', // <==== Use your ProjectId here
child: child!,
),
...
);

Tracking the routes

The next thing we want to do is make sure TestSweets knows what view we are currently on. To do that we can add the TestSweetsNavigatorObserver to the navigatorObservers property.

return MaterialApp(
...
navigatorObservers: [
TestSweetsNavigatorObserver(),
],
);

If you’re using go_router you can add these observers to all your Shell Routes

Tracking Bottom Bar Navigations (optional)

This last part is optional. If you have bottom navigation and you swap out the views based on an indedx, it means that your view path won’t change when swapping tabs.

But we still need TestSweets to treat it as different views. To achieve that all we have to do is tell TestSweets when the tab has changed and pass in the index number.

To do that you can add the code below in the function

TestSweetsNavigatorObserver.instance.setBottomNavIndex(
viewName: Routes.mainView,
index: index,
);

And with that we are 100% ready to start automating. 6 lines of code, and now your end-to-end testing can be done without writing any code additional code.