Selects a button by name or alt value for images.
public selectButton ( string $value ) : |
||
$value | string | The button text |
return | A new instance of Crawler with the filtered list of nodes |
protected function setUp() { $this->client = static::createClient(); $this->client->followRedirects(true); $this->crawler = $this->client->request('GET', self::LOGIN_URL); $this->loginForm = $this->crawler->selectButton(self::LOGIN_BUTTON)->form(); }
public function click($link, $context = null) { if ($context) { $this->crawler = $this->match($context); } if (is_array($link)) { $this->clickByLocator($link); return; } $anchor = $this->strictMatch(['link' => $link]); if (!count($anchor)) { $anchor = $this->crawler->selectLink($link); } if (count($anchor)) { $this->crawler = $this->client->click($anchor->first()->link()); $this->forms = []; $this->debugResponse(); return; } $buttonText = str_replace('"', "'", $link); $button = $this->crawler->selectButton($buttonText); if (count($button)) { $this->submitFormWithButton($button); $this->debugResponse(); return; } $this->clickByLocator($link); }
/** * Use this function to connect to user in the application using login form * MORE SLOW, Use connectUser function above instead * * @param $username * @param $password * @return Client */ protected function loginUsingFormUser($username, $password) { $this->client = static::createClient(); $this->crawler = $this->client->request('GET', '/login'); $credentials = array('_username' => $username, '_password' => $password); $form = $this->crawler->selectButton('_submit')->form($credentials); $this->client->submit($form); $this->client->followRedirects(); return $this->client; }
public function click($link, $context = null) { if ($context) { $this->crawler = $this->match($context); } if (is_array($link)) { $this->clickByLocator($link); return; } $anchor = $this->strictMatch(['link' => $link]); if (!count($anchor)) { $anchor = $this->getCrawler()->selectLink($link); } if (count($anchor)) { $this->crawler = $this->clientClick($anchor->first()->link()); $this->forms = []; return; } $buttonText = str_replace('"', "'", $link); $button = $this->crawler->selectButton($buttonText); if (count($button)) { $buttonValue = []; if (strval($button->attr('name')) !== '' && $button->attr('value') !== null) { $buttonValue = [$button->attr('name') => $button->attr('value')]; } $this->proceedSubmitForm($button->parents()->filter('form')->first(), $buttonValue); return; } try { $this->clickByLocator($link); } catch (MalformedLocatorException $e) { throw new ElementNotFound("name={$link}", "'{$link}' is invalid CSS and XPath selector and Link or Button"); } }
public function click($link, $context = null) { if ($context) { $this->crawler = $this->match($context); } if (is_array($link)) { $this->clickByLocator($link); return; } $anchor = $this->strictMatch(['link' => $link]); if (!count($anchor)) { $anchor = $this->crawler->selectLink($link); } if (count($anchor)) { $this->crawler = $this->client->click($anchor->first()->link()); $this->forms = []; $this->debugResponse(); return; } $buttonText = str_replace('"', "'", $link); $button = $this->crawler->selectButton($buttonText); if (count($button)) { $this->proceedSubmitForm($button->parents()->filter('form')->first(), [$button->attr('name') => $button->attr('value')]); return; } $this->clickByLocator($link); }
public function click($link) { $link = $this->escape($link); $anchor = $this->crawler->selectLink($link); if (count($anchor)) { $this->crawler = $this->client->click($anchor->first()->link()); $this->debugResponse(); return; } $button = $this->crawler->selectButton($link); if (count($button)) { $this->submitFormWithButton($button); $this->debugResponse(); return; } \PHPUnit_Framework_Assert::fail("Link or button for '{$link}' was not found"); }
private function clickCreateGameButton(Crawler &$crawler, $gameName) { $form = $crawler->selectButton(self::CREATE_GAME_BUTTON_NAME)->form(); $form['create_game_form[name]'] = $gameName; $crawler = $this->getClient()->submit($form); $this->assertStatusCode(StatusCode::MOVED_TEMPORARILY); $crawler = $this->getClient()->followRedirect(); $this->assertStatusCode(StatusCode::OK); }
/** * @param Crawler $crawler * @param string $label */ protected function assertShoppingListSave(Crawler $crawler, $label) { $form = $crawler->selectButton('Save and Close')->form(['orob2b_shopping_list_type[label]' => $label]); $this->client->followRedirects(true); $crawler = $this->client->submit($form); $result = $this->client->getResponse(); $this->assertHtmlResponseStatusCodeEquals($result, 200); $html = $crawler->html(); $this->assertContains('Shopping List has been saved', $html); }
/** * @param Crawler $crawler * @param string $moveValue * @return Crawler */ private function clickPlayButton(Crawler &$crawler, $turnNumber, $moveValue) { $button = $crawler->selectButton(self::PLAY_BUTTON_NAME); $this->assertEquals(1, count($button), 'play button is shown'); $form = $button->form(['player_form[turnNumber]' => $turnNumber, 'player_form[move]' => $moveValue]); $crawler = $this->getClient()->submit($form); $this->assertStatusCode(StatusCode::MOVED_TEMPORARILY); $this->getClient()->followRedirect(); $this->assertStatusCode(StatusCode::OK); return $crawler; }
/** * Get the form from the page with the given submit button text. * * @param string|null $buttonText * @return \Symfony\Component\DomCrawler\Form */ protected function getForm($buttonText = null) { try { if ($buttonText) { return $this->crawler->selectButton($buttonText)->form(); } return $this->crawler->filter('form')->form(); } catch (InvalidArgumentException $e) { throw new InvalidArgumentException("Could not find a form that has submit button [{$buttonText}]."); } }
/** * @param Crawler $crawler * @param User $owner */ protected function assertOrderSave(Crawler $crawler, User $owner) { $form = $crawler->selectButton('Save and Close')->form(['orob2b_order_type[owner]' => $owner->getId()]); $this->client->followRedirects(true); $crawler = $this->client->submit($form); $result = $this->client->getResponse(); $this->assertHtmlResponseStatusCodeEquals($result, 200); $html = $crawler->html(); $this->assertContains('Order has been saved', $html); $this->assertViewPage($crawler, $owner); }
/** * @param Crawler $crawler * @param null|int $id */ protected function assertSubtotals(Crawler $crawler, $id = null) { $form = $crawler->selectButton('Save and Close')->form(); $form->getFormNode()->setAttribute('action', $this->getUrl('orob2b_order_subtotals', ['id' => $id])); $this->client->submit($form); $result = $this->client->getResponse(); $this->assertJsonResponseStatusCodeEquals($result, 200); $data = json_decode($result->getContent(), true); $this->assertArrayHasKey('subtotals', $data); $this->assertArrayHasKey('subtotal', $data['subtotals']); }
public function click($link, $context = null) { $literal = Crawler::xpathLiteral($link); if ($context) { $this->crawler = $this->match($context); } $anchor = $this->crawler->filterXPath('.//a[.=' . $literal . ']'); if (!count($anchor)) { $anchor = $this->crawler->selectLink($link); } if (count($anchor)) { $this->crawler = $this->client->click($anchor->first()->link()); $this->debugResponse(); return; } $button = $this->crawler->selectButton($link); if (count($button)) { $this->submitFormWithButton($button); $this->debugResponse(); return; } $nodes = $this->match($link); if (!$nodes->count()) { throw new ElementNotFound($link, 'Link or Button by name or CSS or XPath'); } foreach ($nodes as $node) { $tag = $node->nodeName; $type = $node->getAttribute('type'); if ($tag == 'a') { $this->crawler = $this->client->click($nodes->first()->link()); $this->debugResponse(); return; } elseif ($tag == 'input' && in_array($type, array('submit', 'image')) || $tag == 'button' && $type == 'submit') { $this->submitFormWithButton($nodes->first()); $this->debugResponse(); return; } } }
/** * Does login with ID and access key. **/ public function doResponsavelLogin() { // get csrf token $this->crawler = $this->client->request('GET', $this->responsavel_endpoint); $token = $this->crawler->filter('input[name="csrfmiddlewaretoken"]'); $token = $token->attr('value'); // get form and submit $form = $this->crawler->selectButton('Acessar')->form(); $this->crawler = $this->client->submit($form, ['matricula' => $this->username, 'chave' => $this->password, 'csrfmiddlewaretoken' => $token]); // set matricula $info = $this->crawler->filter('table[class="info"]'); $this->matricula = trim($info->filter('td')->eq(5)->text()); }
public function click($link, $context = null) { $literal = Crawler::xpathLiteral($link); if ($context) { $this->crawler = $this->match($context); } $anchor = $this->crawler->filterXPath('.//a[.=' . $literal . ']'); if (!count($anchor)) { $anchor = $this->crawler->selectLink($link); } if (count($anchor)) { $this->crawler = $this->client->click($anchor->first()->link()); $this->debugResponse(); return; } $button = $this->crawler->selectButton($link); if (count($button)) { $this->submitFormWithButton($button); $this->debugResponse(); return; } $nodes = $this->match($link); if ($nodes->count()) { foreach ($nodes as $node) { if ($node->nodeName == 'a') { $this->crawler = $this->client->click($nodes->first()->link()); $this->debugResponse(); return; } elseif ($node->nodeName == 'input' && $node->getAttribute('type') == 'submit') { $this->submitFormWithButton($nodes->first()); $this->debugResponse(); return; } } } \PHPUnit_Framework_Assert::fail("Link or button for '{$link}' was not found"); }
/** * @param Crawler $crawler * * @depends testImportFormAction */ public function testImportValidateAction(Crawler $crawler) { $this->assertTrue(file_exists($this->file)); /** @var Form $form */ $form = $crawler->selectButton('Submit')->form(); /** TODO Change after BAP-1813 */ $form->getFormNode()->setAttribute('action', $form->getFormNode()->getAttribute('action') . '&_widgetContainer=dialog'); $form['oro_importexport_import[file]']->upload($this->file); $this->client->followRedirects(true); $this->client->submit($form); $result = $this->client->getResponse(); $this->assertHtmlResponseStatusCodeEquals($result, 200); $crawler = $this->client->getCrawler(); $this->assertEquals(0, $crawler->filter('.import-errors')->count()); }
/** * @param Crawler $crawler * @param string $topContent * @param string $bottomContent * @param string $css */ protected function assertLoginPageSave(Crawler $crawler, $topContent, $bottomContent, $css) { $form = $crawler->selectButton('Save and Close')->form(); $form['orob2b_cms_login_page[topContent]'] = $topContent; $form['orob2b_cms_login_page[bottomContent]'] = $bottomContent; $form['orob2b_cms_login_page[css]'] = $css; $this->client->followRedirects(true); $crawler = $this->client->submit($form); $result = $this->client->getResponse(); $this->assertHtmlResponseStatusCodeEquals($result, 200); $html = $crawler->html(); $this->assertContains('Login form has been saved', $html); $this->assertContains($topContent, $html); $this->assertContains($bottomContent, $html); $this->assertContains($css, $html); }
protected function execute(InputInterface $input, OutputInterface $output) { /** @var FormatterHelper $formatter */ $formatter = $this->getHelper('formatter'); $html = file_get_contents('http://spb.hh.ru/search/vacancy?text=php&clusters=true&enable_snippets=true'); $crawler = new Crawler($html, 'http://spb.hh.ru/search/vacancy?text=php&clusters=true&enable_snippets=true'); $output->writeln($formatter->formatBlock('Vacancies', 'question', true)); $crawler->filterXPath(CssSelector::toXPath('body a.search-result-item__name'))->each(function (Crawler $node) use($output) { $output->writeln(sprintf('<comment>%s</comment>', $node->text())); }); $data = $crawler->filter('body a.search-result-item__name')->extract(['_text', 'class']); print_r($data[0]); $output->writeln($formatter->formatBlock('Link test', 'question', true)); $link = $crawler->selectLink('PHP developer')->link(); $output->writeln($link->getUri()); $crawler = new Crawler(file_get_contents('http://spb.hh.ru/login'), 'http://spb.hh.ru/login'); $output->writeln($formatter->formatBlock('Form test', 'question', true)); $form = $crawler->selectButton('Войти')->form(['username' => 'name', 'password' => 'pass']); $output->writeln($form->getUri()); $form['remember']->tick(); $output->writeln(print_r($form->getPhpValues())); }
/** * @param Crawler $crawler * @param string $name * @param Account[] $appendAccounts * @param Account[] $removeAccounts */ protected function assertAccountGroupSave(Crawler $crawler, $name, array $appendAccounts = [], array $removeAccounts = []) { $appendAccountIds = array_map(function (Account $account) { return $account->getId(); }, $appendAccounts); $removeAccountIds = array_map(function (Account $account) { return $account->getId(); }, $removeAccounts); $form = $crawler->selectButton('Save and Close')->form(['orob2b_account_group_type[name]' => $name, 'orob2b_account_group_type[appendAccounts]' => implode(',', $appendAccountIds), 'orob2b_account_group_type[removeAccounts]' => implode(',', $removeAccountIds)]); $this->client->followRedirects(true); $crawler = $this->client->submit($form); $result = $this->client->getResponse(); $this->assertHtmlResponseStatusCodeEquals($result, 200); $html = $crawler->html(); $this->assertContains('Account group has been saved', $html); $this->assertViewPage($html, $name); foreach ($appendAccounts as $account) { $this->assertContains($account->getName(), $html); } foreach ($removeAccounts as $account) { $this->assertNotContains($account->getName(), $html); } }
public function testSelectLinkAndLinkFiltered() { $html = <<<HTML <!DOCTYPE html> <html lang="en"> <body> <div id="action"> <a href="/index.php?r=site/login">Login</a> </div> <form id="login-form" action="/index.php?r=site/login" method="post"> <button type="submit">Submit</button> </form> </body> </html> HTML; $crawler = new Crawler($html); $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'login-form']"); $this->assertCount(0, $filtered->selectLink('Login')); $this->assertCount(1, $filtered->selectButton('Submit')); $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'action']"); $this->assertCount(1, $filtered->selectLink('Login')); $this->assertCount(0, $filtered->selectButton('Submit')); $this->assertCount(1, $crawler->selectLink('Login')->selectLink('Login')); $this->assertCount(1, $crawler->selectButton('Submit')->selectButton('Submit')); }
private function clickDealButton(Crawler &$crawler) { $button = $crawler->selectButton(self::DEAL_BUTTON_NAME); $this->assertEquals(1, count($button), 'deal button is on page'); $form = $button->form(); $crawler = $this->getClient()->submit($form); $this->assertStatusCode(StatusCode::MOVED_TEMPORARILY); $crawler = $this->getClient()->followRedirect(); $this->assertStatusCode(StatusCode::OK); }
/** * @param Crawler $crawler * @param int|null $shoppingListId * @param float $quantity * @param string $code * @param string|null $label * * @return Form */ protected function saveForm(Crawler $crawler, $shoppingListId, $quantity, $code, $label) { return $crawler->selectButton('Save')->form(['orob2b_shopping_list_frontend_line_item_widget[shoppingList]' => $shoppingListId, 'orob2b_shopping_list_frontend_line_item_widget[quantity]' => $quantity, 'orob2b_shopping_list_frontend_line_item_widget[unit]' => $code, 'orob2b_shopping_list_frontend_line_item_widget[shoppingListLabel]' => $label]); }
/** * @param Crawler $crawler * @param string $name * @param Account $parent * @param AccountGroup $group * @param AbstractEnumValue $internalRating */ protected function assertAccountSave(Crawler $crawler, $name, Account $parent, AccountGroup $group, AbstractEnumValue $internalRating) { $form = $crawler->selectButton('Save and Close')->form(['orob2b_account_type[name]' => $name, 'orob2b_account_type[parent]' => $parent->getId(), 'orob2b_account_type[group]' => $group->getId(), 'orob2b_account_type[internal_rating]' => $internalRating->getId()]); $this->client->followRedirects(true); $crawler = $this->client->submit($form); $result = $this->client->getResponse(); $this->assertHtmlResponseStatusCodeEquals($result, 200); $html = $crawler->html(); $this->assertContains('Account has been saved', $html); $this->assertViewPage($html, $name, $parent, $group, $internalRating); }
/** * @param Crawler $crawler * * @return \Symfony\Component\DomCrawler\Form */ private function getForm(Crawler $crawler) { $form = $crawler->selectButton('この内容で作成する')->form(); $form['admin_order_pdf[_token]'] = 'dummy'; return $form; }
/** * @param \Symfony\Component\DomCrawler\Crawler $crawler * @param \Symfony\Bundle\FrameworkBundle\Client $client * * @return mixed */ private function runTroughStep5($crawler, $client) { $form = $crawler->selectButton('Finish installation')->form(); $client->submit($form, array('install_login[email]' => '*****@*****.**', 'install_login[password][first]' => 'password', 'install_login[password][second]' => 'password')); $crawler = $client->followRedirect(); // we should be redirected to step 6 self::assertEquals(200, $client->getResponse()->getStatusCode()); self::assertStringEndsWith('/install/6', $client->getHistory()->current()->getUri()); self::assertGreaterThan(0, $crawler->filter('h2:contains("Installation complete")')->count()); return $crawler; }
private function getForm(Crawler $crawler, $couponCd = '') { $form = $crawler->selectButton('登録する')->form(); $form['shopping_coupon[coupon_cd]'] = $couponCd; $form['shopping_coupon[_token]'] = 'dummy'; return $form; }
/** * @param Crawler $crawler */ protected function assertKnownEmail(Crawler $crawler) { $form = $crawler->selectButton('Request')->form(); $submittedData = ['orob2b_account_account_user_password_request' => ['_token' => $form->get('orob2b_account_account_user_password_request[_token]')->getValue(), 'email' => self::EMAIL]]; $this->client->followRedirects(false); $this->client->submit($form, $submittedData); /** @var MessageDataCollector $collector */ $collector = $this->client->getProfile()->getCollector('swiftmailer'); $messages = $collector->getMessages(); /** @var \Swift_Message $message */ $message = reset($messages); $this->assertInstanceOf('Swift_Message', $message); $this->assertEquals(self::EMAIL, key($message->getTo())); $this->assertContains('Reset Account User Password', $message->getSubject()); $this->assertContains('To reset your password - please visit', $message->getBody()); $this->assertContains(self::EMAIL, $message->getBody()); $user = $this->getAccountUser(['email' => self::EMAIL]); $resetUrl = trim($this->configManager->get('oro_ui.application_url'), '/') . htmlspecialchars($this->getUrl('orob2b_account_frontend_account_user_password_reset', ['token' => $user->getConfirmationToken(), 'username' => $user->getUsername()])); $this->assertContains($resetUrl, $message->getBody()); $crawler = $this->client->followRedirect(); $this->assertEquals('Check Email', $crawler->filter('h2.title')->html()); }