Emulate keypress in webdriver using php - php

I have a problem with trigger keypress event in WebDriver with using php. There is element with class > test
On this element bind keypress by jquery . I try to click,but its no result
$this->_city = $this->driver->findElement(WebDriverBy::className('test'));
$this->_city->click()
Plz, help me, who know how to emulate keypress on webdriwer with using php.

$this->_city = $this->driver->findElement(WebDriverBy::className('test'));
$this->_city->click()
$this->driver->getKeyboard()->sendKeys('TEXT HERE'); // this will insert text in the box
$this->driver->getKeyboard()->pressKey(WebDriverKeys::ENTER); // This will do a enter or whatever key you like to press ( not letter/numbers move ARROW_UP or whatever you like to presskey)
Here are some other keys for the driver:
Check out more Keys from WebDriverKey
CHECK getKeyboard() Methods

The only one worked for me is WebDriverKeys :
$driver->getKeyboard()->pressKey(WebDriverKeys::ENTER);
Hope this helps.

You mention a keypress event, so some type of key on the keyboard being pressed is what you are looking for? Using the click() event is emulating a mouse click on the element in question. You will probably want to use the sendKeys() function once the element has focus.
$this->_city = $this->driver->findElement(WebDriverBy::className('test'));
$this->_city->click()
$this->_city->sendKeys('A');

Related

zend onChange submit

i'm trying to submit a form with the event "onchange" on a Zend_Form_Element_Select but i dont succeed. with a simple button Zend_Form_Element_Submit i dont have probleme, but impossible with the onchange on my liste.
the code on my form:
$this->setName('myparamcliform');
$this->setAttrib('id', 'myformm');
$this->setAction('/first/second/index');
$Liste5 = new zend_psai_Liste();
$ListeActivite = $Liste5->ListeActivite();
$activite = new Zend_Form_Element_Select('activite',array('onchange' => "alert('hello 6');;"));
$activite->setLabel('')
->setMultiOptions($ListeActivite);
there is no problem with a simple alert message, but when i replace it by a submit function, it doesn't work.
different element found on the web (thanks stackoverflow :) ) i tryed :
//documents.forms['myformm'].submit();
//document.myparamcliform.submit();
//->setAttrib('onChange', "this.form.submit();");
//document.getElementById('myformm').submit()
//,array('onchange' => 'this.form.submit();')
$mysubmit = new Zend_Form_Element_Submit('mysubmit');
$mysubmit->setLabel('RECHERCHER');
$this->addElements(array($activite,$mysubmit));
with these different solution, it doesn't work. no error message and no action with the "onchange", but no probleme with the simple alert message.
Why you don't assign a class or id to a button and bind a function to it which will be in some.js file?
It will work for sure and this way you will increase maintainability of your scripts. Simple functions like alert() is ok to write inline, but more complex scripts better to put separately from the template.

Set input field focus on start typing

I am looking for a way to be able to start typing on a website without having selected anything and then have a specific input field in focus.
Google also employs this feature. In their search results you can click anywhere (defocus the search field) and when you start typing it automatically focuses on the search field again.
I was thinking about jQuery general onkeyup function to focus on the field, any suggestions?
Much appreciated.
You should bind the keydown event, but unbind it immediately so that typing may continue in other text inputs without reverting focus to the default input.
$(document).bind('keydown',function(e){
$('#defaultInput').focus();
$(document).unbind('keydown');
});
See example here.
The answer is as simple as this:
$(document).keydown(function() { $('#element').focus(); });
keydown is preferred after all because keyup will only be fired after the first key is pressed - and respectively not capture the first key typed in my search field.
This solution does not have the problem that #mVChr's solution has: Ie you can click on another input with the mouse and start typing without losing focus due to the keydown-binding.
Also this solution does not remove all the element's keydown-bindings, but uses a named handler instead.
var default_input_handler = function() {
$('.default-input').focus();
$(document).off('keydown', default_input_handler);
}
$(document).on('keydown', default_input_handler);
$('input, textarea, select').on('focus', function() {
$(document).off('keydown', default_input_handler);
});
If planning to do this I'd say use onKeyUp instead of onKeyDown. Its much earlier in the action which would help ease the flow of the interaction.

JS script running for every element, rather than simple clicked element

using jQuery, I am trying to get an action assigned to a button on a form. I have 50 of these forms on a page, but every time I click one link, the form action is run 50 time!!
I ran this to check, and it comes to exactly 50
`i=1
$('.thumbs').click(function(){
console.log(i,"Click Count");
i++;
)};
this has the unhelpful effect of running the AJAX fifty times!!
am I using selectors wrong?
EDIT: full script here
EDIT: Example of one element - There are 50 of these inside a container div.
Are you adding the listener to a submit button? Make sure to return true or false if the form should be submitted or not.
it sounds like it could be one of there two:
1) you've added 50 click listners to one button:
you could try this by adding:
$('.thumbs').unbind();
before you assign a new handler.
If this is the case, you should try to find out why and make sure you only add one.
2) the button default action is the problem:
stop default behavior:
$('.thumbs').click(function(event){
event.preventDefault();

how to update two input boxes in jquery

i create two input text fields , one for title and another for permanent link
i need to update the second filed automatically when user is typing the tilte
how can i do such a thing in jquery /php
somehow im looking for a way to simulate wordpress creation of permanent link in post section
$('#first_input_id').bind('keydown', function(e){
var inputmirror = $('#second_input_id');
inputmirror.val(inputmirror.val() + String.fromCharCode(e.which));
});
Something similar to this should do it.
Write a function to read from the current input, munge it however you like, and insert it into the other input.
Then bind that function to the keypress and change events.
You can intercept keyup event on the first input text, and then update the output of the second input:
$('#titleInput').keypress(function(e) { alert ('typed' + String.fromCharCode(e.keyCode))//update your 2nd input text...
}

Add and remove form fields in Cakephp

Im looking for a way to have a form in cakephp that the user can add and remove form fields before submitting, After having a look around and asking on the cake IRC the answer seems to be to use Jquery but after hours of looking around i cannot work out how to do it.
The one example i have of this in cake i found at - http://www.mail-archive.com/cake-php#googlegroups.com/msg61061.html but after my best efforts i cannot get this code to work correctly ( i think its calling controllers / models that the doesn't list in the example)
I also found a straight jquery example (http://mohdshaiful.wordpress.com/2007/05/31/form-elements-generation-using-jquery/) which does what i would like my form to do but i cannot work out how to use the cakephp form helper with it to get it working correctly and to get the naming correct. (obviously the $form helper is php so i cant generate anything with that after the browser has loaded).
I an new to cake and have never used jQuery and i am absolutely stumped with how to do this so if anyone has a cakephp example they have working or can point me in the right direction of what i need to complete this it would be very much appreciated.
Thanks in advance
I would take the straight jquery route, personally. I suppose you could have PHP generate the code for jquery to insert (that way you could use the form helper), but it adds complexity without gaining anything.
Since the form helper just generates html, take a look at the html you want generated. Suppose you want something to "add another field", that when clicked, will add another field in the html. Your html to be added will be something like:
<input type="text" name="data[User][field][0]" />
Now, to use jquery to insert it, I'd do something like binding the function add_field to the click event on the link.
$(document).ready( function() {
$("#link_id").click( 'add_field' );
var field_count = 1;
} );
function add_field()
{
var f = $("#div_addfield");
f.append( '<input type="text" name="data[User][field][' + field_count + ']" />' );
field_count++;
}
Of course, if a user leaves this page w/o submitting and returns, they lose their progress, but I think this is about the basics of what you're trying to accomplish.
This was my approach to remove elements:
In the view, I had this:
echo $form->input('extrapicture1uploaddeleted', array('value' => 0));
The logic I followed was that value 0 meant, not deleted yet, and value 1 meant deleted, following a boolean logic.
That was a regular input element but with CSS I used the 'display: none' property because I did not want users to see that in the form. Then what I did was that then users clicked the "Delete" button to remove an input element to upload a picture, there was a confirmation message, and when confirming, the value of the input element hidden with CSS would change from 0 to 1:
$("#deleteextrapicture1").click(
function() {
if (confirm('Do you want to delete this picture?')) {
$('#extrapicture1upload').hide();
// This is for an input element that contains a boolean value where 0 means not deleted, and 1 means deleted.
$('#DealExtrapicture1uploaddeleted').attr('value', '1');
}
// This is used so that the link does not attempt to take users to another URL when clicked.
return false;
}
);
In the controller, the condition $this->data['Deal']['extrapicture1uploaddeleted']!='1' means that extra picture 1 has not been deleted (deleting the upload button with JavaScript). $this->data['Deal']['extrapicture1uploaddeleted']=='1' means that the picture was deleted.
I tried to use an input hidden element and change its value with JavaScript the way I explained above, but I was getting a blackhole error from CakePHP Security. Apparently it was not allowing me to change the value of input elements with JavaScript and then submit the form. But when I used regular input elements (not hidden), I could change their values with JavaScript and submit the form without problems. My approach was to use regular input elements and hide them with CSS, since using input hidden elements was throwing the blackhole error when changing their values with JavaScript and then submitting the form.
Hopefully the way I did it could give some light as a possible approach to remove form fields in CakePHP using JavaScript.

Categories