Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-andreyev committed Jul 12, 2023
1 parent 07feb70 commit 7c346ad
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ jobs:
defaults read /Applications/Safari.app/Contents/Info CFBundleShortVersionString
fi;
/usr/bin/safaridriver -p 4444 --diagnose &
if [[ "${{ matrix.use_safari_technology_preview }}" == true ]]; then
/Applications/Safari\ Technology\ Preview.app/Contents/MacOS/safaridriver -p 4444 --diagnose &
else
/usr/bin/safaridriver -p 4444 --diagnose &
fi;
while ! nc -z localhost 4444 </dev/null; do echo Waiting for driver to start...; sleep 1; done
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ https://github.com/oleg-andreyev/MinkPhpWebdriverExtension
You must enable the 'Allow Remote Automation' option in Safari's Develop menu to control Safari via WebDriver.

```bash
./bin/start_webdriver.sh &
# ./bin/start_driver.sh <browser> <version>
./bin/start_webserver.sh &
# ./bin/start_driver.sh <browser> [<version>]
# ./bin/start_driver.sh chrome
# ./bin/start_driver.sh firefox
# ./bin/start_driver.sh safari
./bin/start_driver.sh chrome latest &
BROWSER_NAME=chrome ./bin/phpunit
```
Expand Down
14 changes: 12 additions & 2 deletions bin/browser/safari.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ fi

rm -rf ~/Library/Logs/com.apple.WebDriver/*

/usr/bin/safaridriver --port=4444 --diagnose &
if [[ "$USE_SAFARI_TECHNOLOGY_PREVIEW" == true ]]; then
/Applications/Safari\ Technology\ Preview.app/Contents/MacOS/safaridriver -p 4444 --diagnose &
else
/usr/bin/safaridriver -p 4444 --diagnose &
fi;

SAFARIDRIVER_PID=$!

function stop {
kill $SAFARIDRIVER_PID
}

trap stop SIGINT
trap stop ERR

# generate first log
sleep 5
curl 127.0.0.1:4444 -vvv

tail -f ~/Library/Logs/com.apple.WebDriver/* /dev/null
tail -F ~/Library/Logs/com.apple.WebDriver/*
54 changes: 31 additions & 23 deletions src/WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Facebook\WebDriver\Exception\NoSuchCookieException;
use Facebook\WebDriver\Exception\NoSuchElementException;
use Facebook\WebDriver\Exception\ScriptTimeoutException;
use Facebook\WebDriver\Exception\TimeoutException;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\LocalFileDetector;
use Facebook\WebDriver\Remote\RemoteWebDriver;
Expand Down Expand Up @@ -72,32 +73,36 @@ class WebDriver extends CoreDriver
/**
* Instantiates the driver.
*
* @param string $browserName Browser name
* @param array<string, mixed>|null $desiredCapabilities The desired capabilities
* @param string $wdHost The WebDriver host
* @param string $browserName Browser name
* @param array<string, mixed>|DesiredCapabilities|null $desiredCapabilities The desired capabilities
* @param string $wdHost The WebDriver host
*/
public function __construct(
string $browserName = 'firefox',
$desiredCapabilities = null,
array|DesiredCapabilities $desiredCapabilities = null,
string $wdHost = 'http://localhost:4444/wd/hub'
) {
$this->wdHost = $wdHost;
$this->browserName = $browserName;

if ('firefox' === $browserName) {
$this->desiredCapabilities = DesiredCapabilities::firefox();
} elseif ('chrome' === $browserName) {
$this->desiredCapabilities = DesiredCapabilities::chrome();
} else if ($browserName === 'safari') {
$this->desiredCapabilities = DesiredCapabilities::safari();
} else {
$this->desiredCapabilities = new DesiredCapabilities();
}
if (!$desiredCapabilities instanceof DesiredCapabilities) {
if ('firefox' === $browserName) {
$this->desiredCapabilities = DesiredCapabilities::firefox();
} elseif ('chrome' === $browserName) {
$this->desiredCapabilities = DesiredCapabilities::chrome();
} else if ($browserName === 'safari') {
$this->desiredCapabilities = DesiredCapabilities::safari();
} else {
$this->desiredCapabilities = new DesiredCapabilities();
}

if ($desiredCapabilities) {
foreach ($desiredCapabilities as $key => $val) {
$this->desiredCapabilities->setCapability($key, $val);
if (is_array($desiredCapabilities)) {
foreach ($desiredCapabilities as $key => $val) {
$this->desiredCapabilities->setCapability($key, $val);
}
}
} else {
$this->desiredCapabilities = $desiredCapabilities;
}
}

Expand Down Expand Up @@ -338,7 +343,11 @@ public function stop()
*/
public function reset()
{
$this->webDriver->manage()->deleteAllCookies();
// if about:blank (safari just empty) we cannot delete cookies.
if ($this->webDriver->getCurrentURL() !== '') {
$this->webDriver->manage()->deleteAllCookies();
}

// TODO: resizeWindow does not accept NULL
$this->maximizeWindow();
// reset timeout
Expand Down Expand Up @@ -551,16 +560,15 @@ public function getTagName(
return $element->getTagName();
}

/**
* @see https://www.w3.org/TR/webdriver1/#get-element-text
*/
public function getText(
#[Language('xpath')]
$xpath
) {
): string {
$element = $this->findElement($xpath);
$text = $element->getText();

$text = (string) str_replace(["\r", "\r\n", "\n"], ' ', $text);

return $text;
return $element->getText();
}

/**
Expand Down
6 changes: 5 additions & 1 deletion tests/WebDriverConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function createDriver()
}
} else if ($browser === 'safari') {
$desiredCapabilities = DesiredCapabilities::safari();
if (($_SERVER['USE_SAFARI_TECHNOLOGY_PREVIEW'] ?? 'false') === 'true') {
$desiredCapabilities->setBrowserName('Safari Technology Preview');
}
} else {
$desiredCapabilities = new DesiredCapabilities();
}
Expand All @@ -63,7 +66,8 @@ public function createDriver()
$optionsOrProfile = $this->buildFirefoxProfile($desiredCapabilities, $optionsOrProfile, $driverOptions);
} else if ($browser === 'safari') {
$optionsOrProfile = [
'technologyPreview' => ($_ENV['USE_SAFARI_TECHNOLOGY_PREVIEW'] ?? 'false') === 'true',
// 'safari:automaticInspection' => true,
'technologyPreview' => ($_SERVER['USE_SAFARI_TECHNOLOGY_PREVIEW'] ?? 'false') === 'true',
];
}

Expand Down

0 comments on commit 7c346ad

Please sign in to comment.