Методы цепочки вебдрайвера - PullRequest
0 голосов
/ 15 марта 2019

Я хотел бы реализовать класс для выполнения некоторых общих функций веб-драйвера, но я продолжаю получать: сервер драйверов умер.

Примерно так:

class mywebdriver {

    function __construct( $args ){

        //options
        $options = new ChromeOptions();
        $options->addArguments($args);

        //capabilities
        $caps = DesiredCapabilities::chrome();
        $caps->setCapability(ChromeOptions::CAPABILITY, $options);
        $driver = ChromeDriver::start($caps);
        $driver->manage()->timeouts()->implicitlyWait = 20;

        $this->driver = $driver;    
    }

    function login(){       
        //all things related to logging into a site using $this->driver
        $this->driver->get( "www.url.com" );
    }

    function logout(){      
        //all things related to logging into a site using $this->driver
        $this->driver->quit();
    }

    function search( $value ){      
        //enter a value in the search field and print results
    }

    ... other methods
}

Затем, цепочка методов, таких как:

$drive = new mywebdriver();
try {
    $drive->login()
        ->search( "toyota" )
        ->search( "ford" )
        ->logout();
} catch (Exception $e) {
    echo "Caught Exception". $e->getMessage();
}

Это типично и возможно? Я попробовал несколько вещей, но сервер драйверов продолжает выдавать мне это «умершее» исключение.

1 Ответ

0 голосов
/ 15 марта 2019

Чтобы реализовать шаблон цепочки методов, просто верните экземпляр следующим образом:

public function search( $value ) : mywebdriver
{      
    //enter a value in the search field and print results
    return $this;
}
...