How to run javascipt on chrome console - javascript

Hello I want to try to run all of these code on console chrome
var xxx = document.querySelectorAll('.balanceDetails-manageCurrencies.test_mcm-addCurrency')
xxx.forEach(btn => btn.click())
var twd = document.querySelectorAll('.shadow.multiCurrency-flag.multiCurrency-flag_TWD')
twd.forEach(btn => btn.click())
var addcurrency = document.querySelectorAll('.btn.vx_btn.mandate_lg-btn.test_mcm-addCurrencyButton')
addcurrency.forEach(btn => btn.click())
But it doesnt run everything, the process just stop when they excute line number 2
xxx.forEach(btn => btn.click())
Question is, how to run all of these code?

Try combining the arrays then running the click loop on the new array.
replace sel1 etc to your selectors
var xxx = [...document.querySelectorAll(sel1), ...document.querySelectorAll(sel2), ...document.querySelectorAll(sel3)]
xxx.forEach(el => el.click())

It's hard to tell what's going on in your case because we don't know what your selected DOM nodes are doing after being clicked. If you paste that entire code block into the chrome console I would expect it to execute all of it.
Two things off the top of my head could be happening.
The click handler setup to fire on one of those DOM nodes is throwing an error, but that error is being caught by a try/catch block and never bubbles up to the console. This would create the symptoms described.
The click handler for one of those DOM nodes is doing a form post, or something else that will cause the page to reload. Although, if this were the case you'd see the console clear. Not sure if you're seeing that or not.

Related

how can I see js garbage collector in action

in order to see garbage collection in action.
I want to write a script for chrome enviorment that declares a variable and prints it to the console. and then get collected by garbage collector (- so when I try to access it through dev tools I will get a reference error.
according to this link :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management ,
under Real-life example section -
if I use internet explore 7 and run the script:
var div;
window.onload = function() {
div = document.getElementById('myDivElement');
};
which is the example given in the link above but without the circular reference part.
if I will try to open ie7 console and try to reach div - I would get a reference error ( like I want ) because div would be garbage collected.
how can I do something similar now in chrome?

testcafe: Check for something after every click

I am testing a web app that renders some of its errors as HTML and gives no other indication that a problem occurred. After every click, I would like to see if a .error element exists.
I know I could manually add this after every click, but it's going to obfuscate my test and it will be easy to forget some instances. Is there a way to tell testcafe that a certain condition should make the test fail, even if I'm not explicitly checking for it?
I did something like this:
const scriptContent = `
window.addEventListener('click', function () {
if($('.error').length > 0) {
throw new Error($('.error').text());
}
});
`;
fixture`Main test`
.page`../../dist/index.html`.clientScripts(
{ content: scriptContent }
);
This injects a script onto the page I am testing. After every click, it uses jQuery to see if an error class exists. If it does, it throws an error on the page I'm testing. Testcafe reports the message of that error.
I'm hoping there's a better way.

If else statement in cypress is not working-what is wrong with my code?

I am starting out with cypress automation testing tool.
What I am trying to accomplish:
if the page contains "there are no results" then "mark as certified" button should not exist.
else if the page does not contain "there are no results" then click the select all button."mark as certified" button should exist.
here is the code that I have so far:
cy.get('body').then($body=> {
if ($body.find('There are no results')) {
//cy.get('.hZDZBR > .Typography__StyledTypography-sc-153d8g4-0');
cy.get('Mark as Certified').should('not.exist');
}
else {
cy.get('input[id="selectAll"]').click();
cy.get('.hZDZBR > .Typography__StyledTypography-sc-153d8g4-0').contains('Mark as Certified');
cy.get('.hZDZBR').click();
}
})
What cypress does right now is:
It enters the if loop, executes it and says "mark as certified" not found.
But my question is, if "there are no results" does not exist on the page, and it does not find it, then why does it even execute the if loop and go into the else loop?
So it is executing the if loop as though it finds "mark as certified" but I can see that that doesnt exist on the page.
Help is appreciated thanks!
So I updated the above code to:
cy.wait(5000);
if ($body.find('There are no results').length>0) {
//cy.get('.hZDZBR > .Typography__StyledTypography-sc-153d8g4-0');
cy.wait(5000);
cy.get('Mark as Certified').should('not.exist');
cy.get('input[id="selectAll"]').should('not.exist');
}
else if($body.find('There are no results').length==0) {
cy.get('input[id="selectAll"]').click();
cy.get('.hZDZBR > .Typography__StyledTypography-sc-153d8g4-0').contains('Mark as Certified');
cy.get('.hZDZBR').click();
}
})````
Now the control goes to else loop and does not execute:
````cy.get('Mark as Certified').should('not.exist');
cy.get('input[id="selectAll"]').should('not.exist');
What is wrong with my code?
Hi,
I again edited my code with:
const $el=Cypress.$('.Typography__StyledTypography-sc-153d8g4-0 jhYDmS TrainingQueueListstyles__EmptyListMessage-sc-19yfim3-1 ihRiqU');
if($el.length){
cy.log($el.length);
cy.get('Mark as Certified').should('not.exist');
} else{
cy.get('input[id="selectAll"]').click();
cy.get('.hZDZBR > .Typography__StyledTypography-sc-153d8g4-0').contains('Mark as Certified');
cy.get('.hZDZBR').click();
}
"There are no results" exists on the page but cypress still enters the else loop, I fail to understand why.
Also I am trying to log $el.length using cy.log($el.length) but I dont see it logging this value anywhere.
hi, this is the latest code edit I made ,but cypress shows this red mark under the = sign and says , expected "{", I am unable to understand why its expecting this here: Any ideas how I can resolve this?
javascript
const $el=Cypress.$([class='Typography__StyledTypography-sc-153d8g4-0.jhYDmS.TrainingQueueListstyles__EmptyListMessage-sc-19yfim3-1.ihRiqU']);
you can't use code synchronously after a cy.wait like that. You'll need to use a .then:
cy.wait(5000).then(()=>{
// if ($body.find(...
})
Using conditions like this in your code is not recommended. This will cause a test case to no longer be deterministic in the worst case.
I would recommend to write the two if/else cases in two different test cases and in there can clearly describe (maybe also with cypress networking) when it should come to which behaviour.
You can find more information here: https://docs.cypress.io/guides/core-concepts/conditional-testing#Client-side-rendering

Does console.log only execute when you use inspect on website?

I have a lot of console.log in my script, and I don't want them slowing my script down when I don't have the console open.
So my question is, do they execute when the console is closed? Because if they do I would have to comment them out, and then uncomment them every time I need to see them.
Yes, console.log() in your code will execute without the console open on the web page.
In all the browsers I know of, they do execute even while the console is closed - for example, try running the following code with the console closed, then open the console after 5 seconds, and you'll see that the console has indeed been populated with the text:
setInterval(() => {
console.log('log');
}, 1000);
The performance impact of console.log is next to nothing, but if you wanted an easy way to toggle between having console.log calls print to the console, and do nothing, you can define a console variable on the top level of your userscript that has a log function which does nothing, thus shadowing window.console.log inside the scope of your userscript:
const console = { log: () => void 0 };
console.log('foo');
To print to the console normally again, just comment out the const console... line (or use a boolean variable doPrintToConsole or something).
This will not prevent native page scripts from console.logging. (If you wanted to prevent native page scripts from doing so, overwrite window.console.log instead);

ExternalInterface return to AS3 not working consistently in IE

So, I'm having some trouble returning a value from an ExternalInterface call. I have a piece of code that looks like this:
var a:String = String(ExternalInterface.call("function() { var returnTest = 'test'; alert(returnTest); return returnTest;}"));
ExternalInterface.call("alert", a);
The first alert (in the anonymous function on line 1) is correct (obviously). However, the alert on line 2 is returning null 90% of the time in IE10. It works everytime in Firefox though.
To further explain the bit about working 90% of the time, it seems I can roll the dice again on whether or not it will work by adding or removing seemingly meaningless alerts. For example: let's say it's not working, I could add an alert and it will start working. Or, say it is working, I could add an alert for debugging, and it stops working, remove the alert, still doesn't work, add the alert back, and it starts working again. I know this isn't what's happening, but it's behaving as if a coin is flipped every time an alert is added or removed.
And this all only happens in IE, works perfectly every time in Firefox.
Thanks.
Edit:
The code I provided isn't the actual code that needs to work, but rather the code that I wrote to verify where the problem was. The actual situation is that there's a JavaScript property in the environment our Flash is running in that we need to know, but we don't have access to the HTML or JavaScript the SWF will be running in. The actual code I need to run looks more like this:
var pageNameFromJS:String = String(ExternalInterface.call("function() { var pageName = ServerObject.currentPage.name; alert(pageName); return pageName;}"));
ExternalInterface.call("alert", pageNameFromJS);
The alert in the first line is just to make sure that ServerObject.currentPage.name works, which it does. The alert in the second line is debug code that was added when we noticed that functions that require pageNameFromJS weren't working.
Really I dont know why you complicate things like this ;)
You can do it easier :
AS3 code:
ExternalInterface.addCallback("flash_function", flash_function);
function flash_function(from_js_param){
trace('param received from js : '+from_js_param)
}
ExternalInterface.call("js_function", to_js_param)
JS code:
function js_function(from_flash_param){
var to_flash_param = 'I received your '+from_flash_param;
(get_your_swf).flash_function(to_flash_param);
}

Categories

Resources