2017-03-24 6 views
0

Laravel TestCase로 정렬 작업이 올바르게 작동하는지 테스트하고 싶습니다. 나는 간단한 테스트 페이지가 있습니다페이지의 elemets의 Laravel 순서

<div id="values"> 
    @foreach(['Value1', 'Value2'] as $value) 
     <div class="test-class">{{$value}}</div> 
    @endforeach 
</div> 

을 그리고 지금은 처음 .test 수준의 요소가 '값 1'가 포함되어있는 경우 테스트하려는. 나는 다른 접근법을 시도했지만 성공하지 못했습니다. 여기에 첫 번째입니다

public function testSorting() 
{ 
    $this->visit(route('dummy')); 
    $this->see('Value1'); 
    $this->seeInElement('.test-class:first-of-type', 'Value2'); 
} 

이 사람은 예외 결과 :

Symfony\Component\CssSelector\Exception\ExpressionErrorException: "*:first-of-type" is not implemented. 

가 그럼 난
$this->seeInElement('#values:first-child', 'Value2'); 

내 테스트 녹색을 시도했다. 그러나 첫 번째 자식이 '값 1'이므로 빨간색이어야합니다. 그것은 완전히 ': first-child' 부분을 무시했습니다.

은 그 때 나는
$this->seeInElement('#values:nth-child(1)', 'Value2'); 

을 시도하고도 녹색 얻었다. 따라서이 방법은 효과가 없습니다.

내가 놓친 것이 있습니까? 아니면 Laravel 테스트를 통해 페이지의 요소 순서를 테스트 할 수있는 방법이 없습니까?

답변

0

그래서, 내가 그것을 해결했다. 핵심은 \ Symfony \ Component \ DomCrawler \ Crawler 클래스입니다. first(), last() 또는 eq ($ number)과 같은 유용한 메소드가 많이 있습니다.

당신은 트림 제거 할 수
public function seeInFirstElement($selector, $text) 
{ 
    $this->assertContains($text, trim($this->crawler->filter($selector)->first()->text())); 

    return $this; 
} 

() 기능을하지만 그것으로 출력은 실패한 테스트의 경우 더 나은 보이는 : 내 TestCase에 클래스에서이 방법을 만들었습니다. 내 테스트에서 나는 그런 구조를 사용하여 순서를 테스트합니다 :

$this->seeInFirstElement('.test-class', 'Value1'); 
1

하지 특정 질문에 대한 해결책, 그러나 당신의 문제에 대한 해결 방법 : 당신은 현재 루프 반복의 수를 포함 $loop->iteration 도우미 변수로 test-class 된 div를 열거 할 수있다.

<div id="values"> 
    @foreach(['Value1', 'Value2'] as $value) 
     <div class="test-class" id="value_{{$loop->iteration}}">{{$value}}</div> 
    @endforeach 
</div> 

는 이제 DIV 클래스는 열거 ID가 1로 시작해야하고 첫 번째 DIV를 선택 테스트의 선택 #value_1를 사용할 수 있습니다

$this->seeInElement('#value_1', 'Value2');