Selenium を使用してテストしたい 2 つの送信ボタンを含むフォームがあります。
意見:
<?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) に使用するロケーターには依存しません。