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 第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没法联网
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e