- Notifications
You must be signed in to change notification settings - Fork849
PHP client for Selenium/WebDriver protocol. Previously facebook/php-webdriver
License
php-webdriver/php-webdriver
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Php-webdriver library is PHP language binding for Selenium WebDriver, which allows you to control web browsers from PHP.
This library is compatible with Selenium server version 2.x, 3.x and 4.x.
The library supports modernW3C WebDriver protocol, as wellas legacyJsonWireProtocol.
The concepts of this library are very similar to the "official" Java, JavaScript, .NET, Python and Ruby librarieswhich are developed as part of theSelenium project.
Installation is possible usingComposer.
If you don't already use Composer, you can download thecomposer.phar binary:
curl -sS https://getcomposer.org/installer | phpThen install the library:
php composer.phar require php-webdriver/webdriverStarting from version 1.8.0, the project has been renamed fromfacebook/php-webdriver tophp-webdriver/webdriver.
In order to receive the new version and future updates,you need to rename it in your composer.json:
"require": {- "facebook/webdriver": "(version you use)",+ "php-webdriver/webdriver": "(version you use)",}and runcomposer update.
To control a browser, you need to start aremote end (server), which will listen to the commands sentfrom this library and will execute them in the respective browser.
This could be Selenium standalone server, but for local development, you can send them directly to so-called "browser driver" like Chromedriver or Geckodriver.
📙 Below you will find a simple example. Make sure to read our wiki formore information on Chrome/Chromedriver.
Install the latest Chrome andChromedriver.Make sure to have a compatible version of Chromedriver and Chrome!
Runchromedriver binary, you can passport argument, so that it listens on port 4444:
chromedriver --port=4444
📙 Below you will find a simple example. Make sure to read our wiki formore information on Firefox/Geckodriver.
Install the latest Firefox andGeckodriver.Make sure to have a compatible version of Geckodriver and Firefox!
Rungeckodriver binary (it start to listen on port 4444 by default):
geckodriver
Selenium server can be useful when you need to execute multiple tests at once,when you run tests in several different browsers (like on your CI server), or when you need to distribute tests amongstseveral machines in grid mode (where one Selenium server acts as a hub, and others connect to it as nodes).
Selenium server then act like a proxy and takes care of distributing commands to the respective nodes.
The latest version can be found on theSelenium download page.
📙 You can findfurther Selenium server informationin our wiki.
Selenium server could also be started inside Docker container - seedocker-selenium project.
When creating a browser session, be sure to pass the url of your running server.
For example:
// Chromedriver (if started using --port=4444 as above)$serverUrl ='http://localhost:4444';// Geckodriver$serverUrl ='http://localhost:4444';// selenium-server-standalone-#.jar (version 2.x or 3.x)$serverUrl ='http://localhost:4444/wd/hub';// selenium-server-standalone-#.jar (version 4.x)$serverUrl ='http://localhost:4444';
Now you can start browser of your choice:
useFacebook\WebDriver\Remote\RemoteWebDriver;// Chrome$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::chrome());// Firefox$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::firefox());// Microsoft Edge$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::microsoftEdge());
Desired capabilities define properties of the browser you are about to start.
They can be customized:
useFacebook\WebDriver\Firefox\FirefoxOptions;useFacebook\WebDriver\Remote\DesiredCapabilities;$desiredCapabilities = DesiredCapabilities::firefox();// Disable accepting SSL certificates$desiredCapabilities->setCapability('acceptSslCerts',false);// Add arguments via FirefoxOptions to start headless firefox$firefoxOptions =newFirefoxOptions();$firefoxOptions->addArguments(['-headless']);$desiredCapabilities->setCapability(FirefoxOptions::CAPABILITY,$firefoxOptions);$driver = RemoteWebDriver::create($serverUrl,$desiredCapabilities);
Capabilities can also be used to📙 configure a proxy server which the browser should use.
To configure browser-specific capabilities, you may use📙 ChromeOptionsor📙 FirefoxOptions.
- Seelegacy JsonWire protocol documentation orW3C WebDriver specification for more details.
// Go to URL$driver->get('https://en.wikipedia.org/wiki/Selenium_(software)');// Find search element by its id, write 'PHP' inside and submit$driver->findElement(WebDriverBy::id('searchInput'))// find search input element ->sendKeys('PHP')// fill the search box ->submit();// submit the whole form// Find element of 'History' item in menu by its css selector$historyButton =$driver->findElement( WebDriverBy::cssSelector('#ca-history a'));// Read text of the element and print it to outputecho'About to click to a button with text:' .$historyButton->getText();// Click the element to navigate to revision history page$historyButton->click();// Make sure to always call quit() at the end to terminate the browser session$driver->quit();
Seeexample.php for full example scenario.Visit our GitHub wiki for📙 php-webdriver command reference and further examples.
NOTE: Above snippets are not intended to be a working example by simply copy-pasting. Seeexample.php for a working example.
For latest changes seeCHANGELOG.md file.
Some basic usage example is provided inexample.php file.
How-tos are provided right here in📙 our GitHub wiki.
If you don't use IDE, you may useAPI documentation of php-webdriver.
You may also want to check out the Selenium projectdocs andwiki.
To take advantage of automatized testing you may want to integrate php-webdriver to your testing framework.There are some projects already providing this:
- Symfony Panther uses php-webdriver and integrates with PHPUnit using
PantherTestCase - Laravel Dusk is another project using php-webdriver, could be used for testing via
DuskTestCase - Steward integrates php-webdriver directly toPHPUnit, and provides parallelization
- Codeception testing framework provides BDD-layer on top of php-webdriver in itsWebDriver module
- You can also check out thisblogpost +demo project, describing simplePHPUnit integration
We have a great community willing to help you!
❓ Do you have aquestion, idea or some general feedback? Visit ourDiscussions page.(Alternatively, you canlook for many answered questions also on StackOverflow).
🐛 Something isn't working, and you want toreport a bug?Submit it here as a new issue.
📙 Looking for ahow-to orreference documentation? Seeour wiki.
We love to have your help to make php-webdriver better. SeeCONTRIBUTING.md for more information about contributing and developing php-webdriver.
Php-webdriver is community project - if you want to join the effort with maintaining and developing this library, the best is to look onissues marked with "help wanted"label. Let us know in the issue comments if you want to contribute and if you want any guidance, and we will be delighted to help you to prepare your pull request.
About
PHP client for Selenium/WebDriver protocol. Previously facebook/php-webdriver
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.