Flutter driver handle minimalized application Flutter driver handle minimalized application flutter flutter

Flutter driver handle minimalized application


This is a scenario where I would choose a widget test over an integration test (using flutter driver), as it is quicker to execute and easier to verify the correct behaviour

Q: How to write a test for a feature opening an external application (like a URL in a webbrowser).

A: When redirecting to a website or any other external URL using a method like launch() you want to test that you are passing the correct parameter to launch() - in your case that would be something like https://example.com/about

Implementation:You need to mock launch() and verify it has been called with the correct parameter, a test could look something like this, where your LoginPage can be passed a mock for the launcher:

class MockLauncher extends Mock {}void main() {  MockLauncher mockLauncher;   setUp() {    mockLauncher = MockLauncher();    when(mockLauncher.launch(any)).thenReturn(true);  }  testWidgets('click on About us', (WidgetTester tester)  async {    await tester.pumpWidget(LoginPage(launcher: mockLauncher.launch));    await tester.tap(find.byKey(ValueKey('about_us_link')));    verify(mockLauncher.launch('https://example.com/about'));    verifyNoMoreInteractions(mockLauncher);  });}
import 'package:url_launcher/url_launcher.dart';class LoginPage extends ...{  Function urlLauncher;  LoginPage({this.urlLauncher}) {    urlLauncher ??= launcher // use mock if it is passed in  }}

The same approach could also be used for integration testing, but I would suggest to reassess your reasons for doing so, as the test strategy above should be sufficent for most redirect scenarios.