I'm trying to add some simple tests to my Symfony 4 project using PHPUnit and Selenium. Problem is that it's impossible to locate any HTML elements whatsoever, it's driving me nuts.
My test:
class WebTest extends PHPUnit_Extensions_Selenium2TestCase {
protected function setUp() {
$this->setBrowser('firefox');
$this->setBrowserUrl('http://localhost:8000');
}
public function testPageOnline() {
$this->url('/');
$this->assertEquals('Home page', $this->title());
}
public function testHasForm() {
$this->url('/');
$action = $this->byCssSelector('form')->attribute('action');
$this->assertEquals('message', $action);
$name = $this->byName('name');
$dob = $this->byName('dob');
// Verify these input boxes are empty
$this->assertEquals('', $name->value());
$this->assertEquals('', $dob->value());
}
}
The first test ("testPageOnline") always succeeds, but the second always fails, no matter what kind of variations I try. My html code is nothing special, no javascript or anything, just a static page:
<form action="message" method="POST">
<div class="row p-2">
<div class="col-2"><label for="name">Name:</label></div>
<div class="col-2"><input type="text" name="name"></div>
</div>
<div class="row p-2">
<div class="col-2"><label for="dob">Date of birth:</label></div>
<div class="col-2"><input type="text" name="dob"></div>
</div>
<div class="row p-2">
<div class="col-2"><label for="car">Car</label></div>
<div class="col-2">
<select name="car">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</div>
</div>
<div class="row p-2">
<div class="col-2"><label for="message">Message:</label></div>
<div class="col-2"><input type="text" name="message" /></div>
</div>
<div class="row p-2">
<div class="offset-1 col-3">
<button type="submit" id="post" class="btn btn-block btn-dark">Post</button>
</div>
</div>
</form>
PHPUnit output:
PHPUnit 7.5.14 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.8-1+ubuntu18.04.1+deb.sury.org+1 Configuration: /home/xxxx/capsearch/phpunit.xml
Testing default .E
2 / 2 (100%)Time: 5.72 seconds, Memory: 4.00 MB
There was 1 error:
1) WebTest::testHasForm InvalidArgumentException: Element not found.
Can anyone help me figure out whats wrong? I have tried to find different elements, using different selectors (class, ID, name), and I'm 100% sure it's on the correct page because the first test passes.