/** * Find an open port in a given range, trying several times. * Return FALSE if no open port is found after a timeout (1 second by default) * * @param string $host * @param integer $range_start * @param integer $range_end * @param integer $timeout * @return integer|boolean */ public static function ephimeral_port($host, $range_start = 1000, $range_end = 5000, $timeout = 1000) { return Attempt::make(function () use($host, $range_start, $range_end) { $port = rand($range_start, $range_end); return Network::is_port_open($host, $port) ? $port : FALSE; }, $timeout); }
/** * Perform a custom request on the phantomjs server, using curl * @param string $command * @param array $options * @return mixed */ protected function call($command, array $options = array()) { $curl = curl_init(); $options[CURLOPT_URL] = $this->command_url($command); $options[CURLOPT_RETURNTRANSFER] = TRUE; $options[CURLOPT_FOLLOWLOCATION] = TRUE; curl_setopt_array($curl, $options); $raw = ''; Attempt::make(function () use($curl, &$raw) { $raw = trim(curl_exec($curl)); return curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200; }); $error = curl_error($curl); $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($error) { throw new Exception_Driver('Curl ":command" throws exception :error', array(':command' => $command, ':error' => $error)); } if ($code != 200) { throw new Exception_Driver('Unexpected response from the panthomjs for :command: :code', array(':command' => $command, ':code' => $code)); } $result = json_decode($raw, TRUE); return $result; }
/** * Oposite to the find method() * * @param string|array $selector * @param array $filters * @throws Functest_Exception_Found If element is found on the page * @return Node $this */ public function not_present($selector, array $filters = array()) { $locator = self::get_locator($selector, $filters); $self = $this; $not_found = Attempt::make(function () use($self, $locator) { return !$self->all($locator)->first(); }, $this->next_wait_time()); $this->_next_wait_time = NULL; if (!$not_found) { throw new Exception_Found($locator, $this->driver()); } return TRUE; }