Convert Document element to Protractor element - protractor

Is it possible to convert my jquery element to protractor element? For example:
browser.executeScript(() => {
var ele = $(".datatable-row-wrapper .hdt-d:not('not-active')").get(0);
return ele;
}).then(ele => {
**//// this throwing error**
element(ele).click();
});

One option would be to instantiate an ElementFinder instance (not tested):
var elementLib = require('protractor/built/element');
var ElementFinder = elementLib.ElementFinder;
var elementFinder = ElementFinder.fromWebElement_(browser, ele, by.css(".datatable-row-wrapper .hdt-d:not('not-active')"));
elementFinder.click();
Even though I've never encountered the need to actually do so.
The other way around - passing an ElementFinder into a script is much easier:
var elm = $(".datatable-row-wrapper .hdt-d:not('not-active')");
browser.executeScript("arguments[0].click();", elm.getWebElement());

Related

Access datasource in eventhandlers

The below code is generating errors:
Initialize:
var firstC = ["AUD","ZAR"];
var secondC= ["AUD","BRL","CAD","USD"];
function colorG(item, col, row){
var currency = firstC[col] + "-"+ secondC[row];
if(verifyCurrency(currency)==true)
item.getStyle().backgroundColor="green";
}
function verifyCurrency(currency)
{
if(this.getRowData().getExpressionValue("row[digital]").indexOf(currency)!=-1)
return true;
else return false;
}
cell:
colorG(this,1,0);
In which phase do I have to place verifyCurrency so that it will work?
Your assumption about "this" in your verifyCurrency function is wrong. I don't know Javascript good enough to tell you more about this, but I think that "this" is always defined inside a function. But it is not pointing to your item instance!
To fix this (no pun intended), pass the item as an argument to your verifyColor function.

SAPUI5 this.getView in attachRequestCompleted

I am loading data to my model and have a attachRequestCompleted attached. In there I want to set a value to one field, but it returns this.getView is not a function. This whole thing is inside a an interval:
My code looks like this:
var intervalId = setInterval(this.readRfid.bind(this), 3000);
readRfid: function() {
var oRfidModel = new sap.ui.model.xml.XMLModel();
oRfidModel.loadData("http://localhost/xxxxxxx");
oRfidModel.attachRequestCompleted(function() {
var reader = oRfidModel.oData.children["0"].children["0"].innerHTML;
this.getView().byId("objHdr_det_id1").setNumberUnit(reader);
});
Can I not use this.getView in the function? How can I make it work?
Thanks,
Tim
The this instance is not pointing to the Controller.
You probably need to bind a context to the callback function you've passed to the attachRequestCompleted.
As you can see here you can pass an oListener to this method.
I guess that something like:
var oRfidModel = new sap.ui.model.xml.XMLModel();
oRfidModel.loadData("http://localhost/xxxxxxx");
oRfidModel.attachRequestCompleted(function() {
var reader = oRfidModel.oData.children["0"].children["0"].innerHTML;
this.getView().byId("objHdr_det_id1").setNumberUnit(reader);
}, this);
Would work.
If this does not help you you can bind a context to the callback.

I am getting 0 values for my array when I try to add DOM elements from a webpage, how to add values to lists in .each function?

On console it presents just empty array and 0's for all the elements on the Business Insider page with the specific selectors. How can I add each number (which is views on their site) as a list or string of numbers and then .length on the variable and push it to my html interface?
var request = require('request');
var cheerio = require('cheerio');
var bilist = new Array();
//var x = document.getElementById('bi-stats').innerHTML;
var url = 'http://www.businessinsider.com/moneygame';
request(url, function(err,resp,body)
{
if (err)
throw err;
$ = cheerio.load(body); //create the dom object string
$('span.hot').each(function() {
$(this).text().append(bilist);
console.log(bilist);
console.log(bilist.length);
});
});
function start() {
window.addEventListener('load', bi_list(), false);
}
So jQuery has a different syntax for the 'for' iterator.
The correct code is shown below which appends the text element that contains # of views of the site Business Insider.
$('span.hot').each(function(i, elem) {
bilist[i] = $(this).text();
});

Issues in store-fed dijit/form/Select by setting preselected value in dojo 1.8

There is another issue when I try to set the preselected (or any) value of my store-fed dijit/form/Select widget.
The markup code is:
<div data-dojo-type="dijit/form/Select" jsId="editOptionsDialog_select" id="editOptionsDialog_select"></div>
and the js:
function editOptionsDialog_fetchData(cId, fieldName, vId) {
var store;
var def;
var return_def = new Deferred();
store = new degreeStore();
def = store.getJsonData();
def.then(function(data) {
store.data = data.items;
editOptionsDialog_select.setStore(new ObjectStore({
objectStore : store
}));
editOptionsDialog_select.value = vId;
editOptionsDialog_select.startup();
editOptionsDialog_select.set('value', 5);
console.info(editOptionsDialog_select);
// here, firebug still shows value = 1
return_def.resolve();
});
return return_def;
}
thx in advance
Greetings
Finally found a solution to the problem:
Since the selection-element DOES NOT support numerical indexes, the indexes had to be casted to strings. With that, a editOptionsDialog_select.set('value', vId.toString()) has finally worked!
Be sure to feed your store by casted numerical ids or then textual-keys by default -> (String)id where id is an integer.
Greetings

using jQuery selector with a dom element

I have a javascript function that receives a dom element as a parameter. In this function I am trying to get to the closest ancestor 'form'. I wanted to use:
function submit_form(obj)
{
var form1 = $(obj).closest("form");
alert (form1.id);
}
here obj is a dom element of submit type. I just don't seem to get it working.
Could someone please help?
You want
function submit_form(obj)
{
var form1 = $(obj).closest("form")[0];
alert (form1.id);
}
The result of jQuery selection/traversal functions always is an array, possibly with one element only, but still an array. Index into the array to get actual DOM elements.
However, you can do this without jQuery as long as obj is an input/select/textarea element.
function submit_form(obj)
{
var form1 = obj.form;
alert (form1.id);
}
For the sake of completeness, you could also stay within jQuery and do
function submit_form(obj)
{
var $form1 = $(obj).closest("form");
alert ( $form1.attr("id") );
}
function closest(obj, tagName) {
// Go up in the tree until you find the ancestor form
while (obj.parent !== null) {
if (obj.nodeType === 1) {
parent = obj.parentNode;
if (parent.tagName === tagName) {
return parent;
}
}
}
// If no form exists return null
if (obj.tagName !== tagName) {
return null;
}
}
Use it in this way
var closestForm = closest(obj, 'FORM')
form1 would be a jQuery object, so you can either use .attr("id"), or access the DOM element itself using [0].id:
var form1 = $(obj).closest("form");
alert(form1.attr("id"));
var form2 = $(obj).closest("form")[0];
alert(form2.id);