Reusable functions for Protractor test framework - testing

I use Protractor Test Framework and need to use some the same operations for different cases. Like Authentication procedure.
The question is: What's the correct way to use own functions in Protractor?
I remember this tool works: 1) asynchronous and 2) its functions return promises.
Must the reusable function return the promise for make possibility do .then() operation or this function may return no value?
An Example (correct or no):
describe('Login procedure', function() {
it('Login Username', function () {
browser.get('anurl.com');
auth('username', 'password').then(function(){console.log('NICE TO MEET YOU')});
});
var auth = function(loginstr, passwordstr) {
return element(by.css('div[class="login"]')).isDisplayed().then(function (result) {
if (result) {
element(by.css('input[name="email"]')).clear().sendKeys(loginstr).then(
function () {
element(by.css('input[name="password"]')).clear().sendKeys(passwordstr).then(function () {
element(by.css('button[class="submit"]')).click().then(function () {
return element(by.id('welcome')).isPresent();
});
});
});
}
});
}
Thanks!

Your example looks fine.
It is good practice to return a promise from your utility functions (so callers can then off of it if they want). But it is not required.
In practice, the methods you are invoking that create promises (e.g., click or isDisplayed, etc) implicitly register the created promise with the control flow, so the implicit synchronization will not be impacted by refactoring the calls into your own functions.
See the Protractor Page Objects for a similar approach.

Our team uses Orchid-js with Jasmine and Protractor.
It's an extension that makes defining your own functions automatic.
In this case your code will still work, you would just be able to automatically reuse your 'Login procedure' and 'Login Username' functions as well.
Describe('Login procedure', function(username,password) {
It('Login Username', function (username,password) {
browser.get('anurl.com');
auth(username, password).then(function(){console.log('NICE TO MEET YOU')});
})(username,password);
})('username','password');
Then reuse it later
Describe('Login procedure')('differentUsername','differentPassword');

Related

mockRestore does not clear a jest.spyOn().mockImplementation mock

I am mocking Date.now() implementation returning a specific date however, after the test is done the afterAll + mockRestore() doesn't quite rid of the mock.
When I run another test the date now is still mocked to 1626764400000. Is there a different function I have to use to reset the mock? I have already used: mockReset, mockClear, jest.clearAllMocks.
beforeAll((): void => {
jest.spyOn(Date, 'now').mockImplementation(() => 1626764400000);
});
afterAll((): void => {
jest.clearAllMocks();
jest.spyOn(Date, 'now').mockRestore();
});
I have solved a similar issue by calling mockRestore of the returned value of the jest.spyOn(), that also returns a Jest mock function.
Oficcial docs.

Possible to manipulate CasperJS assertions?

My CasperJS asserts seem to be overly strict. I have a function where I am trying to test the names of client logo images from an array, using Casperjs. However I do not seem to be able to use a variable from a forLoop in casperJS.
I understand there are probably hoisting issues that I am not accounting for, but this does not seem to be the primary problem. I have tried several things to resolve hoisting issues, such as immediately invoked functions, try catch blocks, and using ES6 term "Let" in my loop. None seem to work. Then I notice if I simply hard-code the string my variable should represent, and stick a console.log into my assert of a PASSING test, right before the return, the passing test fails.
Here is my failing code
var clients = 'https://www.google.com/';
var logoArray = ["images/logos/AC.png", "images/logos/Affiny.png", "images/logos/ffintus.png", "images/logos/agileAsset.png"]
function checkClientsArrayTest() {
casper.test.begin('The layout is as expected', 10, function suite(test) {
casper.start(clients, function () {
casper.then(function () {
for (var i = 0; i < logoArray.length; i++) {
try { throw i }
catch (ii) {
console.log(ii);
console.log(i);
test.assertEvalEquals(function () {
return document.querySelectorAll('div.client_logo a img')[ii].getAttribute('src')
.match(logoArray[ii]).toString();
}, logoArray[ii], 'Test searches for Client Logos in DOM.');
}
}
});
}).run(function () {
test.done();
});
});
}
If I change logoArray[ii] to a hardcoded string from the first index of the array, it passes. If I consolelog logoArray[ii], it seems to be what I expect. But if I pass a variable to the assert, or even stick a console.log inside of it, the test fails with the following
Running check for the layout of URL: https://www.google.com
0
0
FAIL Test searches for Client Logos in DOM.
type: assertEvalEquals
file: headlessTester.js
subject: null
fn: undefined
params: undefined
expected: "images/logos/AC.png"
Is this an issue of me getting hoisting wrong (shouldn't fail by sticking in a logger if this is the case afaik), or is this due to strictly structured asserts in CasperJS?

How jasmine spy example works

All;
I am just starting learning Jasmine( version 2.0.3 ), when I got to Spies section, the first example confused me:
describe("A spy", function() {
var foo, bar = null;
beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
}
};
spyOn(foo, 'setBar');
foo.setBar(123);
foo.setBar(456, 'another param');
});
it("tracks that the spy was called", function() {
expect(foo.setBar).toHaveBeenCalled();
});
it("tracks all the arguments of its calls", function() {
expect(foo.setBar).toHaveBeenCalledWith(123);
expect(foo.setBar).toHaveBeenCalledWith(456, 'another param');
});
it("stops all execution on a function", function() {
expect(bar).toBeNull();
});
});
I wonder if anyone could explain why the setBar function does not affect the bar defined inside describe block? How Jasmine spies deal with this?
Thanks
Because you are not actually executing the methods.
If you want this test to fail:
it("stops all execution on a function", function() {
expect(bar).toBeNull();
});
After these calls:
foo.setBar(123);
foo.setBar(456, 'another param');
Then you should call and.callThrough for your spy.
spyOn(foo, 'setBar').and.callThrough();
From the documentation
Spies: and.callThrough
By chaining the spy with and.callThrough, the spy will still track all
calls to it but in addition it will delegate to the actual
implementation.
With regard to your question, 'how jasmine deals with this?'
From here you can read a basic explanation:
Mocks work by implementing the proxy pattern. When you create a mock
object, it creates a proxy object that takes the place of the real
object. We can then define what methods are called and their returned
values from within our test method. Mocks can then be utilized to
retrieve run-time statistics on the spied function such as:
How many times the spied function was called.
What was the value that the function returned to the caller.
How many parameters the function was called with.
If you want all of the implementation details, you can check the Jasmine source code which is Open Source :)
In this source file CallTracker you can see how the gather data about the method calls.
A little more about the proxy pattern.

How to override dojo's domReady

I want to override dijit._CssStateMixin's domReady() method.
Is there any way to override that instead of changing the listener mechanism in Dojo.
I tried overriding _cssMouseEvent() method in simple javascript, but it still does invoke dijit's _cssMouseEvent() from domReady().
I have tried following approach:
dojoConfig = {
map: {
'dijit/_CssStateMixin': {
'dojo/domReady': 'app/noop'
}
}
};
I have added 'app' folder and then 'noop.js' inside that.
noop.js has nothing in it:
define([], function () {
return function () {};
});
Even after this I can see that dijit.js's _CssStateMaxin domReady() getting called from listener.apply (code snippet pasted below)
var addStopImmediate = function(listener){
return function(event){
if(!event.immediatelyStopped){// check to make sure it hasn't been stopped immediately
event.stopImmediatePropagation = stopImmediatePropagation;
return listener.apply(this, arguments);
}
};
}
If your ultimate goal is to prevent the domReady callback in dijit/_CssStateMixin from running, your simplest bet is likely to re-map dojo/domReady to a different module that doesn't call the callback at all, when loaded via dijit/_CssStateMixin.
NOTE: Stripping out these handlers might have adverse visual effects on Dijit widgets which inherit _CssStateMixin, since it may hinder the application of Dijit CSS classes related to hover and focus. But if your concern is that _CssStateMixin is hampering performance, it may at least be worth a try to confirm or deny your suspicion.
First we have to create a simple module that returns a function that does nothing, which we will later substitute for dojo/domReady when loaded by dijit/_CssStateMixin, so that it can still call domReady but it won't execute the callback it passes.
For simplicity's sake I'll assume you already have a custom package that you can easily add a module to; for this example I'll assume it's called app. Let's create app/noop:
define([], function () {
return function () {};
});
Now let's configure the loader to map app/noop in place of dojo/domReady specifically when loaded by dijit/_CssStateMixin:
var dojoConfig = {
...,
map: {
'dijit/_CssStateMixin': {
'dojo/domReady': 'app/noop'
}
},
...
};
Now the offending domReady callback should no longer be run.
If you're curious about map, you can read more about it in this SitePen FAQ.

How to make a synchronous query (blocking call) with node-postgres?

While booting my Node.js app, I want to make a couple of synchronous calls to the PostgreSQL database to check some things before continuing the control flow. How can I achieve this using the node-postgres package?
You can use the pg-native or pg-sync package.
The only way to synchronize calls is to nest them in callbacks:
function init() {
pg.connect('tcp://user#host/db', function(err, client, done) {
client.query("SELECT column FROM table", function(err, result) {
// check some things...
client.query("SELECT column FROM other_table", function(err, result) {
// check some other things...
main(); // proceed with flow...
}
});
}
}
function main() {
// main logic
}
This can be a pain, and would be trivial in some other languages, but it is the nature of the beast.
brianc (author of node-postgres) commented here, that
"The only way to do sync queries is to use pg-native and use the sync variant of the
methods. Using pure javascript client it is impossible to do sync
queries because of how node works."
Hope this helps...
Given that the call is inherently asynchronous, you'll need to manage it either via callbacks (the defacto Node style), async or via promises (Q, when.js, Bluebird, etc...) Please note that wrt to the accepted answer, callbacks are not the only way to do this.
You can do this by using pg-pool in combination with asyncawait:
var suspendable = async (function defn(a, b) {
var name1 = await(pool.query('select $1::text as name', ['foo']));
console.log(name1.rows[0].name, 'says hello');
var name2 = await(pool.query('select $1::text as name', ['bar']));
console.log(name2.rows[0].name, 'says hello');
});
suspendable();