-
Notifications
You must be signed in to change notification settings - Fork 0
JS (web)
alex [dot] kramer [at] g_m_a_i_l [dot] com edited this page Dec 3, 2021
·
44 revisions
await new Promise(resolve => setTimeout(resolve, 500)); // sleep for 500 ms
let x = 7;
const foo = (val) => {
return () => { console.log(val); };
}
new_func = foo(x);
new_func(); // => 7
x = 42;
new_func(); // STILL => 7!!!!
Do this instead of selenium_driver.browser.close
so the window doesn't actually close.
let event = document.createEvent('BeforeUnloadEvent');
event.initEvent('beforeunload', true, true);
let userPromptedBeforeLeavingPage = !dispatchEvent(event);
new XMLSerializer().serializeToString(document);
import * as ModuleName from "path/moduleName";
let resolver;
const moduleNameSpy = jest.spyOn(
ModuleName,
"default"
).mockReturnValue(
new Promise(
(resolve, reject) => {
resolver = resolve;
}
)
);
expect(moduleNameSpy).toHaveBeenCalledWith(
arg1,
expect.any()
);
await act(
async () => {
resolver(someReturnValue);
}
);
import { v4 } from 'uuid';
jest.mock('uuid', () => ({
v4: jest.fn()
}));
beforeEach(() => {
v4.mockReset();
});
describe("Mocking uuid", () => {
it("mock uuid twice (jk three times)", () => {
v4
.mockReturnValueOnce("FIRST TIME")
.mockReturnValueOnce("SECOND TIME")
.mockReturnValueOnce("THIRD TIME")
const result = [v4(), v4()];
expect(result).toEqual(["FIRST TIME", "SECOND TIME"]);
});
it("DO IT AGAIN", () => {
v4.mockReturnValueOnce("BLURST TIME")
expect(v4()).toEqual("BLURST TIME");
})
});
$(`table.SOME_CLASS tr td:nth-child(${INDEX_OF_COLUMN})`).each(function(idx, e) {console.log(e.innerText)})
// Uses a basic for loop instead of $.each() in order to delay clicks by awaiting promises
for (index = 0; index < 25; index++) {
console.log("Erasing " + index);
$($("a.edit-usertext")[index]).trigger("click");
$($(".usertext-edit textarea")[index]).text("🤡");
$($(".usertext-buttons button.save")[index]).click();
console.log("Erased " + index);
await new Promise(resolve => setTimeout(resolve, 800));
console.log("Deleting " + index);
$($(".del-button .togglebutton")[0]).trigger("click");
$($(".del-button .yes")[0]).trigger("click");
console.log("Deleted " + index);
await new Promise(resolve => setTimeout(resolve, 800));
}
location.reload();
alert("PnL: " + $("iframe#main").contents().find(".posChange, .negChange").map( function() { return this.innerText.replace(',', ''); } ).toArray().reduce(function(tot, num) { return parseFloat(tot) + parseFloat(num); }))
// Uses a basic for loop instead of $.each() in order to delay clicks by awaiting timeouts
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
currentVariableCountString = $($("#dcs .number")[0]).text().trim() || "0";
currentVariableCount = parseInt(currentVariableCountString);
buttons = $(".variablesList .checkbox-column .add_variable:visible");
buttonCount = buttons.size();
targetVariableCount = buttonCount + currentVariableCount;
console.log("\n\n\n=====> CURRENT: " + currentVariableCount + ", NEW: " + buttonCount + ", SUM: " + targetVariableCount);
for (index = 0; index < buttonCount; index++) {
$(buttons[index]).trigger("click");
await sleep(200);
}
noUpdateWaitCount = 0;
while (noUpdateWaitCount < 8) {
newVariableCountString = $($("#dcs .number")[0]).text().trim() || "0";
newVariableCount = parseInt(newVariableCountString);
// console.log("====> CURRENT: " + currentVariableCount + ", NEW: " + newVariableCount)
if (newVariableCount == targetVariableCount) {
console.log("\n\n=====> 👍👍👍: GREAT SUCCESS! ON TO THE NEXT PAGE...\n\n");
$("a.next_page")[0].click();
break;
}
if (newVariableCount == currentVariableCount) {
noUpdateWaitCount++;
} else {
currentVariableCount = newVariableCount;
noUpdateWaitCount = 0;
}
await sleep(1000);
}
if (noUpdateWaitCount >= 8) {
console.log("\n\n=====> 🔥🔥🔥: DIDN'T ADD ALL VARIABLES! TRY AGAIN!\n\n");
location.reload();
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
stop = false;
while (stop != true) {
document.getElementById("bigCookie").click();
await sleep(.0001);
}