Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do automated browser testing with Dart?

Tags:

dart

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.

like image 773
Ahm Lenx Avatar asked Feb 13 '17 18:02

Ahm Lenx


People also ask

How do you automate a flutter Web application?

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.

Can selenium automate browser?

Selenium is an open-source tool that automates web browsers.

How do you run a dart test?

A single test file can be run just using dart test path/to/test.


1 Answers

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
like image 170
Xavier Avatar answered Oct 01 '22 09:10

Xavier