Skip to content

Commit

Permalink
Fix links continually reported
Browse files Browse the repository at this point in the history
Fixes #81

Signed-off-by: Simon Bennetts <[email protected]>
  • Loading branch information
psiinon committed Oct 23, 2023
1 parent de871e4 commit 4fe28e9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## Unreleased

### Changed

- Init ZAP URL to http://zap/ instead of http://localhost:8080/

### Fixed
- Same links continually reported on domMutation events (Issue 81).

## 0.0.6 - 2023-09-19

### Fixed
Expand Down
10 changes: 10 additions & 0 deletions source/types/ReportedModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ class ReportedElement extends ReportedObject {
this.href = element.getAttribute('href');
}
}

public toShortString(): string {
return JSON.stringify(this, function replacer(k: string, v: string) {
if (k === 'timestamp') {
// No point reporting the same element lots of times
return undefined;
}
return v;
});
}
}

class ReportedEvent {
Expand Down
33 changes: 33 additions & 0 deletions test/ContentScript/integrationTests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,39 @@ function integrationTests(
'"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"sessionStorage\\",\\"tagName\\":\\"\\",\\"id\\":\\"test\\",\\"nodeName\\":\\"\\",\\"url\\":\\"http://localhost:1801/webpages/sessionStorage.html\\",\\"text\\":\\"sessionData\\"}\\",\\"apikey\\":\\"not set\\"}}"]';
expect(JSON.stringify(Array.from(actualData))).toBe(expectedData);
});

test('Should record dup added link once ', async () => {
// Given / When
server = getFakeZapServer(actualData, _JSONPORT);
const context = await driver.getContext(_JSONPORT);
const page = await context.newPage();
await page.goto(
`http://localhost:${_HTTPPORT}/webpages/integrationTest.html`
);
await page.waitForLoadState('networkidle');

await page.evaluate(() => {
// eslint-disable-next-line func-names
const addLink = function (): void {
const aTag = document.createElement('a');
aTag.innerHTML = 'Test link';
aTag.href = 'https://www.example.com';
aTag.title = 'Test link';
document.body.appendChild(aTag);
};
addLink();
addLink();
});
await page.waitForTimeout(1000);
await page.close();
// Then
const expectedData =
'["{\\"action\\":{\\"action\\":\\"reportEvent\\"},\\"body\\":{\\"eventJson\\":\\"{TIMESTAMP,\\"eventName\\":\\"pageLoad\\",\\"url\\":\\"http://localhost:1801/webpages/integrationTest.html\\",\\"count\\":1}\\",\\"apikey\\":\\"not set\\"}}",' +
'"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"nodeAdded\\",\\"tagName\\":\\"A\\",\\"id\\":\\"\\",\\"nodeName\\":\\"A\\",\\"url\\":\\"http://localhost:1801/webpages/integrationTest.html\\",\\"href\\":\\"http://localhost:1801/webpages/integrationTest.html#test\\",\\"text\\":\\"Link\\"}\\",\\"apikey\\":\\"not set\\"}}",' +
'"{\\"action\\":{\\"action\\":\\"reportEvent\\"},\\"body\\":{\\"eventJson\\":\\"{TIMESTAMP,\\"eventName\\":\\"domMutation\\",\\"url\\":\\"http://localhost:1801/webpages/integrationTest.html\\",\\"count\\":1}\\",\\"apikey\\":\\"not set\\"}}",' +
'"{\\"action\\":{\\"action\\":\\"reportObject\\"},\\"body\\":{\\"objectJson\\":\\"{TIMESTAMP,\\"type\\":\\"nodeAdded\\",\\"tagName\\":\\"A\\",\\"id\\":\\"\\",\\"nodeName\\":\\"A\\",\\"url\\":\\"http://localhost:1801/webpages/integrationTest.html\\",\\"href\\":\\"https://www.example.com/\\",\\"text\\":\\"Test link\\"}\\",\\"apikey\\":\\"not set\\"}}"]';
expect(JSON.stringify(Array.from(actualData))).toBe(expectedData);
});
}
}

Expand Down

0 comments on commit 4fe28e9

Please sign in to comment.