Bohdan Nesteruk
Bohdan Nesteruk

Reputation: 924

Trying to send some text to the field

I trying to send some text to the field with this function:

public function iFillInGrizzlistSearchFieldNameOfNewMember2($arg1)
    {
        $page = $this->getSession()->getPage();
       $el = $page->find('css','.grizzlist-quicksearch');
       $el->setText('$arg1');
      }        

but it does not work. Please, tell me, what i do wrong?


And if i have some elements with same class, how i can click on second of them using

function:
     public function iDeleteActiveStatusFromSearchCriteria()
    {
       $page = $this->getSession()->getPage();
       $el = $page->find('css','.delete-bt');
       $el->click();
    }

Upvotes: 1

Views: 7400

Answers (3)

Mirela Lou
Mirela Lou

Reputation: 1

Input text to locator in behat

    public function inputValue($name)
{
  $searchField = $this->findField($locatorOfInput);
  $searchField = $searchField->setValue($name);
}

Js is always a choice :) Input text to locator in behat by using js

public function inputValue($name)
{       
   $this->getDriver()->executeScript("$('$searchFieldLocator').val('".$name ."')";
}

get the parent locator of same-class elements

 public function deleteSecondElement()
{
   $status= $this->findAll('css', $locator);
   $status= $status[1]->click();
}

Upvotes: 0

amitaibu
amitaibu

Reputation: 1116

Here's the step definition we are using:

/** * @Given /^I set tinymce "([^"]*)"$/ */ public function iSetTinymce($arg1) { $this->getSession()->executeScript("tinymce.get()[0].setContent('" . $arg1 . "');"); }

Note that the tinymce.get()[0] is getting the 1st TinyMce instance on the page.

Upvotes: 3

branchgabriel
branchgabriel

Reputation: 4251

By default the framework will go through a set of possible selectors. You don't have to explicitly write step defs unless you want to group them for doing multiple things in one step. Just do two steps instead

When I fill in "grizzlist-quicksearch" with "sometext"
And I press "delete-bt"

These step defs are built in so you don't have to do anything else. However this isn't going to work because the stuff I put into this step is css. The proper way is to match on something else.

When I fill in "valueOfInputNameAttribute" with "sometext"
And I press "valueOfSubmitButtonNameAttribute"

For example here is the code that the framework uses and is available to you to use in your features

https://github.com/Behat/MinkExtension/blob/master/src/Behat/MinkExtension/Context/MinkContext.php

look for anything that uses fillField

You will notice it is using fillField to do the work for you

If you dig deeper into the source you will find this in traversableelement.php in Mink/Element

/**
     * Fills in field (input, textarea, select) with specified locator.
     *
     * @param string $locator input id, name or label
     * @param string $value   value
     *
     * @throws ElementNotFoundException
     */
    public function fillField($locator, $value)
    {
        $field = $this->findField($locator);

        if (null === $field) {
            throw new ElementNotFoundException(
                $this->getSession(), 'form field', 'id|name|label|value', $locator
            );
        }

        $field->setValue($value);
    }

It is using a locator and it will try to find input elements using the text you provide in your feature by id|name|label|value

So you should consider using a unique id in your id attribute or if your name attributes on the input are unique use that instead.. etc.. etc...

To get your focus on things using css would require a new step def which means writing code you probably didn't need to write but sometimes you have to.

If you truly are only limited to css to find your page elements then use a custom step def like this in your FeatureContext

 /**
 * @When /^I fill in the quicksearch with "([^"]*)"$/
 */
public function iFillInTheQuickSearchWith($value)
{
   $element = $session->getPage()->find('css', 'INPUT#grizzlist-quicksearch');
   $element->setValue($value);
}

Hope that helps get you started. Understanding BDD frameworks can be tough when you are starting out.

Upvotes: 1

Related Questions