goez / mink-page-objects

此包已被弃用且不再维护。没有推荐替代包。

Mink 的页面对象模式实现

0.3.3 2015-11-17 10:33 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:52:29 UTC


README

Build Status

alpha版本

安装

$ composer require goez/mink-page-objects --dev

使用

为Google首页创建页面类

use Goez\PageObjects\Page;

class Home extends Page
{
    protected $elements = [
        'SearchForm' => ['css' => 'form'],
    ];

    public function search($keyword)
    {
        return $this->getPart(SearchForm::class)
            ->search($keyword);
    }
}

为搜索结果创建页面类

use Goez\PageObjects\Page;

class SearchResult extends Page
{
}

为搜索表单创建元素对象

use Goez\PageObjects\Part;

class SearchForm extends Part
{
    /**
     * @param $keyword
     * @return SearchResult
     * @throws \Behat\Mink\Exception\ElementNotFoundException
     */
    public function search($keyword)
    {
        $this->element->fillField('q', $keyword);
        $this->element->submit();

        return $this->createPage(SearchResult::class);
    }
}

实例化页面对象并验证关键字搜索

use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Session;
use Goez\PageObjects\Context;

class GoogleSearchTest extends PHPUnit_Framework_TestCase
{
    // Install phantomjs first
    // and you can use this trait
    // for starting phantonjs automatically
    use PhantomJSRunner;

    public function testSearchWithKeyword()
    {
        $driver = new Selenium2Driver('phantomjs');

        $session = new Session($driver);
        $session->start();

        $context = new Context($session, [
            'baseUrl' => 'https://www.google.com',
        ]);

        $context->createPage(Home::class)
            ->open()
            ->search('example')
            ->shouldContainText('Example Domain');
    }
}

许可证

MIT