链接webdriver方法

时间:2019-03-14 22:51:49

标签: php webdriver selenium-chromedriver

我想实现一个类来执行一些常见的webdriver功能,但我不断得到:驱动程序服务器已死。

类似这样的东西:

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 :(得分:0)

要实现方法链模式,只需返回如下实例:

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