【发布时间】: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