Is there any headless browser for dart? Or a wrapper for something like selenium? My goal is to use the browser for automated end-user testing for a website i wrote years ago. Now i need to make few changes on the site. Since it contains specific business logic, i would like to write some quick tests for the site without refactoring or modifying it for unit-tests, before i make those changes.
It seems like a fun introduction to get more familiar with dart ecosystem as well.
To automate the Flutter apps using Appium, the developer community built an Appium Flutter Driver that can be applied to multiple platforms and operating systems. This tool is able to satisfy the use cases that we listed above, which means that you can access the required elements by using the Flutter finders.
Selenium is an open-source tool that automates web browsers.
A single test file can be run just using dart test path/to/test.
You can use Chrome or Dartium and drive it with ChromeDriver and the webdriver package
Here is a quick example:
import 'dart:convert';
import 'dart:io';
import 'package:webdriver/io.dart';
main() async {
// Start the ChromeDriver process
Process chromeDriverProcess = await Process
.start('chromedriver', ['--port=4444', '--url-base=wd/hub']);
await for (String browserOut in const LineSplitter()
.bind(UTF8.decoder.bind(chromeDriverProcess.stdout))) {
if (browserOut.contains('Starting ChromeDriver')) {
break;
}
}
// Connect to it with the webdriver package
WebDriver driver = await createDriver(
uri: Uri.parse('http://localhost:4444/wd/hub/'),
desired: Capabilities.chrome);
// Go to your page
await driver.get('http://stackoverflow.com');
//TODO: write your tests
print(await driver.execute('return navigator.userAgent', []));
// Take a simple screenshot
String screenshot = await driver.captureScreenshotAsBase64();
new File('stackoverflow.png').writeAsBytesSync(BASE64.decode(screenshot));
driver.quit();
chromeDriverProcess.kill();
}
It is not totally "headless" but it is easy to make it work on server like Travis-CI with this config:
before_install:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With