refactor capybara javascript dropzone test - javascript

I'm trying to call a dropzone capybara test multiple times. However, when I call it a second time, the ID has already been used. I'm trying to randomize the ID so it can run multiple times.
def drop_in_dropzone(file_path)
page.execute_script <<-JS
fakeFileInput = window.$('<input/>').attr(
{id: 'fakeFileInput', type:'file'}
).appendTo('body');
JS
attach_file("fakeFileInput", file_path)
page.execute_script("var fileList = [fakeFileInput.get(0).files[0]]")
page.execute_script <<-JS
var e = jQuery.Event('drop', { dataTransfer : { files : [fakeFileInput.get(0).files[0]] } });
$('.dropzone')[0].dropzone.listeners[0].events.drop(e);
JS
end
Error when calling it 2nd time.
Failure/Error: attach_file("fakeFileInput", file_path)
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching file field "fakeFileInput"

You can definitely just generate a random id number for the input but it might be easier to just only create the fakeFileInput it if doesn't already exist. This would only work if you don't use the input for any other purposes than in this method, but it seems like that's what you're doing.
page.execute_script <<-JS
fakeFileInput = fakeFileInput || window.$('<input/>').attr(
{id: '#{fake_input_id}', type:'file'}
).appendTo('body');
JS
If it did already exist it wouldn't get created again and it would just get reused.

Related

Difficulty understanding code which retrieves a filename from a file control

I have this code in a project. I understand that it outputs the file from a file input control. The issue is that I don't understand the working of var filename = e.target.files[0].name. Please help me out, if you can.
$(document).ready(function() {
$('input[type="file"]').change(function(e) {
var fileName = e.target.files[0].name;
$('#choose').html(fileName);
});
});
Breaking the line e.target.files[0].name down in to sections:
e is the Event object passed to the jQuery event handler function as an argument.
target is the property which contains a reference to the element which raised the event
files is a collection of the files selected within the input type="file" control. If the control has the multiple attribute set on it, it's possible for there to be more than 1 file selected, in which case you would need a loop.
[0] retrieves only the first selected file from the files collection - in the same manner as you access an array by index.
name gets the filename of that file.

How to update a user's tags ? using Quickblox javascript SDK

How can i update a user' tags using Quickblox javascript SDK ?
i tried using the following parameter names :
user_tags
tags
using
var params = {
user_tags: ["testing"]
};
Please don't answer me with "you can use custom objects, etc.." i'm already using them for something else.
I want to use tags, and nothing else
As stated in the table on this page: http://quickblox.com/developers/Users#Update_API_User_by_identifier you should use tag_list key which accepts a comma separated list of tags.
QB.users.update(user_id, { tag_list: "tag1,tag2" }, callback);
Unfortunately QB docs aren't the best in the world...

How to use brower.get() in protractor

I was using Protractor to test dropdown selection. The code is following:
var productCountry = element(by.model('main.productCountry1'));
productCountry.click();
var country = element.all(by.repeater('country in countries'));
country.get(5).click();
browser.sleep(500);
The error is:
Stack:
ElementNotVisibleError: element not visible
(Session info: chrome=44.0.2403.81)`
I used code to see the data inside array [names]:
var names =element.all(by.repeater('country in countries').column('country.name'));
names.getText().then(function(txt){
console.log(txt);
});
And the result shows the array looks like this:
['','', '', '', '', .....,'','Kenya','Kiribati',....,'','','','']
Only can get a part of elements, other elements are all empty. And I run several times,each time the empty parts are different.
I used the first line code
browser.get('dist/#');
browser.get('app/index.html');
to get into the webpage. If I run the above second line code, there is a 'Not Found' shown on the website,and the error is:
Stack:
Error: Failed: Error while running testForAngular: asynchronous script timeout: result was not received in 11 seconds
(Session info: chrome=44.0.2403.81)
Does this caused the element empty? The example I saw online always used the second line browser.get(). Is there any one used the first method?
I found If changed the data in $scope.country in scr/app/tab/main.controller, when I run gulp serve, the options in selection changed, but when I run protractor and get from the dist/#, the options are not changed. I guess it maybe caused the element empty. And I used yeoman to generate the files.
For the bower.get():
I used the code like this "browser.get ('/'); " to instead of the two lines of code shown in the question. It will now show the index page.
For drop-down selection test:
There are two things causing this error.
var productCountry = element(by.id('countryName'));
var country1 = element.all(by.repeater('country1 in countries'));
productCountry.click();
browser.sleep(500);
var a = country1.get(2);
a.click();
browser.sleep(500);
expect(a.getAttribute('value')).toEqual('Indonesia');
Here is my code. One reason is after doing productCountry.click(), we need to let the browser sleep for a while to wait for the renderings to take effect.
The other reason is I used two of the same repeater in my HTML file, and when I do the e2e test, the system can't distinguish which repeater I want attached to the element. So I changed the code from:
var names =element.all(by.repeater('country in countries').column('country.name'));
to
var country1 = element.all(by.repeater('country1 in countries'));
var country2 = element.all(by.repeater('country2 in countries'));
And finally I can test two drop-down selections.

Return arrays from meteor mongo

ajduke:bootstrap-tagsinput
I am using the above package to create a tags system. I have used <select multiple> from the True Input Value from the link above and have inserted each tags as Strings within the tag array.
This is what my db looks like.
Posts
tags //Array
[0] : sometag //String
[1] : sometag //String
[2] : sometag //String
//and so forth..
So my question is...
I want to return all the tags within a post to the following format, so that I can reuse it to show the tags when my users try to edit their posts.
Wanted Format
['sometag', 'sometag', 'sometag', and so forth]
Edit1
this is what I've done so far.
Post_Edit.js
Template.postEdit.rendered = function() {
myTags = Posts.findOne({_id: this._id}).tags.fetch(); //AAA
$('.tagsinput').tagsinput('add', myTags); //From above link API
}
I've tried other methods for //AAA line but I am having no luck.I've tried things such as Posts.findOne.map... Maybe my english comprehension is not up to par but the documentations in meteor did not help me understand any better.
Edit 2
Posts.findOne({_id: "ziZw3wLaxFyz3DYP4"}).tags
I've tried putting this in the browser console and got the array in the format that I wanted.
But the problem is, it won't work in my app when I use it.
When I use Posts.findOne({_id: this._id}).tags in my postEdit.rendered, I get this browser console error. Cannot read property 'tags' of undefined
Post_Edit.js
Template.postEdit.rendered = function() {
myTags = Posts.findOne({_id: this._id}).tags;
$('.tagsinput').tagsinput('add', myTags); //From above link API
}
What I don't understand is why is it working in browser console but not my Template.postEdit.rendered?
You are correct that your variable myTags is undefined when Template.postEdit.rendered is called because the Posts database has not finished loading when your function is called.
myTags = Posts.findOne({_id: this._id}).tags.fetch(); //AAA
Solution
There are multiple strategies including:
Updating DOM after subscription completes
http://docs.meteor.com/#/full/Blaze-TemplateInstance-subscribe
Re-run the function when Collection changes
http://docs.meteor.com/#/full/tracker_autorun
Template.postEdit.rendered = function() {
Tracker.autorun(function () { ** NEW LINE **
myTags = Posts.findOne({_id: this._id}).tags;
$('.tagsinput').tagsinput('add', myTags);
} ** NEW LINE **
}
I've approached a completely different way to figure this out.
But I'd still like to know why my method did not work...
post_edit.js
Template.postEdit.helpers({
tags: function(){
Posts.findOne({_id:this._id}).tags;
}
});
post_edit.html
<select class=tagsinput id="tagsinput" data-role="tagsinput">
{{#each}}
<option value="{{this}}">{{this}}</option>
{{/each}}

Couchbase Java API and javascript view not returning value for a specific Key

I am using couchbase API in java
View view = client.getView("dev_1", "view1");
Query query = new Query();
query.setIncludeDocs(true);
query.setKey(this.Key);
ViewResponse res=client.query(view, query);
for(ViewRow row: res)
{
// Print out some infos about the document
a=a+" "+row.getKey()+" : "+row.getValue()+"<br/>";
}
return a;
and the java script view in couchbase
function (doc,meta) {
emit(meta.id,doc);
}
So, when I remove the statement query.setkey(this.Key) it works returns me all the tables, what am I missing here .. How can I change the function to refect only the table name mentioned in the key
Change the map function like this:
function (doc,meta) {
emit(doc.table,null);
}
it is good practice not to emit the entire document like:
emit(doc.table, doc)
NB: This is surprisingly important:
i have tried using setKey("key") so many times from Java projects and setting the key using CouchBase Console 3.0.1's Filter Result dialog, but nothing get returned.
One day, i used setInclusiveEnd and it worked. i checked the setInclusiveEnd checkbox in CouchBase Console 3.0.1's Filter Result dialog and i got json output.
query.setKey("whatEverKey");
query.setInclusiveEnd(true);
i hope this will be helpful to others having the same issue. if anyone finds another way out, please feel free to add a comment about it.
i don't know why their documentation does not specify this.
EXTRA
If your json is derived from an entity class in a Java Project, make sure to include an if statement to test the json field for the entity class name to enclose you emit statement. This will avoid the key being emitted as null:
if(doc._class == "path.to.Entity") {
emit(doc.table, null);
}

Categories

Resources