duanjiaoxi4928 2019-08-12 16:35
浏览 89
已采纳

PHPUnit / Selenium无法找到* any *元素

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.

  • 写回答

1条回答 默认 最新

  • du1108 2019-08-12 18:14
    关注

    I have found the solution on this GitHub post: https://github.com/SeleniumHQ/selenium/issues/5539

    I was using Selenium 3.9, apparently this is broken and I should use 3.8.1 with the '-enablePassThrough false' flag. Now it works!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突