Google Script event undefined - events

I've built a Google Script which runs when my Spreadsheet is edited:
function onEdit(event)
{
Browser.msgBox(event.source);
general(event);
}
When my function is named onEdit(), the Browser.msgBox returns "Spreadsheet" (so the source is defined)
When my function is named othername(), the Browser.msgBox retunrs "undefined".
I execute the script with the same user, the script runs when my Spreadsheet is edited and i don't change anything else than the name of the function.
Thanks.

onEdit is an event in Google Script, so changing it's name will generate different behavior.
Hope this helps!

event.source stays undefined when you use another function than onEdit().
I've replaced event.source by event.range and now it works.
I don't understand why but it runs.

Related

Cypress: How to capture text from a selector on one page to use as text on another page

New cypress user here, I am aware that cypress does not handle variables like how testcafe and others do due to the asyn nature of it. Using the example given and what I could find I have this as an example:
cy.get('selector').invoke('text').as('text_needed')
cy.get('#text_needed')
const txtneeded = this.text_needed
cy.log(txtneeded)
This looks at a given selector, takes what it finds and uses it as text and set it as a variable usable at any time in the test and outputs it to the log. The plan is to use that text in a search filter in another page to find the item it references.
The problem is that it fails with Cannot read properties of undefined (reading 'text_needed')
Is this because the content of the selector is not assigned to text properly, the outer html is <a data-v-78d50a00="" data-v-3d3629a7="" href="#">PO90944</a> The PO90944 is what I want to capture.
Your help would be appreciated!
You cannot save an alias and access it via this.* in the same execution context (callback) because it's a synchronous operation and your alias is not yet resolved at this time.
This is a correct way to go:
cy.get('selector').invoke('text').as('text_needed')
cy.get('#text_needed').then(txtneeded => {
cy.log(txtneeded)
})
First, make sure to define it as traditional function, not as an arrow function as this context doesn't work as you'd expect there, more info here.
Next, typically in a single test you should use .then() callback to perform additional actions on elements, and use aliases when you need to share context between hooks or different tests, so please consider the following:
// using aliases together with this within the single test won't work
cy.get(<selector>).invoke('text').as('text_needed')
cy.get('#text_needed').should('contain', 'PO90944') // works fine
cy.log(this.text_needed) // undefined
// this will work as expected
cy.get(<selector>).invoke('text').then(($el) => {
cy.wrap($el).should('contain', 'PO90944'); // works fine
cy.log($el) // works fine
});
Setting alias in beforeEach hook for example, would let you access this.text_needed in your tests without problems.
Everything nicely explained here.
Edit based on comments:
it('Some test', function() {
cy.visit('www.example.com');
cy.get('h1').invoke('text').as('someVar');
});
it('Some other test', function() {
cy.visit('www.example.com');
cy.log('I expect "Example Domain" here: ' + this.someVar);
});
And here's the output from cypress runner:

Cypress visit command produces error when url is a variable

I am trying to run cy.visit() which will take a variable instead of a hardcoded value as its parameter.
In my env.json file I have an environment variable called url, which I've given a value.
{
"env": {
"url": "https://somedomain.com",
}
}
In my spec file, I am trying to pass this variable when calling cy.visit(), like so:
cy.visit(Cypress.env('url'));
When running the test, I am greeted by this error
CypressError: `cy.visit()` must be called with a `url` or an `options` object containing a `url` as its 1st argument
I've searched all over internet for a solution, but it seems like I'm the only person in the world with this exact problem.
Someone knows what's wrong? Thanks!
Edit:
As pavelsaman said, my custom file was not loading. The reason behind this was I had module.exports = (on, config... defined twice in the index.js file. Maybe it will help someone in the future!
In my env.json file I have an environment variable called url, which I've given a value.
Cypress by default looks for cypress.json file, so it seems your env.json is not loaded.
You can check this default behaviour in the docs: https://docs.cypress.io/api/cypress-api/env.html and here: https://docs.cypress.io/guides/guides/environment-variables.html#Option-2-cypress-env-json
I put successfully those env varaibles in cypress.env.json, and access it like that. You probably missnamed the file or you need to the following - since it is a sub object:
cy.visit(Cypress.env('env').url);

Firefox: run functions from Javascript through an extension

At the present I'm pasting a Javascript into the console of FF and I'm calling the functions from the console:
function fill (i){
if(i==1){
SINGLE_START();
}
else if(i==2){
DUAL_START();
}
else if(i==3){
INTEGRATED_START();
}
else{
alert("=======================\n Tool Filler\n=======================\n\n1 or 2");
}
}
It is used to scrape the content of the website and e.g. create a file or generate an email from certain parts of the website, e.g.:
function SINGLE_START(){
//Focus:
let d = $(document).activeElement.contentDocument.activeElement.contentDocument.activeElement.contentDocument;
etc.
I thougt, there could be a way to use it through an extension and so I installed Tampermonkey and saved the script as userscript within the extension. But than I have a problem that I'm not able to call the desired function from the script as I need it, not just start the script as the website loads.
Does anyone has an idea how to call the functions one by one from within Tampermonkey (or Greasemonkey), or any other extension?
Thanks in advance!
This is because Tampermonkey scripts run in isolated context. There are two kinds:
1. No special privilegies
If you're not using any special GM functions that are unlocked by #grant GM_doThisAndThat and instead you use #grant none, then what internally happens is something like this:
function TheScript() {
// Here is your script that you added to tampermonkey
}
TheScript();
This if you have a function there, it is only available in the script. You need to explicitly expose it to window context:
function fill (i){
... code here ...
}
window.myFill = fill;
Then in console you write myFill(42) and it will execute.
I would recommend that you avoid assigning fill to window since it's a name that could likely conflict with something, but that's up to you.
2. Special privilegies
If you're using some of the GM features, you need to add #grant unsafeWindow and then assign to unsafeWindow variable. Be careful what you expose like this, you don't want to allow the website to access any of the GM_function features as they could access your private data on other websites and your computer!

Cannot call loaded script object handler twice. Why?

I am thoroughly stumped by this problem. I have some standard functions that I saved in a compiled AppleScript file, let's call it functions.scpt. In another script, I use load script to load a script object to functions.scpt:
set funcs to load script "path/to/functions.scpt
I have one handler in functions.scpt called icon(). For whatever reason, if I try to call this function more than once in my other script, I get an error. Here's some quick example code:
--called once
funcs's icon()
--called twice
funcs' icon()
Whenever I do this, I get the following error: error "«script» doesn’t understand the “icon” message." number -1708 from «script» to «class icon».
Why can't I call the same function from a loaded script object twice? How can I fix this? I need to call it multiple times.
Thank you to #CRGreen and #jweaks. I actually just found the answer on this thread: https://stackoverflow.com/a/13256042/2884386
I had set a variable icon within the handler, which was overwriting the handler. So when the other script tried to access it the second time, it wasn't pointing to the handler, but to the variable. I rewrote the internal variables, and that fixed it.
So, this is (as an example) correct:
on icon()
set _icon to "path/to/icon.png"
return POSIX file _icon
end icon
Whereas this is incorrect:
on icon()
set icon to "path/to/icon.png
return POSIX file icon
end icon

Parse.User.current(), globals etc

I am new to Express and Parse. I am trying to make a simple modification to the Anyimg cloud tutorial. I would like to display the default home page only if the user is authenticated. If I use Parse.User.authenticated(), I get:
The error was TypeError: Object function (){e.apply(this,arguments)} has no method 'authenticated'
at app.js:33:18
at main.js:1:1
Parse.User.current() compiles with no error, but the code does not work. What am I missing? I have tried literally over 60 different options over the last 3 days with no success.
I also tried to declare a GLOBAL from within user.js and that did not work. I did a require cloud/user from within app.js and that did not help at all.
I hope someone can help. It appears that Parse tutorials (outside parse.com) from about a year ago break.
On the other hand, I wanted to display the login page (not default) upon logout. I got it done in less than 10 minutes.
The correct way to do this would be:
var currentUser = Parse.User.current();
if (currentUser) {
// logged in
} else {
// not logged in
}
Parse.User.current() returns the current user, or undefined if not logged in.
as seen here: https://parse.com/docs/js_guide#users-current
As mentioned above, I already had code that was similar to Fosco's. However, the var statement was not in the scope of the function as I was trying out o. I just moved it in and it all works. Thanks to everyone for looking into this.

Resources