/** * @param string $filename Snapshot filename. Should be unique per test * @param string $logDir Directory to write snapshots into * @param UriInterface|string|null $base Resolve all relative links with this base uri */ public function saveSessionSnapshot($filename, $logDir, $base = null) { if (is_dir($logDir) && is_writable($logDir)) { if ($this->currentSession instanceof CrawlerSession) { $this->currentSession->saveHtml("{$logDir}/{$filename}.html", $base); } if ($this->currentSession instanceof BrowserSession) { $this->currentSession->saveScreenshot("{$logDir}/{$filename}.png"); } } else { throw new InvalidArgumentException(sprintf('Make sure directory %s exists and is writable', $logDir)); } }
public function testSimple() { $index = new Crawler('index.html'); $session = new CrawlerSession($index); $a = $session->get('.subnav li a:visible(true):text("Subpage 2")'); $this->assertEquals('Subpage Title 2', $a->getAttribute('title')); $session->get('#form')->with(function ($form) use($index) { $this->assertEquals('form', $form->getAttribute('id')); $form->getField('Enter Email:value("*****@*****.**")')->with(function ($email) { $this->assertEquals('*****@*****.**', $email->getValue()); }); $form->setField('Enter Name', 'Other thing'); $name = $form->getField('Enter Name'); $this->assertMatchesSelector('input[value="Other thing"]', $index->getElement($name->getId()), 'Should set a text input value'); $form->setFieldFile('Logo', '/var/usr/example.jpg'); $file = $form->getField('file'); $this->assertMatchesSelector('input[value="/var/usr/example.jpg"]', $index->getElement($file->getId()), 'Should set a file input value'); $label = $form->getLabel('Gender Male'); $this->assertMatchesSelector('label[for="gender-1"]', $index->getElement($label->getId()), 'Should find the correct label by text content'); }); }
/** * @return CrawlerSession */ public function getCrawlerSession($uri = '') { $session = new CrawlerSession(static::getDriver()); $session->open(self::getServerUri() . $uri); return $session; }
/** * @param BrowserInterface $browser */ public function __construct(BrowserInterface $browser) { $this->browser = $browser; parent::__construct($browser); }
public function testRedirectLoop() { $this->setExpectedException('LogicException', 'Maximum Number of redirects (5) for url /redirect-loop'); $session = new CrawlerSession(new Crawler()); $session->open('/redirect-loop'); }
/** * @covers ::select */ public function testSelect() { $ids = ['1', '2']; $ids2 = ['3', '4']; $selector = 'field selector'; $filters = new Filters(); $session = new CrawlerSession($this->crawler); $this->crawler->expects($this->at(0))->method('queryIds')->with($this->logicalAnd($this->isInstanceOf('SP\\Spiderling\\Query\\Field'), $this->attributeEqualTo('selector', $selector), $this->attribute($this->identicalTo($filters), 'filters')))->willReturn($ids); $this->crawler->expects($this->at(1))->method('queryIds')->with($this->logicalAnd($this->isInstanceOf('SP\\Spiderling\\Query\\Css'), $this->attributeEqualTo('selector', 'option'), $this->attributeEqualTo('filters', new Filters(['text' => 'option text']))), $ids[0])->willReturn($ids2); $this->crawler->expects($this->at(2))->method('select')->with($ids2[0]); $session->select($selector, 'option text', $filters); }
/** * @covers ::__construct * @covers ::getCrawler */ public function testConstruct() { $session = new CrawlerSession($this->crawler); $this->assertSame($this->crawler, $session->getCrawler()); }