0

I am trying to make a test suit for a php project using phpunit and phpunit-selenium. In my composer file I have

"require-dev": {
    "phpunit/phpunit": "^5.7",
    "phpunit/phpunit-selenium": "^3.0"
}

The installed phpunit version is 5.7.4 I am using selenium-server-standalone-3.0.1.jar as the selenium server. I start the server with java -Dwebdriver.gecko.driver="C:\Harlan\Selenium\geckodriver.exe" -jar selenium-server-standalone-3.0.1.jar

In my test class I have

require_once dirname(__FILE__) . '/../../vendor/autoload.php';
class UserSubscriptionTest extends PHPUnit_Extensions_Selenium2TestCase {

public function setUp() {
    $this->setHost('localhost');
    $this->setPort(4444);
    $this->setBrowserUrl('http://localhost/cwckids/web/user/login');
    $this->setBrowser('firefox');


}

public function tearDown() {
    $this->stop();
}



public function testFormSubmissionWithUsername()
{
$this->byName('login-form[login]')->value('admin');
$this->byName('login-form[password]')->value('mypassword');
$this->byId('login-form')->submit();

$content = $this->byTag('body')->text();
$this->assertEquals('Everything is Good!', $content, 'something wrong!!');
}    
}

My problem is that the firefox browser opens up but doesn't load the page http://localhost/cwckids/web/user/login

The test fails immediately because it cannot find the elements. It give a message saying Unable to locate element: {"method":"name","selector":"login-form[login]"}

I couldn't find a solution for the problem. Is it some version incompatibility? I tried with a few versions of Firefox and also selenium server. My Firefox version is 50.1.0. If it is a version incompatibility can someone suggest correct versions? Thanks

The complete trace

C:\xampp\htdocs\seleniumtest>phpunit tests/acceptance/UserSubscriptionTest.php PHPUnit 5.7.4 by Sebastian Bergmann and contributors.

E 1 / 1 (100%)

Time: 6.51 seconds, Memory: 9.25MB

There was 1 error:

1) UserSubscriptionTest::testFormSubmissionWithUsername PHPUnit_Extensions_Selenium2TestCase_WebDriverException: Unable to locate element: {"method":"name","selector":"login-form[login]"} For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03' System info: host: 'DESKTOP-I0LAEAM', ip: '192.168.8.101', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_77' Driver info: driver.version: unknown

C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Driver.php:165 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\CommandsHolder.php:108 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Element\Accessor.php:134 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Element\Accessor.php:175 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase\Element\Accessor.php:108 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase.php:394 C:\xampp\htdocs\seleniumtest\tests\acceptance\UserSubscriptionTest.php:66 C:\xampp\htdocs\seleniumtest\tests\acceptance\UserSubscriptionTest.php:66 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase.php:348 C:\xampp\htdocs\seleniumtest\vendor\phpunit\phpunit-selenium\PHPUnit\Extensions\Selenium2TestCase.php:314

ERRORS! Tests: 1, Assertions: 0, Errors: 1.

1 Answer 1

1

There's a few things I'd suggest changing:

You are missing a call to $this->url(); from, what I can see within your test method:

public function testFormSubmissionWithUsername()
{
    $this->url('your actual URL here'); // Add this line
    $this->byName('login-form[login]')->value('admin');
    $this->byName('login-form[password]')->value('mypassword');
    $this->byId('login-form')->submit();

    $content = $this->byTag('body')->text();
    $this->assertEquals('Everything is Good!', $content, 'something wrong!!');
}    

You are also not calling the parent::setUp() method from within your own setUp method:

protected function setUp()
{
    parent::setUp();

    // Set your browser, port setup etc here
}

There's also no need to explicitly call $this->stop(); from within tearDown, so remove that function completely.

Lastly, I'd tell selenium to take screenshots of failures, they save a lot of time:

/**
 * Override this method from \PHPUnit_Framework_TestCase so we can capture a screenshot.
 *
 * @return void
 */
public function onNotSuccessfulTest($exception)
{
    $filedata   = $this->currentScreenshot();
    $file       = YOUR_SCREENSHOT_DIR . '\testfails\\' . basename(get_class($this)) . '.png';
    file_put_contents($file, $filedata);

    parent::onNotSuccessfulTest($exception);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. Your answer really helped. Not sure why $this->setBrowserUrl('localhost/cwckids/web/user/login'); doesn't set the url, but your solution works fine. I like the screenshot idea too. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.