1<?php
2
3namespace Facebook\WebDriver;
4
5use Facebook\WebDriver\Remote\DriverCommand;
6use Facebook\WebDriver\Remote\ExecuteMethod;
7
8/**
9 * Managing timeout behavior for WebDriver instances.
10 */
11class WebDriverTimeouts
12{
13    /**
14     * @var ExecuteMethod
15     */
16    protected $executor;
17    /**
18     * @var bool
19     */
20    protected $isW3cCompliant;
21
22    public function __construct(ExecuteMethod $executor, $isW3cCompliant = false)
23    {
24        $this->executor = $executor;
25        $this->isW3cCompliant = $isW3cCompliant;
26    }
27
28    /**
29     * Specify the amount of time the driver should wait when searching for an element if it is not immediately present.
30     *
31     * @param int $seconds Wait time in second.
32     * @return WebDriverTimeouts The current instance.
33     */
34    public function implicitlyWait($seconds)
35    {
36        if ($this->isW3cCompliant) {
37            $this->executor->execute(
38                DriverCommand::IMPLICITLY_WAIT,
39                ['implicit' => $seconds * 1000]
40            );
41
42            return $this;
43        }
44
45        $this->executor->execute(
46            DriverCommand::IMPLICITLY_WAIT,
47            ['ms' => $seconds * 1000]
48        );
49
50        return $this;
51    }
52
53    /**
54     * Set the amount of time to wait for an asynchronous script to finish execution before throwing an error.
55     *
56     * @param int $seconds Wait time in second.
57     * @return WebDriverTimeouts The current instance.
58     */
59    public function setScriptTimeout($seconds)
60    {
61        if ($this->isW3cCompliant) {
62            $this->executor->execute(
63                DriverCommand::SET_SCRIPT_TIMEOUT,
64                ['script' => $seconds * 1000]
65            );
66
67            return $this;
68        }
69
70        $this->executor->execute(
71            DriverCommand::SET_SCRIPT_TIMEOUT,
72            ['ms' => $seconds * 1000]
73        );
74
75        return $this;
76    }
77
78    /**
79     * Set the amount of time to wait for a page load to complete before throwing an error.
80     *
81     * @param int $seconds Wait time in second.
82     * @return WebDriverTimeouts The current instance.
83     */
84    public function pageLoadTimeout($seconds)
85    {
86        if ($this->isW3cCompliant) {
87            $this->executor->execute(
88                DriverCommand::SET_SCRIPT_TIMEOUT,
89                ['pageLoad' => $seconds * 1000]
90            );
91
92            return $this;
93        }
94
95        $this->executor->execute(DriverCommand::SET_TIMEOUT, [
96            'type' => 'page load',
97            'ms' => $seconds * 1000,
98        ]);
99
100        return $this;
101    }
102}
103