2014-11-26 20 views
9

Mi stavo chiedendo se potresti indicare dove si trova il problema, se è Behat, il selettore CSS o il Sahi Driver.Imposta CSS Selector (first-child) funziona in Behat 3 con Sahi Driver

Abbiamo appena aggiornato a Behat 3 e stiamo usando Sahi Driver (la più recente versione open source). Abbiamo scoperto che qualsiasi test Behat che utilizza lo pseudo-elemento first-child sembra ora interrotto.

Esempio:

Passo:

And I follow "#the-list tr:first-child .row-title" 

(che contiene un elemento di ancoraggio con la classe row-titolo su di essa, vedi HTML)

Errore:

Link with id|title|alt|text "#the-list tr:first-child .row-title" not found. (Behat\Mink\Exception\ElementNotFoundException)

HTML:

<tbody id="the-list"> 
    <tr id="post-162382" class="post-162382 type-post status-publish format-standard hentry category-uncategorized alternate iedit author-other level-0"> 
     <th class="check-column" scope="row"></th> 
     <td class="post-title page-title column-title"> 
      <strong> 
       <a class="row-title" title="Edit “Post Title”" href="https://domain.com"></a> 
      </strong> 
      <div class="locked-info"></div> 
      <div class="row-actions"></div> 
      <div id="inline_162382" class="hidden"></div> 
     </td> 

CSSSelector.php (Override abbiamo usato con il nostro vecchio Behat, abbiamo lasciato questo file in)

/** 
* Makes all of the form field related steps allow CSS selectors. 
*/ 
class CSSSelectorContext extends MinkContext 
{ 
    /** 
    * Finds an element via CSS or fails with a given error 
    * 
    * @param $element string A CSS selector string 
    * @param $error Exception An error to throw in case the element can not be found 
    * @return object   An Element object 
    */ 
    protected function findOrFail($element, $error){ 
     $element = $this->getSession()->getPage()->find('css', $element); 
     if (!isset($element)){  
      throw $error; 
     } 
     return $element; 
    } 

    public function clickLink($link) { 
     try { 
      parent::clickLink($link); 
      return; 
     }catch (Exception $error){ 
      $link = $this->fixStepArgument($link); 
      $link = $this->findOrFail($link, $error); 
      $link->press(); 
     } 
    } 

Quando si utilizza il selettore CSS nella console Chrome con jQuery seleziona l'elemento appropriato. Ho passato il codice e ho guardato le traduzioni css -> xpath e poi ho convalidato xpath con l'html prodotto sul sito che stiamo testando e sembra essere valido. Il selettore css funziona anche con il driver Goutte.

XPath Generated:

find(function(){ 
     var count = 0; 
     while (_sahi._byXPath("("+"\/\/html\/descendant-or-self::*[@id = 'the-list']\/descendant-or-self::*\/*[name() = 'tr' and (position() = 1)]\/descendant-or-self::*\/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' row-title ')]"+")["+(count+1)+"]")) count++; 
     return count; 
    })() 

descendant-or-self::*[@id = 'the-list']/descendant-or-self::*/*[name() = 'tr' and (position() = 1)]/descendant-or-self::*/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' row-title ')] 

//html/descendant-or-self::*[@id = 'the-list']/descendant-or-self::*/*[name() = 'tr' and (position() = 1)]/descendant-or-self::*/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' row-title ')] 

Quando cambio il CSS a:

Passo:

And I follow "#the-list tr .row-title" 

Funziona perché credo che solo raccoglie il primo tr da un elenco di loro comunque, ma vogliamo essere in grado di usare il primo figlio, naturalmente.

Grazie per il vostro aiuto!

+1

[Questo problema github] (https://github.com/Behat/Mink/issues/268) sembra parlare della stessa cosa. Aiuta affatto? –

+0

Il problema https://github.com/symfony/symfony/issues/11803 è pertinente, ma non ha senso perché l'Xpath generato da Symfony sembra corretto. – sugarwaffle

+0

Hai provato "# the-list tr: nth-child (1) .row-title"? –

risposta

0

Ci scusiamo per il ritardo alla festa

Il tuo problema è il fatto che proprio Visoni passo "che seguo"/"clickLink" non accetta le seguenti:

  1. s "#" per ID
  2. Qualsiasi cosa diversa da un ID, testo, titolo o valore alternativo.

Io suggerisco di usare un "click sul" gradino invece di un qualcosa di "seguire", in questo modo:

/** 
    * @Then /^(?:|I)click (?:|on)(?:|the)"([^"]*)"(?:|.*)$/ 
    */ 
    public 
    function iClickOn($arg1) 
    { 
     $findName = $this->getSession()->getPage()->find("css", $arg1); 
     if (!$findName) { 
      throw new Exception($arg1 . " could not be found"); 
     } else { 
      $findName->click(); 
     } 
    } 

Stai usando CSS proprio qui, che permetterà per questo da scrivere :

Then I click on the "#the-list tr:first-child .row-title" link

e 'un errore che ho fatto anche, e questa è la soluzione che abbiamo deciso di allora, e non abbiamo dovuto guardare indietro.