Yii, Selenium and multiple submit buttons - ajax

I've got a form with two submit buttons which I want to test using Selenium.
View:
<?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(); ?>
Controller is nothing special, you can assume it just prints "Your profile has been saved" or "Your profile was not saved" depending on what $_POST['cancel'] it gets
Test code:
<?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');
The problem is that code works great in browser but not when running tests in Selenium/Firefox. When running tests, it "sees" the first button only (Cancel), clicking "Save changes" has the same effect. If you place Save changes button first, it will not "see" Cancel button.
If you turn enableAjaxValidation off, it works both in browser and Selenium, but I'd like to have a more elegant solution of course. Like for example turning off the AJAX validation on clicking on Cancel.
No, the problem doesn't depend on which locator you use for buttons (xpath, css, id).

clickAndWait() calls waitForPageToLoad() - ajax form validation will generally not trigger this event (unless a page loads), so your test will never complete; this is probably why if you turn ajax validation off it works.
You might want to look at the other options selenium provides (this is an old link to the free phpunit pocket guide that describes some other options - it's based on phpunit 3.1 though) such as using click() and then using waitForCondition() with some javascript to run and return true if the new text is displayed.

Related

Using codeigniter to execute code from another file

I am new to CodeIgniter and I am used to the old school php scripting so I'll need some help with this:
I want to include a Captcha system in one of my forms.
According to its documentation, to generate an image, you need to do it like this:
<img id="captcha" src="/securimage/securimage_show" alt="CAPTCHA Image" />
I downloaded the files, but where do I put them? And how do I use Codeigniter to call the securimage_show.php file? And output its contents into the src attribute of the image?
When adding Captcha in Fuel (a codeigniter based cms), I've put the php file that generates the Captcha image in the folder where you put images, then link to it the same way you link to an image:
<?php echo img(array('src'=>'image_show.php', 'alt'=> 'CAPTCHA Image')); ?>
Perhaps not the nicest solution, but it works.
Alternatively, just use a Captcha plugin written specifically for codeigniter, such as the NuCaptcha CodeIgniter plugin, http://docs.nucaptcha.com/plugins/codeigniter.
Codeigiter have a captcha helper.
First, you want to create a folder where you will be able to store your captcha images and give this folder permissions to perform read/write operations. In this case, I've created captcha folder in root of my codeigniter instance.
Then, we want to load captcha helper:
$this->load->helper('captcha');
Let's initiate instance of captcha with our settings (you can do it either in Controller or View with your form):
$rand_string = strtoupper(random_string('nozero', 4));
$settings = array(
'word' => $rand_string,
'img_path' => './captcha/',
'img_url' => base_url() .'captcha/',
'img_width' => '250',
'img_height' => 35,
'expiration' => 7200
);
$cap = create_captcha($settings);
$this->session->set_userdata('captchaWord',$cap['word']);
Please note, that I'm keeping generated captcha word in my session whenever I create it (for instance on page refresh). This way I can compare original captcha word with input from my form. Then, I will display generated captcha image with input field somewhere in my form (View):
<form id="my_form">
<input type="text" name="captcha" value=""/>
<?= $cap['image']; ?>
</form>
Now, all I have to do, is to compare input received from my_form with actual captcha value (in my controller, where I handle form submission):
$userCaptcha = $this->input->post('captcha');
$actual_word = $this->session->userdata('captchaWord');
if( strcmp(strtoupper($userCaptcha),strtoupper($actual_word)) == 0 ) {
// input and captcha are the same
}

cakephp ajax not working properly

I am new in cakephp and I want to implement Ajax on my home page.
I have three modules in my page (client, developer and project). I want to add an ajax link. It's working perfectly only in the index page.
My code:
<h2>Projects</h2>
<div class="clear"></div>
<ul>
<li title="Project List">
<?php echo $ajax->link('Projects List', array("controller" => "projects", "action" => "index"), array( 'update' => 'main_page' ));?>
</li><br />
<li title="Add New Project">
<?php echo $ajax->link('Add New Project', array("controller" => "projects", "action" => "add"), array( 'update' => 'main_page' ));?>
</li>
</ul>
Now, my first issue is that in the add form, the validation with js is not working.
Second is: if I use cakephp inbuilt validation then it validates my form but redirects the page to "admin/projects/add" if no data is inserted.
Third problem is that when the above case happens and I want to redirect to listing page through my ajax link, at that time its also not working.
The best thing to do is stop using the ajax helper. Its been depreciated and will not be available in the 3.x branch.
This was done because it was not a good idea to start with, very limiting and buggy.
ajax with something like jQuery is not very difficult and you should rather look into using that or a similar tool.

codeigniter form helper wiredness

I have some of wiredness going on in some code I am writing. I have a regular form to update a user account. The fields are populated with data from the database. after changing that needs to be changed, I can't submit the form. when I click on the button, it behaves like disabled submit with javascript but I didn't. On the otherhand if I use javascript and stop it from submitting and console log to see if a click is happening, it appears the button is being clicked but just nothing. below is my code in my view for the form.
form_open('members/users/update_curr_user');
$data5 = array('name'=>'username','id'=>'username','value'=>$uservar['username']);
echo 'Username :'.form_input($data5);
$data6 = array('name'=>'email','id'=>'email','value'=>$uservar['email']);
echo 'Email Address :'.form_input($data6);
$phone1 = array('name'=>'phone','id'=>'phone','value'=>$uservar['phone']);
echo 'Your phone number formatted like so: 0802-331-5544'.form_input($phone1);
switch ($uservar['active']):
case 0:
$data7 = array(
'name'=>'status',
'id'=> 'status',
'value' =>'Deactivated'
);
echo 'Status : Active or Deactivated'.form_input($data7);
break;
case 1:
$data8 = array(
'name' =>'status',
'id' =>'status',
'value'=>'active'
);
echo 'Status :Active or Deactivated'.form_input($data8);
break;
endswitch;
$group1 = array('name'=>'group','id'=>'group','value'=>$uservar['group']);
echo 'Group :'.form_dropdown('group',$groups).'<br />';
echo '<br /><br />';
//$data9 = 'id="updateuser"';
//echo form_submit('submit','Update User',$data9);
?>
<input name="submit" id="updateuser" type="submit" value="Update User" />
<?php echo form_close();?>
Because of how unsure I was of what was going on I manually created a button still the same. Other forms on the page are working ok. If it's of any consequence, I am using phil sturgeon's template library, ion_auth and firephp.
You're not actually writing out the form tag. You need to put an echo up there. Do this:
echo form_open('members/users/update_curr_user');
Your submit button should now work.

CodeIgniter dropdown (Javascript?)

Is there any way I can make a dropdown submit a form without clicking a button to to submit it. I want to be able to change languages on the fly in my site. I have the languages all set up. Here's my dropdown:
<?php echo form_open('languages');
$language = array(
'select' => 'Select Language',
'english' => 'English',
'spanish' => 'Español',
'german' => 'Deutsch',
'french' => 'Français'
);
echo form_dropdown('language', $language);
echo form_hidden('current_page', uri_string());
echo form_submit('submit', $this->lang->line('header6'));
echo form_close();
?>
How do I get the form to work without the submit button?
The easiest way would be to use jQuery. Your form and dropdown should have IDs to make referencing them easier.
$attributes = array('id' => 'form1');
echo form_open('languages', $attributes);
...
echo form_dropdown('language', $language, null, 'mydropdown');
Also include the following javascript (you'll need to load jQuery on the page for this to work). I've included Google's hosted version but you could install it locally if you prefer.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#mydropdown").live("change keyup", function () {
$("#form1").submit();
});
});
</script>
You don't strictly need to bind to keyup, you could just bind to the change event (in which case you could replace live("change") with simply change() but keyup covers keyboard changes to the dropdown.
I use onchange with any live select menu... makes it very very easy to fire off other methods.
<select id="myID" onchange="alert('Fired!')"><option value="gold">Beer</option>

Zend Framework fancybox confirmation dialog with ajax and posted values

I created a confirmation page for deleting an item. This works perfectly.
Now I want to appear the confirmation page in fancybox, still passing all the variables and delete the data or close the dialog.
It's important that the process still works as it does now (delete->confirmationpage->redirect/deletion) so that users with javascript disabled can perform these actions without a problem.
Now have I been reading about zend framework,ajax, json and more, but the more I read, the less I understand.
So in a nutshell, my question:
I want to pass a variable to the fancybox and if 'yes' perform the delete action or on 'no' close the dialog. This within the zend framework and jquery.
Any advice or tips are greatly appreciated!
You need to use content switching in your ajax which will then render your action appropriately, for example:
function confirmDeleteAction() {
if($this->_request->isXmlHttpRequest()) {
//This is ajax so we want to disable the layout
$this->_helper->layout->disableLayout();
//Think about whether you want to use a different view here using: viewRenderer('yourview')
}
//The normal code can go here
}
function deleteAction() {
//You probably don't want to show anything here, just do the delete logic and redirect therefore you don't need to worry where it's coming from
}
And in your fancybox, have a view that has a form and two buttons, the form should point to your delete action with the id of whatever your deleting as a get param. Set some javascript up that says something like (and this is mootools code, but you can convert it easily):
$('confirmButton').addEvent('click', function(e) {
e.preventDefault();
this.getParent('form').submit();
}
$('cancelButton').addEvent('click', function(e) {
e.preventDefault();
$('fancyBox').destroy(); //I'm not sure it has an id of fancyBox, never used it
}
Came across the question today and figured I could give it a fresh look. For Zend Framework the solution is really simple:
This is how the action looks:
public function deleteAction()
{
$this->_helper->layout()->disableLayout();
$this->view->news = $this->newsService->GetNews($this->_getParam('id'));
if($this->getRequest()->isPost())
{
if($this->getRequest()->getPost('delete') == 'Yes')
{
$this->newsService->DeleteNews($this->_getParam('id'), $this->view->user->username, $this->view->translate('deleted: ').'<strong>'.$this->view->pages[0]['title'].'</strong>');
$this->_helper->flashMessenger(array('message' => $this->view->translate('The page is deleted'), 'status' => 'success'));
$this->_helper->redirectToIndex();
}
elseif($this->getRequest()->getPost('delete') == 'No')
{
$this->_helper->flashMessenger(array('message' => $this->view->translate('The page is <u>not</u> deleted'), 'status' => 'notice'));
$this->_helper->redirectToIndex();
}
}
}
The delete.phtml
<div>
<h2><?php echo $this->translate('Delete news'); ?></h2>
<?php
foreach($this->news as $news)
{
?>
<p>
<?php echo $this->translate('You are now deleting <strong>\'').$news['title'].$this->translate('\'</strong>. Are you sure about this?'); ?>
</p>
<p>
<?php echo $this->translate('<strong>Note! </strong>This action is inreversable, even for us!'); ?>
</p>
<form action="<?php echo $this->baseUrl(false).'/news/index/delete/'.$news['id']; ?>" method="post">
<?php
}
?>
<input class="submit deleteYes" type="submit" name="delete" value="<?php echo $this->translate('Yes'); ?>" />
<input class="submit deleteNo" type="submit" name="delete" value="<?php echo $this->translate('No'); ?>" />
</form>
And this is how the link to delete a file looks (within a foreach loop with my database results)
<a class="deleteConfirmation" href="<?php echo $this->baseUrl(false).'/news/index/delete/'.$news->id; ?>">delete</a>
This works like you would expect; when you click on delete the user goes to the delete confirmation page and redirects the user back to the index after the form is submitted. But I wanted the confirmation in a dialog (in my case I use fancybox). To achieve this, add the following jquery to your index:
$('.deleteConfirmation').fancybox({
// Normal fancybox parameters
ajax : {
type : "POST"
}
});

Resources