import 'dart:async'; import 'dart:io'; import 'common.dart'; import 'package:flutter_driver/flutter_driver.dart'; /// Example Integration Test which can also run WebDriver command depending on /// the requests coming from the test methods. Future integrationDriver( {FlutterDriver driver, Function onScreenshot}) async { if (driver == null) { driver = await FlutterDriver.connect(); } // Test states that it's waiting on web driver commands. // [DriverTestMessage] is converted to string since json format causes an // error if it's used as a message for requestData. String jsonResponse = await driver.requestData(DriverTestMessage.pending().toString()); Response response = Response.fromJson(jsonResponse); // Until `integration_test` returns a [WebDriverCommandType.noop], keep // executing WebDriver commands. while (response.data != null && response.data['web_driver_command'] != null && response.data['web_driver_command'] != '${WebDriverCommandType.noop}') { final String webDriverCommand = response.data['web_driver_command']; if (webDriverCommand == '${WebDriverCommandType.screenshot}') { // Use `driver.screenshot()` method to get a screenshot of the web page. final List screenshotImage = await driver.screenshot(); final String screenshotName = response.data['screenshot_name']; final bool screenshotSuccess = await onScreenshot(screenshotName, screenshotImage); if (screenshotSuccess) { jsonResponse = await driver.requestData(DriverTestMessage.complete().toString()); } else { jsonResponse = await driver.requestData(DriverTestMessage.error().toString()); } response = Response.fromJson(jsonResponse); } else if (webDriverCommand == '${WebDriverCommandType.ack}') { // Previous command completed ask for a new one. jsonResponse = await driver.requestData(DriverTestMessage.pending().toString()); response = Response.fromJson(jsonResponse); } else { break; } } // If No-op command is sent, ask for the result of all tests. if (response.data != null && response.data['web_driver_command'] != null && response.data['web_driver_command'] == '${WebDriverCommandType.noop}') { jsonResponse = await driver.requestData(null); response = Response.fromJson(jsonResponse); print('result $jsonResponse'); } await driver.close(); if (response.allTestsPassed) { print('All tests passed.'); exit(0); } else { print('Failure Details:\n${response.formattedFailureDetails}'); exit(1); } }