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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化