MongoDB won't update with testcafe tests - node.js

Im using testcafe for GUI tests. I'm running the node server in the background and then start testing. When I'm navigating though the GUI with testcafe API it all works great, but when I'm trying to call a function that changes the db (hard coded in the test) the database isn't effected at all.
Here is my code:
fixture('Permissions')
.page('https://localhost');
test('go to permissions', async browser => {
await onlineFormsController.createOnlineFrom("OMRI",'NEWFORM',async ()=>{
await browser.click('#editUsersTree');
await browser.click('#loadDefaultTree');
await browser.wait(500);
await browser.pressKey('enter');
await browser.wait(500);
await browser.pressKey('enter');
await browser.click('#saveTree');
await browser.pressKey('enter');
await browser.navigateTo('https://localhost/Home');
await browser.wait(5000);
});
});
The function onlineFormsController.createOnlineFrom should create new form in my database, but nothing happened. It's working good separately form the testcafe test (tested it with mocha and it works great), but when I'm running the test with testcafe it's like this line is ignored or have no effects at all.
Any ideas what causing this problem?

TestCafe should not interfere with MongoDB work.  
I recommend you the following: 
ensure that the createOnlineFrom returns a Promise object;
do not pass function with test actions as a parameter. Since the createOnlineFrom returns a Promise object, you can organize your code in a more readable way:
test('go to permissions', async browser => {
await onlineFormsController.createOnlineFrom("OMRI",'NEWFORM');
await browser.click('#editUsersTree');
});
If these recommendations do not help, please provide the full code of your createOnlineFrom function.

Related

Selenium Test Case not getting executed with Jest

I am working on setting up an automation test suite for an application using selenium and jest and it turns out that the console never reaches the inner body of the minimalist test case written below.
describe('When a user Opens Launchpad', () => {
test('It should be able to Navigate to Tasks Application without errors', async () => {
driver.get('http://localhost:4004/fiori.html').then(function () {
const temp = driver.findElement(By.xpath("li[#id='__tile11']"));
temp.then(function (element){
element.getAttribute("innerHTML");
expect(element.getText()).toBe("VT Dashboard");
})
});
}, 200000);
});
I looked online and tried multiple fixes like putting the driver.get() method above all these functions, making the test cases synchronous and using getText() instead of getAttribute() but none of them worked.
I either get an element not found error (The element actually exists when I check it on the chromium browser) or the test case executes successfully without reaching the expect statement in debug mode.
Bottomline is that driver.findElement() returns a promise instead of an element and would be great if I could get an element instead of promise.
Any help or correction here would be greatly appreciated.
If the function is async you should return the promise chain from that function or just use await keyword. So try following:
test('It should be able to Navigate to Tasks Application without errors', async () => {
await driver.get('http://localhost:4004/fiori.html');
const temp = await driver.findElement(By.xpath("li[#id='__tile11']"));
element.getAttribute("innerHTML");
expect(element.getText()).toBe("VT Dashboard");
}, 200000);
or
test('It should be able to Navigate to Tasks Application without errors', async () => {
return driver.get('http://localhost:4004/fiori.html').then(function () {
const temp = driver.findElement(By.xpath("li[#id='__tile11']"));
temp.then(function (element){
element.getAttribute("innerHTML");
expect(element.getText()).toBe("VT Dashboard");
})
});
}, 200000);

BadRequestException: no open transaction, qldb, nodejs driver

I set up my nodejs app with qldb to implement a wallet service. Set up some tests with some success tests and some expected error tests and once in a while this error 'BadRequestException: no open transaction' would happen and cause my tests to fail. if I run the test again, they will pass. Again once in a while, this error will happen unexpectedly cause the tests to fail. I noted when commented out my expected error tests and the error didn't happen or did not happen as often. and this error happens not only to the expected error test but to the successful tests.
this is how my tests look like
describe('createWallet()', () => {
it('should return an object with wallet Id', async () => {
let result6 = await controller.createWallet({ body: mocks.walletInfo6.info });
documentId6 = result6.walletId;
expect(result6).to.have.property('walletId').that.is.a.uuid;
});
it('One player should have only one active wallet for each currency', async () => {
try {
let res = await controller.createWallet({ body: mocks.walletInfo1.info });
assert.fail('expected error was not thrown')
} catch (e) {
expect(e.message).to.equal('Player already owns an active wallet in this currency.');
}
});
});
describe('suspendWallet()', () => {
it('should change wallet status to suspend', async () => {
let res = await controller.suspendWallet({ documentId: documentId3 });
await controller.suspendWallet({ documentId: documentId5 });
expect(res).to.be.a.string;
expect(res).to.equal(documentId3);
});
it('should not change wallet status if wallet Id is invalid', async () => {
try {
let res = await controller.suspendWallet({ documentId: mocks.invalidWalletId });
assert.fail('expected error was not thrown')
} catch (e) {
expect(e.message).to.equal('Did not find any record with this document Id.');
}
});
});
It's hard to be certain about how your application is running into this error without looking at how the driver is being used to execute transactions.
The driver APIs (for example - execute ) returns a promise. One way the application could be seeing the "No transaction open error" is that the promise is not resolved before sending further commands.
Cookbook - Refer to the QLDB JS driver cookbook which lists code sample for CRUD operations. Note how the samples use await inside the transactions to wait for the promises to resolve. Not waiting for the promises returned by execute can cause the driver to commit the transaction before the execute call is processed and hence a "No open transaction error".
Sample code for executing transactions -
var qldb = require('amazon-qldb-driver-nodejs');
const qldbDriver = new qldb.QldbDriver("vehicle-registration");
(async function() {
await qldbDriver.executeLambda(async (txn) => {
await txn.execute("CREATE TABLE Person");
});
})();
In case you still face issues, please share the code snippet where you use the driver to execute transactions.
Update on this issue. I use nodejs driver version 2.1.0.
My team and I found out that the problem was because there are rollbacks that happen after the error tests and we don't know when the rollbacks are done. When the rollback of the previous test is still running, the transaction for that test is still open so if the next test tries to open a new transaction it would conflict and would not able to open a new transaction for the next test. To fix this, we just not throw errors inside the transaction to prevent rollbacks to happen. This way works for our code, but a better solution would be a way to detect when the rollback is done from the driver and wait for the transaction to close before opening a new transaction.

how to keep sending requests one after the other in nodejs

I am using puppeteer to automate a website, puppeteer session may take about (30s-60s), and I want to fire a request(open another puppeteer session) right after the one before it finishes and I realized that I can't use setInterval because time is not constant in my case, how can I achieve such thing
Use a recursive function to keep calling the same function right after it's done.
async function bot(){
const browser = await puppeteer.launch()
// other code
await browser.close()
}
async function fireMyRequest(){
await bot()
await fireMyRequest()
}

Error Handling - How do I stop the script if there's an error in a before each hook?

I am using Testcafe for E2E automation and I want to add some prettier errors (instead of x element not found in DOM). Before each test, I do a clean login to the application, but that might fail (since I run the tests in DEV environment after a successful master build deployment), such as, devs broke the build, a service essential for login is down, the login itself broke and so on.
I want my script to fail right away instead of trying to run all tests and fail with the same reason. I read on stack overflow that I should use a return and that my script should stop with return, that's why I added it in the second catch block (retry login, then fail), but it doesn't seem to do that. What am I doing wrong?
Should I use a try/catch with a return in the loginpage.login or I can achieve what I want to achieve in the before each hook?
Right now, if login fails for whatever reason, the script still attempts to execute all tests. I want the tests to be skipped if the before each hook failed. How can I achieve this?
fixture `Activity History Tests`
.page `https://mypage.com`
.beforeEach(async (t) => {
try {
await loginPage.login(username, password)
}
catch(e) {
try {
console.log('Login Failed, retrying. Failure reason: ')
await loginPage.login(username, password)
}
catch(e) {
throw new Error('Couldn\'t login')
return
}
}}
)
test('Test #1: do something, async (t) => {
await t.setTestSpeed(0.8)
try { //the test goes here }
The contents of loginPage.login() are:
async login(username: string, password: string) {
await t
.setTestSpeed(0.5)
.maximizeWindow()
.click(this.loginButton)
await t
.typeText(this.emailField, username, {paste:true})
.click(this.submit)
await this.passwordField.visible
await t
.typeText(this.passwordField, password)
.click(this.signInButton)
// await t
//.click(this.staySignedIn)
const breakPanelText = await this.breakPanel
await t
.expect(breakPanelText.innerText).notContains("Hello Agent", {timeout: 10000})
};
At present, there is no public API to abort test execution using the Test API. I've created a suggestion in the TestCafe GitHub repository for your use case.

cucumber js execution - ELIFECYLE ERR

I'm new to JS and trying cucumber js for the first time
This is how my step defn looks like:
Pseudocodes
Given("I launch Google.com", async (){
await Launch.launchGoogle():
})
When("I enter search text cucumber js", async (){
await Launch.searchCucumber():
})
This is how my Launch.js looks like:
module.exports launchGoogle= async function() {
await driver.get("www.google.com"):
}
module.exports searchCucumber = async function(){
await driver.findElement(By.name("q")).sendKeys("cucumber");
}
In this case, when I run the feature with 2 steps, I get ELIFECYCLE ERR at the end of first step.
When I remove the await in the step definitions, it runs fine. But, the console displays as 2 steps passed even before the chrome browser is launched. That is, it fires the Given and When steps and shows the result even as the code in Launch.js is still executing.
Pls help how to solve this?
I just figured out that the default step timeout is 5000ms. Since, launching the browser and hitting the URL was taking more than that, it was failing. i just increased the step timeout to 30000ms and it works fine.

Resources