Skip to content

Commit c1e0de0

Browse files
authored
Merge pull request php-webdriver#383 from OndraM/feature/functional-tests
Rewrite functional tests and run them also in Firefox on Travis
2 parents cceea0c + 6d385d7 commit c1e0de0

18 files changed

+534
-172
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ composer.phar
22
composer.lock
33
vendor
44
.php_cs.cache
5+
phpunit.xml
56

67
# generic files to ignore
78
*.lock

.travis.yml

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ php:
55
- 5.6
66
- 7
77
- 7.1
8-
- hhvm
98

109
matrix:
1110
include:
11+
# Add build to run tests against Firefox (other runs are agains HtmlUnit by default)
12+
- php: 7
13+
env: BROWSER_NAME="firefox"
1214
# Add PHP 7 build to check codestyle only in PHP 7 build
1315
- php: 7
1416
env: CHECK_CODESTYLE=1
17+
before_script: ~
18+
script:
19+
- ./vendor/bin/php-cs-fixer fix --diff --dry-run
20+
- ./vendor/bin/phpcs --standard=PSR2 ./lib/ ./tests/
1521

1622
env:
1723
global:
@@ -20,23 +26,35 @@ env:
2026
cache:
2127
directories:
2228
- $HOME/.composer/cache
29+
- jar
2330

2431
before_install:
2532
- travis_retry composer self-update
2633

2734
install:
28-
- travis_retry composer install --no-interaction --prefer-source
35+
- travis_retry composer install --no-interaction
2936

3037
before_script:
31-
- if [ -z "$CHECK_CODESTYLE" ]; then sh -e /etc/init.d/xvfb start; fi
32-
- if [ -z "$CHECK_CODESTYLE" ]; then wget -q -t 3 http://selenium-release.storage.googleapis.com/2.45/selenium-server-standalone-2.45.0.jar; fi
33-
- if [ -z "$CHECK_CODESTYLE" ]; then java -jar selenium-server-standalone-2.45.0.jar -log selenium.log; fi &
34-
- if [ -z "$CHECK_CODESTYLE" ]; then until $(echo | nc localhost 4444); do sleep 1; echo waiting for selenium-server...; done; fi
38+
- sh -e /etc/init.d/xvfb start
39+
# TODO: upgrade to Selenium 3.0.2 (with latest HtmlUnit) once released, as HtmlUnit in 3.0.1 is broken
40+
- if [ ! -f jar/selenium-server-standalone-2.53.1.jar ]; then wget -q -t 3 -P jar https://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.1.jar; fi
41+
- if [ ! -f jar/htmlunit-driver-standalone-2.20.jar ]; then wget -q -t 3 -P jar https://github.com/SeleniumHQ/htmlunit-driver/releases/download/2.20/htmlunit-driver-standalone-2.20.jar; fi
42+
# Temporarily run HtmlUnit from standalone jar file (it was not part of Selenium server standalone in version 2.53)
43+
- java -cp "jar/selenium-server-standalone-2.53.1.jar:jar/htmlunit-driver-standalone-2.20.jar" org.openqa.grid.selenium.GridLauncher -log selenium.log &
44+
# TODO: use this after upgrade to Selenium 3.0.2
45+
#- /usr/lib/jvm/java-8-oracle/bin/java -Dwebdriver.firefox.marionette=false -jar jar/selenium-server-standalone-3.0.2.jar -log selenium.log &
46+
- until $(echo | nc localhost 4444); do sleep 1; echo waiting for selenium-server...; done
47+
- php -S localhost:8000 -t tests/functional/web/ &>>php-server.log &
3548

3649
script:
37-
- if [ -n "$CHECK_CODESTYLE" ]; then ./vendor/bin/php-cs-fixer fix --diff --dry-run; fi
38-
- if [ -n "$CHECK_CODESTYLE" ]; then ./vendor/bin/phpcs --standard=PSR2 ./lib/ ./tests/; fi
39-
- if [ -z "$CHECK_CODESTYLE" ]; then ./vendor/bin/phpunit; fi
50+
- ./vendor/bin/phpunit
4051

4152
after_script:
4253
- cat selenium.log
54+
- cat php-server.log
55+
56+
addons:
57+
firefox: "latest-esr"
58+
apt:
59+
packages:
60+
- oracle-java8-installer

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
This project versioning adheres to [Semantic Versioning](http://semver.org/).
33

44
## Unreleased
5+
- `Symfony\Process` is used to start local WebDriver processes (when browsers are run directly, without Selenium server) to workaround some PHP bugs and improve poratbility
6+
- Clarified meaning of selenium server URL variable in methods of `RemoteWebDriver` class
57

68
## 1.2.0 - 2016-10-14
79
- Added initial support of remote Microsoft Edge browser (but starting local EdgeDriver is still not supported)

lib/Remote/RemoteWebDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ public function setCommandExecutor(WebDriverCommandExecutor $executor)
479479
}
480480

481481
/**
482-
* Set the command executor of this RemoteWebdriver
482+
* Get the command executor of this RemoteWebdriver
483483
*
484484
* @return HttpCommandExecutor
485485
*/
@@ -512,7 +512,7 @@ public function getSessionID()
512512
}
513513

514514
/**
515-
* Get all selenium sessions.
515+
* Returns a list of the currently active sessions.
516516
*
517517
* @param string $selenium_server_url The url of the remote Selenium WebDriver server
518518
* @param int $timeout_in_ms

tests/functional/BaseTest.php

Lines changed: 0 additions & 109 deletions
This file was deleted.

tests/functional/FileUploadTest.php

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,47 @@
1717

1818
use Facebook\WebDriver\Remote\LocalFileDetector;
1919

20-
/**
21-
* An example test case for php-webdriver.
22-
*
23-
* Try running it by
24-
* '../vendor/phpunit/phpunit/phpunit.php ExampleTestCase.php'
25-
*/
2620
class FileUploadTest extends WebDriverTestCase
2721
{
28-
public function testFileUploading()
22+
public function testShouldUploadAFile()
2923
{
30-
$this->driver->get($this->getTestPath('upload.html'));
31-
$file_input = $this->driver->findElement(WebDriverBy::id('upload'));
32-
$file_input->setFileDetector(new LocalFileDetector())
33-
->sendKeys(__DIR__ . '/files/FileUploadTestCaseFile.txt');
34-
self::assertNotEquals($this->getFilePath(), $file_input->getAttribute('value'));
24+
$this->driver->get($this->getTestPageUrl('upload.html'));
25+
26+
$fileElement = $this->driver->findElement(WebDriverBy::name('upload'));
27+
28+
$fileElement->setFileDetector(new LocalFileDetector())
29+
->sendKeys($this->getTestFilePath());
30+
31+
$fileElement->submit();
32+
33+
$this->driver->wait()->until(
34+
WebDriverExpectedCondition::titleIs('File upload endpoint')
35+
);
36+
37+
$uploadedFilesList = $this->driver->findElements(WebDriverBy::cssSelector('ul.uploaded-files li'));
38+
$this->assertCount(1, $uploadedFilesList);
39+
40+
$uploadedFileName = $this->driver->findElement(WebDriverBy::cssSelector('ul.uploaded-files li span.file-name'))
41+
->getText();
42+
$uploadedFileSize = $this->driver->findElement(WebDriverBy::cssSelector('ul.uploaded-files li span.file-size'))
43+
->getText();
44+
45+
$this->assertSame('FileUploadTestFile.txt', $uploadedFileName);
46+
$this->assertSame('10', $uploadedFileSize);
3547
}
3648

37-
public function testUselessFileDetectorSendKeys()
49+
public function xtestUselessFileDetectorSendKeys()
3850
{
3951
$this->driver->get($this->getTestPath('upload.html'));
52+
4053
$file_input = $this->driver->findElement(WebDriverBy::id('upload'));
41-
$file_input->sendKeys($this->getFilePath());
42-
self::assertEquals($this->getFilePath(), $file_input->getAttribute('value'));
54+
$file_input->sendKeys($this->getTestFilePath());
55+
56+
$this->assertEquals($this->getTestFilePath(), $file_input->getAttribute('value'));
4357
}
4458

45-
private function getFilePath()
59+
private function getTestFilePath()
4660
{
47-
return __DIR__ . '/files/FileUploadTestCaseFile.txt';
61+
return __DIR__ . '/Fixtures/FileUploadTestFile.txt';
4862
}
4963
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
namespace Facebook\WebDriver;
17+
18+
use Facebook\WebDriver\Exception\NoSuchElementException;
19+
use Facebook\WebDriver\Remote\RemoteWebElement;
20+
21+
/**
22+
* Tests for findElement() and findElements() method of RemoteWebDriver.
23+
*/
24+
class RemoteWebDriverFindElementTest extends WebDriverTestCase
25+
{
26+
public function testShouldThrowExceptionOfElementCannotBeFound()
27+
{
28+
$this->driver->get($this->getTestPath('index.html'));
29+
30+
$this->setExpectedException(NoSuchElementException::class, 'Unable to locate element');
31+
$this->driver->findElement(WebDriverBy::id('not_existing'));
32+
}
33+
34+
public function testShouldFindElementIfExistsOnAPage()
35+
{
36+
$this->driver->get($this->getTestPath('index.html'));
37+
38+
$element = $this->driver->findElement(WebDriverBy::id('id_test'));
39+
40+
$this->assertInstanceOf(RemoteWebElement::class, $element);
41+
}
42+
43+
public function testShouldReturnEmptyArrayIfElementsCannotBeFound()
44+
{
45+
$this->driver->get($this->getTestPath('index.html'));
46+
47+
$elements = $this->driver->findElements(WebDriverBy::cssSelector('not_existing'));
48+
49+
$this->assertInternalType('array', $elements);
50+
$this->assertCount(0, $elements);
51+
}
52+
53+
public function testShouldFindMultipleElements()
54+
{
55+
$this->driver->get($this->getTestPath('index.html'));
56+
57+
$elements = $this->driver->findElements(WebDriverBy::cssSelector('ul > li'));
58+
59+
$this->assertInternalType('array', $elements);
60+
$this->assertCount(3, $elements);
61+
$this->assertContainsOnlyInstancesOf(RemoteWebElement::class, $elements);
62+
}
63+
}

0 commit comments

Comments
 (0)