【问题标题】:Selenium2 phpunit submit form wait for page to loadSelenium2 phpunit 提交表单等待页面加载
【发布时间】:2012-08-19 19:09:21
【问题描述】:

我正在尝试运行一个提交空白表单的测试,等待 10 秒以确保页面已重新加载,然后检查是否出现错误消息。

我正在使用我开始使用的 Selenium2

java -jar /usr/local/bin/selenium-server-standalone-2.25.0.jar

我有一些测试可以确保字段存在,例如

public function testEmailFieldIsPresentById()
{
$element = $this->byCssSelector('#email');
$this->assertEquals(1, count($element));
}

我根据阅读的不同文章尝试了不同的函数调用,但都没有工作。

这就是我到目前为止的两次等待尝试已被注释掉的内容。

<?php

class LoginFormSubmitTest extends PHPUnit_Extensions_Selenium2TestCase
{

    protected function setUp()
    {
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://localhost/login');
    }

    public function testWithAllBlankFields()
    {
        // Submit the form
        $this->byId('recording-form-login')->submit();
        // Wait 10 seconds
        //$this->waitForPageToLoad(10000);
        //$this->timeouts()->implicitWait(10000);
    }

}

任何人都可以指出一些关于此问题的好的文档建议解决它的方法吗?

谢谢

【问题讨论】:

    标签: phpunit selenium-webdriver


    【解决方案1】:

    声明

    $this->setBrowserUrl('http://localhost/login');
    

    实际上并没有打开网页。 setBrowserUrl == 为测试设置 base URL。

    这应该对你有用 - 注意额外的行 $this->url('http://localhost/login');

    protected function setUp()
    {
        $this->setBrowser('firefox');
        $this->setBrowserUrl('http://localhost/');
    }
    
    public function testWithAllBlankFields()
    {
        $this->url('http://localhost/login');
        // Submit the form
        $this->byId('recording-form-login')->submit();
        // Wait 10 seconds
        //$this->waitForPageToLoad(10000);
        //$this->timeouts()->implicitWait(10000);
    }
    

    【讨论】:

      【解决方案2】:

      我找到了两个很好的教程,它们解释了需要一个用于 PHP 交互的 Selenium API 的包装器。

      http://testigniter.blogspot.co.uk/2012/01/running-selenium-2-webdriver-using.html http://edvanbeinum.com/using-selenium-2-phpunit-to-automate-browser-testing

      https://github.com/facebook/php-webdriver 下载包装器后,我的代码现在如下所示,并捕获了 tearDown 函数失败的屏幕截图

      <?php
      
      // Include the Facebook PHP Webdriver init file
      require_once '../php-webdriver/__init__.php';
      
      class loginFormSubmitTest extends PHPUnit_Framework_TestCase {
      
          /**
          * @var WebDriverSession
          */
          protected $_session;
      
          public function setUp()
          {
              parent::setUp();
              $web_driver = new WebDriver();
              $this->_session = $web_driver->session();
          }
      
          public function tearDown()
          {
      
              // Get the status of the test
              $status = $this->getStatus();
      
              // Check if the status has an error or a failure
              if ($status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR || $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
                  // Take a screenshot
                  $image_data = base64_decode($this->_session->screenshot());
      
                  file_put_contents(date('Y-m-d-H-i-s') . '.png', $image_data);
              }
      
              $this->_session->close();
              unset($this->_session);
              parent::tearDown();
          }
      
          public function test_submit_form_with_all_blank_fields()
          {
      
              $this->_session->open('http://localhost/login');
      
              $this->_session->element(
                  'id',
                  'recording_form_login_submit'
              )->click();
      
              $email_label_span_text = $this->_session->element('css selector', '#recording-form-login label[for="email"] span')->text();
      
              $this->assertSame(
                  'Required',
                  $email_label_span_text
              );
      
          }
      
      }
      

      【讨论】:

      • 你这里的open方法是Selenium 1相当于Selenium 2中的url方法
      猜你喜欢
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多