Protractor e2e: extracting a string and assigning it to a var - javascript

I'm using Protractor and TypeScript to drive e2e regression
element
<strong _ngcontent-jwu-49="" data-protractor="StreamId">11107</strong>'
case
I need to extract ID from an element and construct a URL out of a string and ID
no matter what I do, a promise is not getting resolved
here is what I've tried
function getStreamId() {
//browser.ignoreSynchronization = true;
var streamIdElement = $$("[data-protractor='StreamId']")
var streamId = streamIdElement.getText().then(
function(text){
browser.get('https://URL/page/'+text);
console.log(streamId)
})
still a promise
and
browser.executeScript("var text = document.querySelectorAll('[data-
protractor='StreamId']').innerHtml").then(function(text)
{console.log(text);});
fails WebDriverError: unknown error: Runtime.evaluate threw exception:
SyntaxError: missing ) after argument list
My question is 2 part one:
what is the best practice in protractor if a value needs to be extracted and interacted with later
is it even possible to resolve a promise into a string (not to read it in the log with .then(function(text){console.log(text)}), so I can assign it to a variable and use it later on)

Your first attempt is actually quite close to the end result. You've located the element, called the getText() and resolved the promise.
Make the function return a promise and resolve once you need an actual value:
function getStreamId() {
var streamIdElement = $$("[data-protractor='StreamId']")
return streamIdElement.getText();
}
getStreamId().then(function(streamId) {
browser.get('https://URL/page/'+streamId);
console.log(streamId);
});

Related

Return text from URL in GTM Custom JS variable

I need to capture text from the file saved under URL (ex. https://fiddle.jshell.net/robots.txt) And return it under the function.
What is on my mind:
function(){
var url = 'https://fiddle.jshell.net/robots.txt'
var storedText;
fetch(url)
.then(function(response) {
response.text().then(function(text) {
storedText = text;
done();
});
});
function done() {
return storedText;
}
});
Unfortunately this function is not working. I received an error: Error at line 2, character 2: Parse error. primary expression expected Type: JavaScript compiler error
This should be used as a Custom JavasSript Variable in Google Tag Manager
Also this How do I return the response from an asynchronous call? is not explaining my issue and all solutions were checked by me, nothing worked due Parse error or console error.
I have to use GTM custom JavaScrip Variable, I can use anything else.
Simpler JS than better.
Function done() returns storedText value to a point of call, i. e. it works but returns nothing.
I would not recommend using an external call within a custom Custom JavasSript Variable in GTM:
You can not really control how often a Custom JavasSript Variable is executed in GTM. An it is executed a lot.
Just do the test and run a console log from a custom javascript variable.

Getting a new id for a firestore doc to add it to the document itself

According to the documentation here, it should be possible get an id for a not-yet-created firestore document, add it the object to be saved, and then persist it like this:
// Add a new document with a generated id.
var newCityRef = db.collection("cities").doc();
// later...
newCityRef.set(data);
In my application, I follow this pattern with the following code:
async createNewProject(projectObject : Project) : Promise<boolean> {
let doc = this.firestore.collection("projects").doc();
projectObject.id = doc.ref.id;
try {
await doc.set(projectObject);
} catch(err) {
console.error(err);
return false;
}
return true;
}
When it runs though, i get an error in the console:
FirebaseError: Function CollectionReference.doc() requires its first argument to be of type non-empty string, but it was: undefined
Can anybody shed any light? I've seen other folks on her referencing this method (using the doc method with no parameters as the solution to the problem, yet others are seeing this error. Is this some kind of API on the Angular implementation of the API, or a problem at the firebase side of things (or did I do something wrong?)
The doc() method returns a DocumentReference, which does not have a ref property but has an id one. Therefore you need to do:
projectObject.id = doc.id;
instead of
projectObject.id = doc.ref.id;

Callback function in Axios returning error (using Vue, no jQuery)

I am using Axios with the Vue Javascript framework to loop through an array. I'm trying to set each element in the array to the result of the API call for that element. My functions were not running in order, so I tried using a callback function but it returns an error.
I have looked through other answers(which is why I'm trying to use a callback function) but I still can't get my code to work.
I have a CodePen that shows my problem:
https://codepen.io/thwaawaa/pen/VoEbqR
The CodePen console gives me this error:
[object Error] {
column: 19,
line: 34,
sourceURL: “some long link”
}
here is a working fork with no errors : Codepen
the issue you had is that the this.word inside your then callback was not defined because you didn't bind the vue this inside it ...also you handled the response by using 2 thens so the first one was handling the response but it wasn't returning anything to the second one that's why response was undefined to fix this just use one then or return a value from one and chain it to the other then after it.... i would recommend removing the first one on your axi method and add the code inside it on the other then also like we said you have to bind then and you can do that by replacing :
then(function(response){
var r = response.data[0].phonetic;
this.word[x] = r;
})
with this :
then((response) => {
var r = response.data[0].phonetic;
this.word[x] = r;
})
the arrow function will bind the this implicitly so your function now knows what this means and it won't trigger the catch

Cannot read property of undefined after Promise.promisify

let nasPath = "";
return getFamInfo(args.familyID)
.then(function (famInfo) {
nasPath = //some code involving famInfo here
return getSFTPConnection(config.nasSettings);
}).then(function (sftp) {
const fastPutProm = Promise.promisify(sftp.fastPut);
return fastPutProm(config.jpgDirectory, nasPath, {});
});
If I put a breakpoint after const fastPutProm = Promise.promisify(sftp.fastPut);, fastPutProm is a function with three arguments. But when I try to run this code, I get a TypeError: Cannot read property 'fastPut' of undefined error. What am I doing wrong here?
That error means that your sftp value is undefined so when you try to pass sftp.fastPut to the promisify() method, it generates an error because you're trying to reference undefined.fastPut which is a TypeError.
So, the solution is to back up a few steps and figure out why sftp doesn't have the desired value in it.
Another possibility is that the error is coming from inside the module and it's because the implementation of sftp.fastPut is referencing this which it expects to be sftp. Your method of promisifying is not preserving the value of this. You can fix that by changing your code to:
const fastPutProm = Promise.promisify(sftp.fastPut, {context: sftp});

chrome.management.get not working

function Notify(header,content,image){
var note = webkitNotifications.createNotification(image||"",header,content);
note.show();
return note;}
var extensions = ["pbjhaapnigfhipfahcfkeakpcgkmnklc"];
function CheckReload(){
for(var CN=0;CN<extensions.length;CN++){
var id = extensions[CN];
var ex = chrome.management.get(id);
console.log("Checking",ex,"-",id);
if(!ex.enabled){
Notify("Extension reloaded!",ex.name+" was found crashed, and reloaded!");
chrome.management.setEnabled(id,true);
}
}
}
setInterval(CheckReload,1000);
Ok, so what I was expecting was for this to check the extensions in the "extensions" array, and if they weren't enabled it would create a notification saying that it wasn't, and then enable it.
However, chrome.management.get(id) seems to be returning undefined.
I expected an output like:
Checking Object - [id]
Instead, what I got was:
Checking undefined - pbjhaapnigfhipfahcfkeakpcgkmnklc
Uncaught TypeError: Cannot read property 'enabled' of undefined
How can I fix this?
Most of the methods provided by chrome don't return a value, instead they take a callback function as a parameter, and call that function with the wanted result.
You should replace your code by
chrome.management.get(id, function(ex) {
console.log("Checking",ex,"-",id);
if(!ex.enabled){
Notify("Extension reloaded!",ex.name+" was found crashed, and reloaded!");
chrome.management.setEnabled(id,true);
}
});
See http://developer.chrome.com/extensions/management.html#method-get for details.
If you are running your code from an extensions, make sure that your extension have permissions to management.

Categories

Resources