Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log errors thrown when filling a form #217

Merged
merged 8 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice


defaults:
run:
Expand Down
18 changes: 15 additions & 3 deletions docs/harness.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,13 @@ <h1 class="page-title">Source: harness.js</h1>
}

this.log(`Filling ${answers.length} pages with answer: ${JSON.stringify(answers)}`);
const fillResult = await this.page.evaluate(async innerAnswer => await window.formFiller.fillAppForm(innerAnswer), answers);
const fillResult = await this.page.evaluate(async innerAnswer => {
try {
return await window.formFiller.fillAppForm(innerAnswer);
} catch (err) {
throw new Error(`Error encountered while filling form: ${err}`);
}
}, answers);
this.log(`Result of fill is: ${JSON.stringify(fillResult, null, 2)}`);

if (this.options.logFormErrors &amp;&amp; fillResult.errors &amp;&amp; fillResult.errors.length > 0) {
Expand Down Expand Up @@ -719,7 +725,13 @@ <h1 class="page-title">Source: harness.js</h1>
self._state.pageContent = await self.page.content();

self.log(`Filling ${answers.length} pages with answer: ${JSON.stringify(answers)}`);
const fillResult = await self.page.evaluate(async (innerContactType, innerAnswer) => await window.formFiller.fillContactForm(innerContactType, innerAnswer), contactType, answers);
const fillResult = await self.page.evaluate(async (innerContactType, innerAnswer) => {
try {
return await window.formFiller.fillContactForm(innerContactType, innerAnswer);
} catch (err) {
throw new Error(`Error encountered while filling form: ${err}`);
}
}, contactType, answers);
self.log(`Result of fill is: ${JSON.stringify(fillResult, null, 2)}`);

// https://github.com/medic/cht-conf-test-harness/issues/105
Expand Down Expand Up @@ -853,7 +865,7 @@ <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="mock.cht-
<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Tue Apr 12 2022 01:26:36 GMT+0300 (East Africa Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.11</a> on Thu Apr 06 2023 18:04:43 GMT+0300 (East Africa Time)
</footer>

<script> prettyPrint(); </script>
Expand Down
2,644 changes: 1,564 additions & 1,080 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cht-conf-test-harness",
"version": "2.4.3",
"version": "2.4.4",
"description": "Test Framework for CHT Projects",
"main": "./src/harness.js",
"scripts": {
Expand All @@ -27,6 +27,7 @@
"@medic/eslint-config": "^1.1.0",
"bikram-sambat": "^1.7.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"chai-exclude": "^2.0.2",
"cht-core-3-10": "git+https://github.com/medic/cht-core.git#3.10.x",
"cht-core-3-11": "git+https://github.com/medic/cht-core.git#3.11.x",
Expand Down
16 changes: 14 additions & 2 deletions src/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,13 @@ class Harness {
}

this.log(`Filling ${answers.length} pages with answer: ${JSON.stringify(answers)}`);
const fillResult = await this.page.evaluate(async innerAnswer => await window.formFiller.fillAppForm(innerAnswer), answers);
const fillResult = await this.page.evaluate(async innerAnswer => {
try {
return await window.formFiller.fillAppForm(innerAnswer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the late feedback on this one: but i'm a little unsure on the value we are adding here... why catch an exception to just rethrow it? what problem are we solving?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't catch the exception, we see a confusing error in the stack trace like

Error: Evaluation failed: FormLogicError
at ExecutionContext._evaluateInternal (node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:221:19)
at process._tickCallback (internal/process/next_tick.js:68:7)

When we catch and rethrow, the error in the stack trace makes more sense for the user like

Error: Evaluation failed: Error: Error encountered while filling form: FormLogicError: Could not evaluate: concat(../mother_name, ' baby'), message: Failed to execute 'evaluate' on 'Document': The string 'concat("Jane"s", " baby")' is not a valid XPath expression.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can your test also make an assertion about not just the boilerplate you're adding but also the more descriptive error message like 'error.includes('mother_name')`?

} catch (err) {
throw new Error(`Error encountered while filling form: ${err}`);
}
}, answers);
this.log(`Result of fill is: ${JSON.stringify(fillResult, null, 2)}`);

if (this.options.logFormErrors && fillResult.errors && fillResult.errors.length > 0) {
Expand Down Expand Up @@ -691,7 +697,13 @@ const fillContactForm = async (self, contactType, action, ...answers) => {
self._state.pageContent = await self.page.content();

self.log(`Filling ${answers.length} pages with answer: ${JSON.stringify(answers)}`);
const fillResult = await self.page.evaluate(async (innerContactType, innerAnswer) => await window.formFiller.fillContactForm(innerContactType, innerAnswer), contactType, answers);
const fillResult = await self.page.evaluate(async (innerContactType, innerAnswer) => {
try {
return await window.formFiller.fillContactForm(innerContactType, innerAnswer);
} catch (err) {
throw new Error(`Error encountered while filling form: ${err}`);
kennsippell marked this conversation as resolved.
Show resolved Hide resolved
}
}, contactType, answers);
self.log(`Result of fill is: ${JSON.stringify(fillResult, null, 2)}`);

// https://github.com/medic/cht-conf-test-harness/issues/105
Expand Down
4 changes: 4 additions & 0 deletions test/app-forms.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,8 @@ describe('forms that have caused bugs', () => {
const bikramDate = await harness.page.evaluate(() => window.$$('[data-itext-id="/pregnancy/summary/r_pregnancy_details:label"]').text());
expect(bikramDate).to.include('महिनावारी भएको अन्तिम मिति : १५ कारà¥');
});
it('should log error thrown during filling an app form', async () => {
await expect(harness.fillForm('mother_name', ['Jane"s']))
.to.be.rejectedWith("FormLogicError: Could not evaluate: concat(../mother_name, ' baby')");
});
});
44 changes: 44 additions & 0 deletions test/collateral/forms/app/mother_name.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms">
<h:head>
<h:title>Add household</h:title>
<model>
<itext>
<translation lang="en">
<text id="/mother_name/contact/mother_name:label">
<value>Whose baby is it ?</value>
</text>
<text id="/mother_name/contact/name:label">
<value>Baby mother</value>
</text>
<text id="/mother_name/contact:label">
<value>Baby mother</value>
</text>
</translation>
</itext>
<instance>
<mother_name id="contact:clinic:create" prefix="J1!contact:clinic:create!" delimiter="#" version="2023-04-18 04:46:19">
<contact>
<name/>
<mother_name/>
</contact>
<meta tag="hidden">
<instanceID/>
</meta>
</mother_name>
</instance>
<instance id="contact-summary"/>
<bind nodeset="/mother_name/contact/name" type="string" required="true()" calculate="concat(../mother_name, ' baby')"/>
<bind nodeset="/mother_name/contact/mother_name" type="string" required="true()"/>
<bind nodeset="/mother_name/meta/instanceID" type="string" readonly="true()" calculate="concat('uuid:', uuid())"/>
</model>
</h:head>
<h:body class="pages">
<group appearance="field-list" ref="/mother_name/contact">
<label ref="jr:itext('/mother_name/contact:label')"/>
<input ref="/mother_name/contact/mother_name">
<label ref="jr:itext('/mother_name/contact/mother_name:label')"/>
</input>
</group>
</h:body>
</h:html>
67 changes: 67 additions & 0 deletions test/collateral/forms/contact/household-create.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms">
<h:head>
<h:title>Add household</h:title>
<model>
<itext>
<translation lang="en">
<text id="/data/contact/contact_name:hint">
<value>Please include both names</value>
</text>
<text id="/data/contact/contact_name:jr:constraintMsg">
<value>Enter a valid name consisting of only letters starting with a capital letter</value>
</text>
<text id="/data/contact/contact_name:label">
<value>Names</value>
</text>
<text id="/data/contact/name:label">
<value>Household Head</value>
</text>
<text id="/data/contact:label">
<value>Household Head</value>
</text>
</translation>
<translation lang="lg">
<text id="/data/contact/contact_name:hint">
<value>Osabibwa oteekewo amannya gombi</value>
</text>
<text id="/data/contact/contact_name:jr:constraintMsg">
<value>Yingizaamu erinnya ettuufu nga likolebwa nnukuta zokka ate nga litandisa nnukuta nnene</value>
</text>
<text id="/data/contact/contact_name:label">
<value>Amannya</value>
</text>
<text id="/data/contact/name:label">
<value>Akulira amaka</value>
</text>
<text id="/data/contact:label">
<value>Akulira amaka</value>
</text>
</translation>
</itext>
<instance>
<data id="contact:clinic:create" prefix="J1!contact:clinic:create!" delimiter="#" version="2023-04-18 03:47:20">
<contact>
<name/>
<contact_name/>
</contact>
<meta tag="hidden">
<instanceID/>
</meta>
</data>
</instance>
<bind nodeset="/data/contact/name" type="string" calculate="concat(../contact_name, ' Household')" required="true()"/>
<bind nodeset="/data/contact/contact_name" type="string" required="true()" jr:constraintMsg="jr:itext('/data/contact/contact_name:jr:constraintMsg')"/>
<bind nodeset="/data/meta/instanceID" type="string" readonly="true()" calculate="concat('uuid:', uuid())"/>
</model>
</h:head>
<h:body class="pages">
<group appearance="field-list" ref="/data/contact">
<label ref="jr:itext('/data/contact:label')"/>
<input ref="/data/contact/contact_name">
<label ref="jr:itext('/data/contact/contact_name:label')"/>
<hint ref="jr:itext('/data/contact/contact_name:hint')"/>
</input>
</group>
</h:body>
</h:html>
8 changes: 8 additions & 0 deletions test/contact-forms.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ const chaiExclude = require('chai-exclude');
const path = require('path');
const Harness = require('../src/harness');
const { cloneDeep } = require('lodash');
const chaiAsPromised = require('chai-as-promised');


chai.use(chaiExclude);
chai.use(chaiAsPromised);
const { expect } = chai;


Expand Down Expand Up @@ -188,4 +191,9 @@ describe('contact forms', () => {
reported_date: now.valueOf()
});
});
it('should log descriptive error thrown during filling a contact form', async () => {

await expect(harness.fillContactCreateForm('household', ['Peter"s']))
.to.be.rejectedWith("FormLogicError: Could not evaluate: concat(../contact_name, ' Household')");
});
});