clickOutside method

  1. @override
Future<void> clickOutside()
override

Clicks outside of the current element.

If the current element does not exist or is not displayed, do nothing.

Implementation

@override
Future<void> clickOutside() async {
  if (!exists || !displayed) return;

  final rect = getBoundingClientRect();
  _retryWhenStale<void>(() {
    final bodyElement = _utils.byTag('body');
    final bodyRect = bodyElement.getBoundingClientRect();
    if (!rect.intersects(bodyRect)) {
      // No intersection. Just click the body which is outside of [_single].
      bodyElement.click();
      return;
    }

    // Find a [Point] that is not in the current element.
    final point = <Point<num>?>[
      bodyRect.topLeft,
      bodyRect.topRight,
      bodyRect.bottomLeft,
      bodyRect.bottomRight
    ].firstWhere((p) => !rect.containsPoint(p!), orElse: (() => null));

    if (point != null) {
      _utils.driver.mouse.moveTo(
          element: bodyElement.contextSync as sync_wd.WebElement?,
          xOffset: point.x.toInt() - (bodyRect.left as int),
          yOffset: point.y.toInt() - (bodyRect.top as int));
      _utils.driver.mouse.click();
    } else {
      throw PageLoaderException(
          'Could not click outside of the current element [$this].'
          ' It is because it covers the whole <body>.');
    }
  });
}