如何测试与Laravel点击的div的结果?(Phpunit))

时间:2022-10-18 17:52:47

I have the next div ->

我有下一个div ->

<div id="grid">
  <div id="grid-item" onClick="sendServer(1)">Data 1 </div>
  <div id="grid-item" onClick="sendServer(2)">Data 2 </div>
  <div id="grid-item" onClick="sendServer(3)">Data 3 </div>

</div>

sendServer() is an ajax that validate and send datas to the server and go to route '/irCurso{id}'.

sendServer()是一个ajax,它验证并将数据发送到服务器,并发送到路由'/irCurso{id}'。

I want to test the result when those div has clicked, I know that I can't use ->click() method be cause it just works with <a> tag. This is my code :

我想在这些div被单击时测试结果,我知道我不能使用->click()方法be,因为它只对标记有效。这是我的代码:

$this->visit('/cursos')
            ->click('id=grid-item')
            ->seePage('irCurso/1');

Thank you :)

谢谢你:)

2 个解决方案

#1


0  

As Artenes Nogueira mentioned, Laravel 5.3 and earlier version does not have browser test support.

正如Artenes Nogueira提到的,Laravel 5.3和早期版本没有浏览器测试支持。

In order to solve this problem, You need to install PHPUnit_selenium package using composer

为了解决这个问题,您需要使用composer安装PHPUnit_selenium包

composer require --dev phpunit/phpunit-selenium

作曲家要求- dev phpunit)/ phpunit-selenium

Create Selenium Test Case class inside tests folder

在测试文件夹中创建Selenium测试案例类。

<?php


class SeleniumTestCase extends PHPUnit_Extensions_Selenium2TestCase
{
    /**
     * The base URL to use while testing the application.
     *
     * @var string
     */
    protected function setUp()
    {
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://localhost:8000/');
    }

    protected function visit($path)
    {
        $this->url($path);
        return $this;
    }

    protected function see($text, $tag = 'body')
    {
        print_r(request()->session()->all());
        //method call by tag name;
        $this->assertContains($text,$this->byTag($tag)->text());
        return $this;
    }

    protected function pressByName($text){
        $this->byName($text)->click();
        return $this;
    }
    protected function pressByTag(){
        $this->byTag('button')->click();
        return $this;
    }
    protected function type($value, $name)
    {
        $this->byName($name)->value($value);
        return $this;
    }

    protected function hold($seconds){
        sleep($seconds);
        return $this;
    }
}

and Create new test case for visiting homepage URL

并为访问主页URL创建新的测试用例。

<?php    

class ExampleTest extends SeleniumTestCase
{
    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testTitle()
    {
        $this->visit('/')
            ->see('Site title','title');
    }
}

and Before starting any test you need to start selenium server

在开始任何测试之前,您需要启动selenium服务器。

java -jar /usr/local/bin/selenium-server-standalone-2.35.0.jar

Finally, Run command PHPUnit test from terminal

最后,从终端运行命令PHPUnit测试

Happy Testing!

测试快乐!

References:

引用:

Selenium and Laravel 5.2

硒和Laravel 5.2

#2


-1  

If you are using 5.3 or lower it is not possible to test the result of a div clicked by the Laravel test API.

如果您正在使用5.3或更低的版本,则不可能测试Laravel测试API单击的div的结果。

When you click in your div, you expect to run a JS function. Unfortunately Laravel test API will not interpratate your js, so it will not be executed. And since it is not executed, there are not results to be evaluated.

当您单击div时,您希望运行一个JS函数。不幸的是,Laravel测试API不会插入你的js,所以它不会被执行。由于它没有执行,所以没有结果需要评估。

But with Dusk (in laravel 5.4) you are able to test javascript through the Laravel test API. Check out the docs for more info about Dusk: https://laravel.com/docs/master/dusk

但是使用黄昏(laravel 5.4),您可以通过laravel测试API测试javascript。查看文档了解更多关于黄昏的信息:https://laravel.com/docs/master/黄昏。

#1


0  

As Artenes Nogueira mentioned, Laravel 5.3 and earlier version does not have browser test support.

正如Artenes Nogueira提到的,Laravel 5.3和早期版本没有浏览器测试支持。

In order to solve this problem, You need to install PHPUnit_selenium package using composer

为了解决这个问题,您需要使用composer安装PHPUnit_selenium包

composer require --dev phpunit/phpunit-selenium

作曲家要求- dev phpunit)/ phpunit-selenium

Create Selenium Test Case class inside tests folder

在测试文件夹中创建Selenium测试案例类。

<?php


class SeleniumTestCase extends PHPUnit_Extensions_Selenium2TestCase
{
    /**
     * The base URL to use while testing the application.
     *
     * @var string
     */
    protected function setUp()
    {
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://localhost:8000/');
    }

    protected function visit($path)
    {
        $this->url($path);
        return $this;
    }

    protected function see($text, $tag = 'body')
    {
        print_r(request()->session()->all());
        //method call by tag name;
        $this->assertContains($text,$this->byTag($tag)->text());
        return $this;
    }

    protected function pressByName($text){
        $this->byName($text)->click();
        return $this;
    }
    protected function pressByTag(){
        $this->byTag('button')->click();
        return $this;
    }
    protected function type($value, $name)
    {
        $this->byName($name)->value($value);
        return $this;
    }

    protected function hold($seconds){
        sleep($seconds);
        return $this;
    }
}

and Create new test case for visiting homepage URL

并为访问主页URL创建新的测试用例。

<?php    

class ExampleTest extends SeleniumTestCase
{
    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testTitle()
    {
        $this->visit('/')
            ->see('Site title','title');
    }
}

and Before starting any test you need to start selenium server

在开始任何测试之前,您需要启动selenium服务器。

java -jar /usr/local/bin/selenium-server-standalone-2.35.0.jar

Finally, Run command PHPUnit test from terminal

最后,从终端运行命令PHPUnit测试

Happy Testing!

测试快乐!

References:

引用:

Selenium and Laravel 5.2

硒和Laravel 5.2

#2


-1  

If you are using 5.3 or lower it is not possible to test the result of a div clicked by the Laravel test API.

如果您正在使用5.3或更低的版本,则不可能测试Laravel测试API单击的div的结果。

When you click in your div, you expect to run a JS function. Unfortunately Laravel test API will not interpratate your js, so it will not be executed. And since it is not executed, there are not results to be evaluated.

当您单击div时,您希望运行一个JS函数。不幸的是,Laravel测试API不会插入你的js,所以它不会被执行。由于它没有执行,所以没有结果需要评估。

But with Dusk (in laravel 5.4) you are able to test javascript through the Laravel test API. Check out the docs for more info about Dusk: https://laravel.com/docs/master/dusk

但是使用黄昏(laravel 5.4),您可以通过laravel测试API测试javascript。查看文档了解更多关于黄昏的信息:https://laravel.com/docs/master/黄昏。