Completed
Push — master ( 782892...5bf53f )
by Juan Francisco
03:56
created

JavascriptTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 39
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A executeScript() 0 3 1
A evaluateScript() 0 3 1
A wait() 0 10 3
1
<?php
2
3
namespace Zumba\Mink\Driver;
4
5
use Behat\Mink\Exception\DriverException;
6
7
/**
8
 * Class JavascriptTrait
9
 * @package Zumba\Mink\Driver
10
 */
11
trait JavascriptTrait {
12
13
  /**
14
   * Executes a script on the browser
15
   * @param string $script
16
   */
17
  public function executeScript($script) {
18
    $this->browser->execute($script);
0 ignored issues
show
Bug introduced by
The property browser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
19
  }
20
21
  /**
22
   * Evaluates a script and returns the result
23
   * @param string $script
24
   * @return mixed
25
   */
26
  public function evaluateScript($script) {
27
    return $this->browser->evaluate($script);
28
  }
29
30
  /**
31
   * Waits some time or until JS condition turns true.
32
   *
33
   * @param integer $timeout timeout in milliseconds
34
   * @param string  $condition JS condition
35
   * @return boolean
36
   * @throws DriverException                  When the operation cannot be done
37
   */
38
  public function wait($timeout, $condition) {
39
    $start = microtime(true);
40
    $end = $start + $timeout / 1000.0;
41
    do {
42
      $result = $this->browser->evaluate($condition);
43
      usleep(100000);
44
    } while (microtime(true) < $end && !$result);
45
46
    return (bool)$result;
47
  }
48
49
}
50