Chaining webdriver methods

25
March 16, 2019, at 04:30 AM

I'd like to implement a class to carry out some common webdriver functions, but I keep getting: the driver server has died.

Something like this:

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
}

Then, chaining the methods, such as:

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

Is this typical, and possible? I've tried a few things, but the driver server keeps giving me that "died" exception.

Answer 1

To implement a method chain pattern, just return the instance like that:

public function search( $value ) : mywebdriver
{      
    //enter a value in the search field and print results
    return $this;
}
READ ALSO
Access Denied error while using PHP to connect to database in CentOS 7

Access Denied error while using PHP to connect to database in CentOS 7

I am trying to connect to remote MySQL database at address 192168

46
How to fix file not found on Heroku?

How to fix file not found on Heroku?

I have a laravel project deployed in Heroku, in my Laravel project i have a php script i made to receive json, my php script is in public folder, when i use php artisan serve to serve the project, the script is accessible with no problem through the following...

32
How to set a shipping cost by item in Woocommerce

How to set a shipping cost by item in Woocommerce

In Woocommerce, when I add multiple products in cart, the shipping only applies to to 1 productHow should I change that to apply the cost to each item?

12