Behat + mink how to test jquery-ui autocomplete field? - jquery-ui-autocomplete

I'm using Behat to test a registration page.
This page contains a few fields with autocompletion.
User fills in some value in the field, page waits 500 milliseconds, makes an ajax-request and displays some options along with a relevant message (no items found / several items found / one item found).
I'm using a predefined step "I fill in field with value" (tried to use "I fill in value for field" instead).
Next step is custom, similar to one described in documentation - I'm just waiting for message to appear and check that it has correct text.
But after filling the field Mink removes focus from it, causing blur event to be fired on the field.
Jquery-ui clears the field value in response to this blur event, so after 500 milliseconds field is empty and ajax request is aborted.
How can I fix this problem?
Behat v2.5.0
Mink v1.5.0
Mink-extension v1.3.3
Jquery-ui v1.8.21
Selenium v2.44.0

Solution below is specific to my markup code but should be easily adapted to yours. You might need to modify it.
Assuming that selecting auto suggested option in textbox. Autosuggestion options appear as <li> elements after user’s key strokes. <li> element presents in the page however its content is empty at the beginning so there is nothing to be selected by behat. To solve the problem, non existing content between <li> tags is wrapped with <label> tag.
User types in this auto suggestion field:
<input name="brand" type="text" />
Where autosuggestion data appears:
<ul>
<li><label>{{ suggestion }}</label></li>
</ul>
Gherkin:
When I fill in "brand" with "S"
And I wait 1 seconds
Then I select autosuggestion option "Sula"
And I wait 1 seconds
......
FeatureContext:
/**
* #Given /^I wait (\d+) seconds$/
*/
public function iWaitSeconds($seconds)
{
sleep($seconds);
}
/**
* #Then /^I select autosuggestion option "([^"]*)"$/
*
* #param $text Option to be selected from autosuggestion
* #throws \InvalidArgumentException
*/
public function selectAutosuggestionOption($text)
{
$session = $this->getSession();
$element = $session->getPage()->find(
'xpath',
$session->getSelectorsHandler()->selectorToXpath('xpath', '*//*[text()="'. $text .'"]')
);
if (null === $element) {
throw new \InvalidArgumentException(sprintf('Cannot find text: "%s"', $text));
}
$element->click();
}

#Scenario: I'm stuck
Given I use jquery-ui-autocomplete
And I can not test it with behat
Then I trigger keyDown event like this:
"""
/**
* #When I select :entry after filling :value in :field
*/
public function iFillInSelectInputWithAndSelect($entry, $value, $field)
{
$page = $this->getSession()->getPage();
$field = $this->fixStepArgument($field);
$value = $this->fixStepArgument($value);
$page->fillField($field, $value);
$element = $page->findField($field);
$this->getSession()->getDriver()->keyDown($element->getXpath(), '', null);
$this->getSession()->wait(500);
$chosenResults = $page->findAll('css', '.ui-autocomplete a');
foreach ($chosenResults as $result) {
if ($result->getText() == $entry) {
$result->click();
return;
}
}
throw new \Exception(sprintf('Value "%s" not found', $entry));
}
"""

Use this to Fill in the search box: (using the ID selector)
/**
* #Then I type :text into search box
*/
public function iTypeTextIntoSearchBox($text)
{
$element = $this->getSession()->getPage()->findById('searchInput');
$script = "$('#searchInput').keypress();";
$element->setValue($text);
$this->getSession()->evaluateScript($script);
}

Related

MailChimp Campaign Content Update

MailChimp campaign content docs - https://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/content
I'm trying to replace some placeholders in a campaign content with actual values via the API. At first, I thought there might be some syntax errors or internal logic errors like non-unique mc:edits into a mc:repeatable that would get the HTML refused/declined by MailChimp, hence the update not taking place, however, that was not the case.
Tried replacing html with a simple <p>test</p> and it was still not working.
Here are a couple of local logs, I'll use xyz as my campaign id:
2018-02-26 16:26:13 [::1][9804][-][warning][application] calling GET /campaigns/xyz/content []
2018-02-26 16:26:13 [::1][9804][-][warning][application] got both plain_text and html versions of content
2018-02-26 16:26:13 [::1][9804][-][warning][application] calling PUT /campaigns/xyz/content {"html":"<p>test</p>"}
2018-02-26 16:26:14 [::1][9804][-][warning][application] got response [
'plain_text' => 'test' + other MailChimp stuff such as footer, that were appended automatically by MailChimp,
'html' => '<p>test</p>'
]
// calling GET immediately after PUT in order to see if any update occurred
2018-02-26 16:26:14 [::1][9804][-][warning][application] calling GET /campaigns/xyz/content []
2018-02-26 16:26:14 [::1][9804][-][warning][application] got updated html (my "test" paragraph + auto footer from MailChimp) and proper plain_text
Everything looks fine according to these, that means both versions updated as they were supposed to. However, on the next API/MailChimp dashboard request, it displays the old HTML content, preserving the update I've just made in the plain text version only.
No errors, nothing to look into. It could be any internal MailChimp behaviour.
PS: I know about Setting Mailchimp campaign content html not working or MailChimp API v3 campaign content template sections, but none of the answers provided to those are helpful.
PS2: I know I should contact MailChimp, but according to
Our MailChimp Support Team isn't trained at in-depth API troubleshooting. If you need a developer to help you configure something using the API, check out our great Experts Directory, which lists third-party MailChimp experts who can be hired to help out.
they don't provide support for API troubleshooting.
MailChimp doesn't allow updating the campaign's HTML content because the campaign type is based on a template.
In order to update the HTML content, the campaign has to be set to custom HTML instead of a template. You can check the type by sending a GET API request to /campaigns or /campaigns/{campaign_id} and finding the content_type attribute in the response (documentation).
Alternatively, in the dashboard, the type can be determined by editing the design of the email. Anything under 'Code your own' is HTML and templates are of course templates.
I'm not entirely sure why the first response to a PUT request on a template campaign shows the updated content, but changing the content type should let you update as you want to.
Hope this helps!
If anyone's still looking for an answer to this.
I managed to solve the issue several weeks ago without creating the campaign via API, but actually updating it.
I used placeholders like [product_card id=123], 3 cards per block/row, all repeatable, which are wrapped in a class that I named product-card. In the MailChimp dashboard, you may still see the placeholders, but on preview and any form of preview like thumbnail, it will display correctly.
On the server, I crawl through the campaign's content, "detect" section names based on how they seemed to me in MailChimp and update each section with the content that I want.
PHP snippet below, some Yii2 stuff, but mostly plain PHP. I use $preview to display a preview of how the template would look, I know it's not visible in the function.
/**
* #param $id - Id of the campaign
* #param $s - Whether to save or just preview
*
* #return bool
*/
function changeContent($id, $s = false)
{
$mcCampaign = new McCampaign();
$mcCampaign::$itemId = $id;
$content = $this->api->get("/campaigns/{$id}/content");
if (!isset($content['html'])) return false;
$template = $content['html'];
$forgedDom = new \DOMDocument();
$forgedDom->loadHTML($template);
$mcSections = [];
$finder = new \DOMXPath($forgedDom);
$nodes = $finder->query('//td[contains(#class, "product-card")]');
// disabling this shit in order to avoid strict errors
libxml_use_internal_errors(true);
$mcEditId = 1;
$mcEditIndex = 0;
foreach ($nodes as $key => $node) {
/** #var \DOMElement $node */
$textContent = $node->textContent;
if (!preg_match("#\[product_card id=\d+\]#", $textContent)) continue;
$productId = filter_var($textContent, FILTER_SANITIZE_NUMBER_INT);
$node->textContent = false;
$product = Product::findOne($productId);
$productDetails = $product ? $this->renderPartial('/partials/_mc-product', [
'product' => $product
]) : 'Product not found.';
if ($key != 0) {
if ($key % 3 == 0) {
$mcEditId = 1;
$mcEditIndex++;
} else {
$mcEditId++;
}
}
$mcSections["repeat_1:{$mcEditIndex}:product_card_{$mcEditId}"] = $productDetails;
$fragment = $forgedDom->createDocumentFragment();
$fragment->appendXML($productDetails);
$node->appendChild($fragment);
}
libxml_use_internal_errors(false);
$preview = $forgedDom->saveHTML();
// just in case
/* $preview = str_replace(["\n", "\t", "\r"], "", $preview); */
if ($s) {
if (!empty($mcSections)) {
$res = $this->api->put("/campaigns/{$id}/content", [
'template' => [
'id' => *template_id_here*,
'sections' => $mcSections
],
]);
// forcing Mc to rebuild cache
$this->api->get("/campaigns/{$id}/content");
Yii::$app->session->setFlash('success', 'Done.');
return $this->redirect(['campaign/index']);
} else {
Yii::$app->session->setFlash('error', 'Something went wrong.');
}
}
}

PhpUnit form does not submit, no error

I'm doing a functional test in PhpUnit to test simple modal window functionality which has only one field and two buttons (i have tried various ways so the code may be not clean, I'm pasting just to show the idea):
$form = $crawler
// find all buttons with the text "Pridėti"
->filter('button:contains("Pridėti")')
->eq(0)
->form()// select the first button in the list
;
$form['appbundle_classinfo[name]'] = '5a';
$crawler = $client->submit($form);
//It doesn't even save to database
$container = self::$kernel->getContainer();
$em = $container->get('doctrine')->getManager();
$classinfo = $em->getRepository('AppBundle:ClassInfo');
//Echoes modal window
echo $client->getResponse()->getContent() ;die;
I also tried to var_dump the form, it shows that the value is added to form:
string(25) "appbundle_classinfo[name]"
["value":protected]=>
string(2) "5a"
So the form is not submitted. Can you help me find out why?
<?php
namespace AppBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PostTest extends WebTestCase
{
public function testShowPost()
{
//returns a Client, your browser use your web site
$client = static::createClient();
// The request() method (read more about the request method) returns a Crawler object which can be used to select elements in the response, click on links and submit forms.
$crawler = $client->request('GET', '/your/route/controller/action');
// select button by name or id
$form = $crawler->selectButton('Login')->submit();
//submit the form
$crawler->submit($form);
$this->assertGreaterThan(
0,
$crawler->filter('html:contains("hello world")')->count()
);
// get HTML content returned
dump($client->getResponse()->getContent());
}
http://symfony.com/doc/current/testing.html#functional-tests
hope that were useful, let me know.

pdftk error when using checkbox on pdf form

When I use checkbox on pdf form, pdftk gives the following error and does not create output pdf.
Unhandled Java Exception:
Unhandled Java Exception:
java.lang.NullPointerException
at gnu.gcj.runtime.NameFinder.lookup(libgcj.so.12)
at java.lang.Throwable.getStackTrace(libgcj.so.12)
at java.lang.Throwable.stackTraceString(libgcj.so.12)
at java.lang.Throwable.printStackTrace(libgcj.so.12)
at java.lang.Throwable.printStackTrace(libgcj.so.12)
Today I am having a similar problem with Checkbox. And I also saw java.lang.NullPointerException error. After investigation, I found that it is because my fillable checkbox is using custom glyphicon ('X') as checkmark instead of default styling.
So after reading this answer https://stackoverflow.com/a/29034948/11898471, It does workout by getting rid of my custom checkbox glyphicon. Without seeing your code I don't know exactly how you may do it your way, but my situation is to flatten a client uploaded PDF form with custom checkbox. What I did, is to extract all form data and re-fill the form so they get rid of all custom checkbox markup. Something like:
$pdf = new Pdf($uploadedFile->getRealPath(), ['command' => env('PDFTK_PATH')]);
/* Extract form field to remove custom markup field that cannot be filled. Eg: custom checkbox icon */
$pdf2 = new Pdf($uploadedFile->getRealPath(), ['command' => env('PDFTK_PATH')]);
$data = $pdf2->getDataFields();
$data = (array) $data;
$fill_data = [];
foreach ($data as $field) {
if (isset($field['FieldValue'])) {
$fill_data[$field['FieldName']] = $field['FieldValue'];
}
}
/* Update form field */
$pdf->fillForm($fill_data)
->flatten()
->saveAs(storage_path('app/'.$flattenedFilename));

Does Google Apps Script have something like getElementById?

I am gonna to use Google App Script to fetch the programme list from the website of radio station.
How can I select the specified elements in the webpage by specifying the id of the element?
Therefore, I can get the programs in the webpage.
Edit, Dec 2013: Google has deprecated the old Xml service, replacing it with XmlService. The script in this answer has been updated to use the new service. The new service requires standard-compliant XML & HTML, while the old one was forgiving of such problems as missing close-tags.
Have a look at the Tutorial: Parsing an XML Document. (As of Dec 2013, this tutorial is still on line, although the Xml service is deprecated.) Starting with that foundation, you can take advantage of the XML parsing in Script Services to navigate the page. Here's a small script operating on your example:
function getProgrammeList() {
txt = '<html> <body> <div> <div> <div id="here">hello world!!</div> </div> </div> </html>'
// Put the receieved xml response into XMLdocument format
var doc = Xml.parse(txt,true);
Logger.log(doc.html.body.div.div.div.id +" = "
+doc.html.body.div.div.div.Text ); /// here = hello world!!
debugger; // Pause in debugger - examine content of doc
}
To get the real page, start with this:
var url = 'http://blah.blah/whatever?querystring=foobar';
var txt = UrlFetchApp.fetch(url).getContentText();
....
If you look at the documentation for getElements you'll see that there is support for retrieving specific tags, for example "div". That finds direct children of a specific element, it doesn't explore the entire XML document. You should be able to write a function that traverses the document examining the id of each div element until it finds your programme list.
var programmeList = findDivById(doc,"here");
Edit - I couldn't help myself...
Here's a utility function that will do just that.
/**
* Find a <div> tag with the given id.
* <pre>
* Example: getDivById( html, 'tagVal' ) will find
*
* <div id="tagVal">
* </pre>
*
* #param {Element|Document}
* element XML document or element to start search at.
* #param {String} id HTML <div> id to find.
*
* #return {XmlElement} First matching element (in doc order) or null.
*/
function getDivById( element, id ) {
// Call utility function to do the work.
return getElementByVal( element, 'div', 'id', id );
}
/**
* !Now updated for XmlService!
*
* Traverse the given Xml Document or Element looking for a match.
* Note: 'class' is stripped during parsing and cannot be used for
* searching, I don't know why.
* <pre>
* Example: getElementByVal( body, 'input', 'value', 'Go' ); will find
*
* <input type="submit" name="btn" value="Go" id="btn" class="submit buttonGradient" />
* </pre>
*
* #param {Element|Document}
* element XML document or element to start search at.
* #param {String} elementType XML element type, e.g. 'div' for <div>
* #param {String} attr Attribute or Property to compare.
* #param {String} val Search value to locate
*
* #return {Element} First matching element (in doc order) or null.
*/
function getElementByVal( element, elementType, attr, val ) {
// Get all descendants, in document order
var descendants = element.getDescendants();
for (var i =0; i < descendants.length; i++) {
var elem = descendants[i];
var type = elem.getType();
// We'll only examine ELEMENTs
if (type == XmlService.ContentTypes.ELEMENT) {
var element = elem.asElement();
var htmlTag = element.getName();
if (htmlTag === elementType) {
if (val === element.getAttribute(attr).getValue()) {
return element;
}
}
}
}
// No matches in document
return null;
}
Applying this to your example, we get this:
function getProgrammeList() {
txt = '<html> <body> <div> <div> <div id="here">hello world!!</div> </div> </div> </html>'
// Get the receieved xml response into an XML document
var doc = XmlService.parse(txt);
var found = getDivById(doc.getElement(),'here');
Logger.log(found.getAttribute(attr).getValue()
+ " = "
+ found.getValue()); /// here = hello world!!
}
Note: See this answer for a practical example of the use of these utilities.
Someone has made an example here where the following custom functions are available for cut & paste use:
getElementById()
getElementsByClassName()
getElementsByTagName()
Then you can do something like this
function doGet() {
var html = UrlFetchApp.fetch('http://en.wikipedia.org/wiki/Document_Object_Model').getContentText();
var doc = XmlService.parse(html);
var html = doc.getRootElement();
var menu = getElementsByClassName(html, 'menu-classname')[0];
return menu;
}
I'm going to assume that you are referring to using UrlFetchApp's fetch() method. In which case, the answer is no, in the context of what you are thinking of.
If you look at the return type for fetch() in the documentation it returns HTTPResponse. There are a few methods for that, but most of them involve getting the returned data as a string. The good news is, you could still use any (well, most) of the traditional JS String methods documented here - so you could use search(), match(), etc. Depending on your project you could use those to find the data you are looking for in the response.

In Yii, how best to POST both search results selection & previously entered form model back to form controller?

To complete a Yii form field, users often need to search for a referenced model record (like searching for a friend's profile in a social app). I'm sure other Yii apps are doing this elegantly. But in my dirty approach, in the search results page, I use a CHtml::submitButton to POST two models back to the form containing:
the "found" record (a user id associated with one of the profiles from the search results)
the previously entered form field contents (relationship characterization fields)
Alternatively, the autocomplete widget works well, but doesn't do the detailed search that I need (e.g. search based on a partial name and city or state or other user profile content).
Alternatively, you'd think that within the search results view I might be able to modify the form member to contain the found record (new friend's user id) and just POST/submit the modified model from the search results page . But for that to work each of the search results in the list needs a unique user id populated in that form field, and I can't figure out how to duplicate the form model before modifying that one member server-side for each of the search results' "submit" or "select" buttons, and it just doesn't seem right to create all those form models.
So what seems to work is to submit two separate models using subforms (within the search results view) , with the submitButton POSTing a model and the extra parameter (user id) separately.
Is there a better way? ...to link to and from a search results page and a form field, retaining already-entered data and populating the searched for field with a selected record from the search results.
Here's controllers/SiteController:
public function actionBefriend() {
$model=new BefriendForm;
if(isset($_POST['BefriendForm'])) {
$model->attributes=$_POST['BefriendForm'];
if ($model->validate()) {
$model->createFriendship();
$this->redirect('Index'); }
else
$er=$model->getErrors(); }
if(isset($_POST['idfriend'])) {
$model->idfriend=$_POST['idfriend']; }
if(isset($model->idfriend)) {
$model->friend_name=Bio::model()->findByPk($model->idfriend)->name; }
$this->render('newFrienship', array('model' =>$model)); // newFriendship is the form view }
Here's controllers/Bio.php (Profile)
public function actionIndex() {
$criteria = new CDbCriteria();
$model=new BefriendForm;
if(isset($_GET['q']))
$q = $_GET['q'];
elseif (isset($_POST['BefriendForm'])) {
$model->attributes=$_POST['BefriendForm'];
$q = $model['friend_name']; }
if(isset($q)) {
$criteria->compare('name', $q, true, 'OR');
$criteria->compare('city', $q, true, 'OR');
$criteria->compare('state', $q, true, 'OR');
$criteria->compare('bio_text', $q, true, 'OR'); }
else
$q = '';
$dataProvider=new CActiveDataProvider('Bio', array('criteria'=>$criteria));
$this->render('index',array('dataProvider'=>$dataProvider, 'q'=>$q, 'model'=>$model )); }
Here's the start of views/site/newFriendship (form view)
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'newFriendship-BefriendForm',
'enableAjaxValidation'=>true,)); ?>
Here's the core of views/bio/index.php (search results index page):
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'viewData'=>array('model'=>$model) )); ?>
Here's the search result row in views/bio/_view.php that links back to BefriendForm (SiteController) that sends the id of the user to populate the friend field in the form (which gets a name from the id):
<form method="POST">
<input type="hidden" name="idfriend" value="<?php echo $data->idfriend ?>" />
// Here's that submit button that I can't get to send both the model
//and the idfriend back to the form to repopulate it
// without manually writing HTML to submit all the fields individually
// or creating 2 subforms to submit together with a signle submitButton.
<?php echo CHtml::submitButton('Befriend', array('submit' => array('site/Befriend'),'model'=$model);
</form>
The best alternative I can see is to imbibe the searcher widget within the form.
Turns out you can just replace the form field containing the primary key with the appropriate value before POSTing back to the NewFriendship form when the user clicks the "Select" or "Befriend" button. So only one model is posted back to the original form from the search results page.
Replace the section from views/bio/_view.php in the question with...
<?php
foreach($model->attributeNames() as $name)
if($name != 'friend_id')
echo CHtml::activeHiddenField($model,$name);
else
echo CHtml::activeHiddenField($model,$name,array('value'=>$data->getPrimaryKey()));
echo CHtml::submitButton('Befriend', array('submit' => array('site/Befriend')));
?>