Protractor doesn't send data for login keys, - protractor

I have an issue relating to sending keys to login page, only the browser launched and nothing happens, how ever was able to run the script previously with no issue.This might be due to an upgrade on angular in the test server any solution for it much appreciated? recently had angualr 5 upgrade from a lower version
describe(' E2E1', function () {
afterEach(function () {
browser.executeScript('window.sessionStorage.clear();');
browser.executeScript('window.localStorage.clear();');
browser.restart();
});
it('Scenario 1', function () {
browser.get('some url');
var EC = protractor.ExpectedConditions;
browser.wait(EC.visibilityOf($('#company-code')))
.then(function () {
browser.driver.findElement(by.id('company-code')).sendKeys('cc');
browser.driver.findElement(by.id('username')).sendKeys('email#dmoain.com');
browser.driver.findElement(by.id('password')).sendKeys('sompepassword');
browser.driver.findElement(by.id('signin')).click()
.then(function () {
const EC = protractor.ExpectedConditions;
browser.wait(EC.urlContains('URL after login'), 100000).then(function () {
console.log('Successfully logged')
});
});
});
});

Related

Service Worker not working when hosted, but works on localhost

I'm working on a PWA and I'm facing an issue with the service worker and I can't figure out what's wrong.
So when I run the lighthouse audit on localhost, it passes every criteria except for the HTTPS one. You can view it below;
However, when I publish the code to my github pages, and run the same audit there, the service worker is never activated. It gives me the error. The status becomes 'redundant' when I run the audit online.
Link: https://ibr4h1m.github.io/MAD5/index.html
Below I'll show the code, which is the exact same on the website that I've mentioned above.
main.js:
//Simple ServiceWorker
if('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js');
};
sw.js
const cacheName = 'tourguide-site';
const appShellFiles = ['index.html',
'help.html',
'destinations.html',
'contact.html',
'js/main.js',
'css/style.css',
'sw.js'
];
self.addEventListener('install', (e) => {
console.log('[Service Worker] Install');
e.waitUntil((async () => {
const cache = await caches.open(cacheName);
console.log('[Service Worker] Caching all: app shell and content');
await cache.addAll(appShellFiles);
})());
});
// Simple Activate since the other one is BS
self.addEventListener('activate', function () {
console.log('SW Activated');
});
self.addEventListener('fetch', (e) => {
e.respondWith((async () => {
const r = await caches.match(e.request);
console.log(`[Service Worker] Fetching resource: ${e.request.url}`);
if (r) { return r; }
const response = await fetch(e.request);
const cache = await caches.open(cacheName);
console.log(`[Service Worker] Caching new resource: ${e.request.url}`);
cache.put(e.request, response.clone());
return response;
})());
});
Online audit:
const appShellFiles = ['index.html',
'help.html',
'destinations.html',
'contact.html',
'js/main.js',
'css/style.css',
'sw.js'
];
Remove the sw.js from your appShellFiles

Best way to exeute various tests in protractor for various login id's

I have to test application using protractor for various different types of users. userid determines the type of user( Admin or partner or user). For all users i need to test the application for all major functionality. Here is what i want to do
Login User 1 > execute test1, test 2, test 3 .... Logout
Login user 2 > execute test1, test 2, test 3 .... Logout
Login User 3 > execute test1, test 2, test 3 .... Logout
Login User 4 > execute test1, test 2, test 3 .... Logout
I want to create a test framework to cover the scenario. I would appreciate inputs on best way to achieve this.
You could do this in a couple of ways. There are pro's and con's to each.
The first way is to just loop over the list of users.
describe('your suite', () => {
for (let user of users) {
describe('test for user: ' + user.username, () => {
beforeAll(() => { login(user) });
it('should do a test', () => {
// test code with user
});
it('should do another test', () => {
// test code with user
});
afterAll(() => { logout(user) });
});
}
});
Another way is to define your callback functions then compose your tests for each user.
testCallback1 = function() {
// test code with user
}
testCallback2 = function() {
// test code with user
}
describe('test for user'), () => {
let user = users[0];
beforeAll(() => { login(user) });
it('should do a test', testCallback1);
it('should do another test', testCallback2);
afterAll(() => { logout(user) });
});
describe('test for user'), () => {
let user = users[1];
beforeAll(() => { login(user) });
it('should do a test', testCallback1);
it('should do another test', testCallback2);
afterAll(() => { logout(user) });
});
There is probably a better way to do this. These are just a couple of suggestions.
I have a similar scenario and here is how I've tackled it. I added an array of users in the protractor config and then I can loop through those users to run my login tests. It's essentially the same setup as the first suggestion by cnishina in his answer. The difference for me is that my test code lives in one file and I have the code that gets the users and loops through them in another file. Probably overkill for a simple scenario but we have several different types of users (admin, non-admin, users with services, users without services, etc.) to run the login tests against so this helps to simplify that situation. I'm using TypeScript but the same thing can be done with JS if that's what you are using.
//in the protractor config
params: {
loginUsers: [
"login1#domain.test",
"login2#domain.test",
"login3#domain.test",
//and so on ...
]
}
Then I have a file where my login tests live called shared-login.tests.ts
export default class SharedLoginTests {
public static sharedSetup(url: string): void {
beforeEach(() => {
//shared setup code ...
}
afterEach(() => {
//shared tear down code ...
}
}
public static executeSharedLoginTests(username: string): void {
it(`should allow ${username} to login`, () => {
//login test code ...
}
}
}
And then I have login.tests.ts like this:
import { browser } from "protractor";
import SharedLoginTests from "./shared-login.tests";
describe(testtitle, () => {
const users: string[] = browser.params.loginUsers;
SharedLoginTests.sharedSetup(browser.params.baseUrl);
for (const user of users) {
SharedLoginTests.executeSharedLoginTests(user);
}
});

how to deal with mongodb race condition in integration test

I have a mongoose schema with a unique field and I am trying to write a backend (express) integration test which checks that POSTing the same entity twice results in HTTP 400. When testing manually behaviour is as excpected. Automatic testing however requires a wait:
it('should not accept two projects with the same name', function(done) {
var project = // ...
postProjectExpect201(project,
() => {
setTimeout( () => {
postProjectExpect400(project, done);
},100);
}
);
});
The two post... methods do as named and the code above works fine, but if the timeout is removed, BOTH requests receive HTTP 200 (though only one entity created in the database).
I'm new to those technologies and I'm not sure what's going on. Could this be a mongodb related concurrency issue and if so how should I deal with it?
The database call looks like this:
Project.create(req.body)
.then(respondWithResult(res, 201))
.catch(next);
I already tried connecting to mongodb with ?w=1 option btw.
Update:
To be more verbose: Project is a mongoose model and next is my express error handler which catches the duplicate error.
The test functions:
var postProjectExpect201=function(project, done, validateProject) {
request(app)
.post('/api/projects')
.send(project)
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) {
return done(err);
}
validateProject && validateProject(res.body);
done();
});
};
var postProjectExpect400=function(project, done) {
request(app)
.post('/api/projects')
.send(project)
.expect(400)
.end((err, res) => {
if (err) {
return done(err);
}
done();
});
};

404 on Karma when trying to use web worker

I'm getting the error WARN [web-server]: 404: /app/workers/total.js when trying to run a unit test on a web worker.
Karma.conf.js includes the following:
...
files: [
...
'app/workers/*.js',
'unit-tests/mocks/**/*.js',
'unit-tests/src/**/*.js'
],
....
the test goes as follows:
describe('totals', function () {
var worker;
beforeEach(function() {
worker = new Worker('/app/workers/total.js');
});
it('should do something', function () {
...
});
});
I have tried many urls, but none seem to work
Finally I found the solution on https://github.com/karma-runner/karma/issues/1302, the trick is to include /base as part of the worker URL, being the solution:
describe('totals', function () {
var worker;
beforeEach(function() {
worker = new Worker('/base/app/workers/total.js');
});
it('should do something', function () {
...
});
});
Note /base as part of the worker URL.
Thanks to maksimr

How to set up unit tests in sailsjs

I cannot run sailsjs unit tests. It seems sails cannot be lifted,
my test (/test/unit/test.js):
var Sails = require('sails');
var app;
before(function() {
console.log('before');
Sails.lift({
log: {
level: 'error'
}
}, function(err, server) {
console.log('lifted');
app = server;
done(err, app);
});
});
// Global after hook
after(function(done) {
app.lower(done);
});
describe('mailer service', function() {
it('should connect to gmail', function() {
console.log(app);
});
});
In my app folder I run: mocha test/unit/test.js
The "app" variable is undefined, console.log('lifted') is not being triggered. What am I doing wrong?
First of all.
You need to call before with a done parameter :
before(function(done){...})
Does your app lift succesfully when you run it with ?
sails lift