Skip to content

Instantly share code, notes, and snippets.

@steffenr
Last active September 13, 2018 12:24
Show Gist options
  • Save steffenr/c3e79cc0d4dec8defe5cee7406274087 to your computer and use it in GitHub Desktop.
Save steffenr/c3e79cc0d4dec8defe5cee7406274087 to your computer and use it in GitHub Desktop.

Revisions

  1. steffenr renamed this gist Sep 13, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. steffenr created this gist Sep 12, 2018.
    35 changes: 35 additions & 0 deletions x.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <?php
    /**
    * @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/
    */
    public function iFillInSelectInputWithAndSelect($field, $value, $entry)
    {
    $page = $this->getSession()->getPage();

    $inputField = $page->find('css', $field);
    if (!$inputField) {
    throw new \Exception('No field found');
    }

    $choice = $inputField->getParent()->find('css', '.select2-selection');
    if (!$choice) {
    throw new \Exception('No select2 choice found');
    }
    $choice->press();

    $select2Input = $page->find('css', '.select2-search__field');
    if (!$select2Input) {
    throw new \Exception('No input found');
    }
    $select2Input->setValue($value);

    $this->getSession()->wait(1000);

    $chosenResults = $page->findAll('css', '.select2-results li');
    foreach ($chosenResults as $result) {
    if ($result->getText() == $entry) {
    $result->click();
    break;
    }
    }
    }