【问题标题】:Yii, Selenium and multiple submit buttonsYii、Selenium 和多个提交按钮
【发布时间】:2013-04-10 12:44:56
【问题描述】:

我有一个带有两个提交按钮的表单,我想使用 Selenium 进行测试。

查看:

<?php
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'profile-form',
    'enableAjaxValidation' => true,
    'action' => '',
    'clientOptions' => array(
        'validateOnSubmit' => true,
        'validateOnChange' => true,
        'validateOnType' => false,
    ),
));
?>

<input name="cancel" type="submit" value="Cancel" />
<input name="submit" type="submit" value="Save changes" />

<?php $this->endWidget(); ?>

控制器没什么特别的,您可以假设它只是打印“您的个人资料已保存”或“您的个人资料未保存”,具体取决于它得到的 $_POST['cancel']

测试代码:

<?php
$this->open('/profile_form_url');
$submit_button_selector = 'css=#profile-form input[name="submit"]';
$cancel_button_selector = 'css=#profile-form input[name="cancel"]';

$this->clickAndWait($cancel_button_selector);
$this->assertTextPresent('Your profile was not saved');

$this->open('/profile_form_url');
$this->clickAndWait($submit_button_selector);
$this->assertTextPresent('Your profile has been saved');

问题是代码在浏览器中运行良好,但在 Selenium/Firefox 中运行测试时却不行。运行测试时,它只“看到”第一个按钮(取消),单击“保存更改”具有相同的效果。如果您先放置保存更改按钮,它将不会“看到”取消按钮。

如果您关闭 enableAjaxValidation,它可以在浏览器和 Selenium 中运行,但我当然希望有一个更优雅的解决方案。例如,在单击“取消”时关闭 AJAX 验证。

不,问题不取决于您为按钮使用的定位器(xpath、css、id)。

【问题讨论】:

  • 您是否尝试过为按钮添加 id 并将其用作选择器?
  • 是的(见最后一句话)。
  • 您是否尝试使用 setSpeed() 函数减慢 Selenium 步骤?
  • 没有,只是试了一下——不影响问题。无论如何,感谢您的提示,它可能在其他地方有用。
  • 什么版本的 Selenium,你的 PHP 绑定和 Firefox?

标签: ajax firefox selenium yii functional-testing


【解决方案1】:

clickAndWait() 调用waitForPageToLoad() - ajax 表单验证通常不会触发此事件(除非页面加载),因此您的测试永远不会完成;这可能就是为什么如果你关闭 ajax 验证它可以工作。

您可能想查看 selenium 提供的其他选项(this is an old link to the free phpunit pocket guide 描述了一些其他选项 - 虽然它基于 phpunit 3.1),例如使用 click(),然后使用 waitForCondition() 和一些 javascript 来运行和返回如果显示新文本,则为 true。

【讨论】:

  • 不幸的是,事实并非如此。 clickAndWait() 触发 ajax 验证,然后触发表单提交,所以这部分工作正常。 click() - waitForCondition() 不起作用,因为实际提交了表单并且页面加载。问题是提交带有不正确的提交按钮值。无论如何感谢您的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多