-
Notifications
You must be signed in to change notification settings - Fork 844
/
example.php
74 lines (51 loc) · 2.27 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
// An example of using php-webdriver.
// Do not forget to run composer install before. You must also have Selenium server started and listening on port 4444.
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
require_once('vendor/autoload.php');
// This is where Selenium server 2/3 listens by default. For Selenium 4, Chromedriver or Geckodriver, use http://localhost:4444/
$host = 'http://localhost:4444/wd/hub';
$capabilities = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create($host, $capabilities);
// navigate to Selenium page on Wikipedia
$driver->get('https://en.wikipedia.org/wiki/Selenium_(software)');
// write 'PHP' in the search box
$driver->findElement(WebDriverBy::id('searchInput')) // find search input element
->sendKeys('PHP') // fill the search box
->submit(); // submit the whole form
// wait until 'PHP' is shown in the page heading element
$driver->wait()->until(
WebDriverExpectedCondition::elementTextContains(WebDriverBy::id('firstHeading'), 'PHP')
);
// print title of the current page to output
echo "The title is '" . $driver->getTitle() . "'\n";
// print URL of current page to output
echo "The current URL is '" . $driver->getCurrentURL() . "'\n";
// find element of 'History' item in menu
$historyButton = $driver->findElement(
WebDriverBy::cssSelector('#ca-history a')
);
// read text of the element and print it to output
echo "About to click to button with text: '" . $historyButton->getText() . "'\n";
// click the element to navigate to revision history page
$historyButton->click();
// wait until the target page is loaded
$driver->wait()->until(
WebDriverExpectedCondition::titleContains('Revision history')
);
// print the title of the current page
echo "The title is '" . $driver->getTitle() . "'\n";
// print the URI of the current page
echo "The current URI is '" . $driver->getCurrentURL() . "'\n";
// delete all cookies
$driver->manage()->deleteAllCookies();
// add new cookie
$cookie = new Cookie('cookie_set_by_selenium', 'cookie_value');
$driver->manage()->addCookie($cookie);
// dump current cookies to output
$cookies = $driver->manage()->getCookies();
print_r($cookies);
// terminate the session and close the browser
$driver->quit();