/** * Set user agent */ public function testSetUserAgent() { $useragent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16'; // setup $capabilities = DesiredCapabilities::firefox(); /** @var FirefoxProfile $profile */ $profile = $capabilities->getCapability(FirefoxDriver::PROFILE); $profile->setPreference('general.useragent.override', $useragent); $this->webDriver = RemoteWebDriver::create('http://' . self::HOST . '/wd/hub', $capabilities); // test $this->webDriver->get('http://demo.mobiledetect.net/'); $elements = $this->webDriver->findElements(WebDriverBy::tagName('h1')); static::assertEquals(3, count($elements)); $elementContainingQuestion = $elements[1]; static::assertEquals('Is your device really a phone?', $elementContainingQuestion->getText()); $this->webDriver->quit(); }
/** * @param WebDriverBy $by * @return \Facebook\WebDriver\Remote\RemoteWebElement[] */ public function findElements(WebDriverBy $by) { $this->logFind($by->getMechanism(), $by->getValue()); $elements = parent::findElements($by); foreach ($elements as $element) { $this->logElement($element); } return $elements; }
/** * Execute debugging * * @param callback|null $callback * @param int $timeout Timeout between checks */ public function execute(callable $callback = null, $timeout = 500000) { $this->originWindow = $this->driver->getWindowHandle(); $this->showPopup(); do { $isDebugging = true; try { $this->processDebugActions($callback); } catch (WebDriver\Exception\WebDriverException $e) { $isDebugging = false; } if ((bool) $this->executeScriptFunction('isCheckRequested()')) { $this->executeScriptFunction('unmarkCheckRequest()'); $placeholders = $this->executeScriptFunction('getPlaceholders()'); $this->driver->switchTo()->window($this->originWindow); $this->revertHighlighting(); $missedLocators = array(); $isSingleChecker = $this->executeScriptFunction('isSingleChecker()'); foreach ($this->getDebuggingLocators($placeholders) as $key => $locator) { try { if (count($this->driver->findElements($locator))) { $elements = $isSingleChecker ? array($this->driver->findElement($locator)) : $this->driver->findElements($locator); foreach ($elements as $element) { $this->addHighlightingElement($key, $locator, $element); } } else { $missedLocators[] = $key; } } catch (WebDriver\Exception\WebDriverException $e) { $missedLocators[] = $key; } } $this->highlightElements(); $this->executeScriptFunction('highlightLocators(' . json_encode($missedLocators) . ')'); } usleep($timeout); $isDebugging = $isDebugging && ($this->isPopupFocused() || self::WINDOW_NAME == (string) $this->driver->executeScript('return window.' . self::WINDOW_VARIABLE . '.name')); } while ($isDebugging); }
/** * Grabs all visible text from the current page. * * @return string */ public function getVisibleText() { $els = $this->webDriver->findElements(WebDriverBy::cssSelector('body')); if (count($els)) { return $els[0]->getText(); } return ""; }
/** * Get a list of elements by a selector. * * @param $selector * * @return \Facebook\WebDriver\Remote\RemoteWebElement[] */ protected function els($selector) { return $this->driver->findElements(WebDriverBy::cssSelector($selector)); }