|
| 1 | +const assert = require('assert'); |
| 2 | +const sinon = require('sinon'); |
| 3 | + |
| 4 | +const helper = require('../../../src/utils/helper'); |
| 5 | +const TestMap = require('../../../src/utils/testMap'); |
| 6 | +const TestObservability = require('../../../src/testObservability'); |
| 7 | + |
| 8 | +// Regression coverage for SDK-5914 / suppressNotFoundErrors. |
| 9 | +// |
| 10 | +// Before this fix, sendTestRunEvent walked eventData.commands for any |
| 11 | +// status:'fail' record and flipped testData.result to 'failed' even when |
| 12 | +// the failing command was a Nightwatch `isVisible({suppressNotFoundErrors:true})` |
| 13 | +// lookup whose absence is an expected, callsite-opted-into outcome. |
| 14 | +// The bug shipped because this function had no unit coverage at all. |
| 15 | +describe('TestObservability - sendTestRunEvent (suppressNotFoundErrors)', function () { |
| 16 | + const buildTest = (commands, envelopeRollup = {status: 'pass', failed: 0, errors: 0}) => ({ |
| 17 | + metadata: { |
| 18 | + name: 'Conditional Suite', |
| 19 | + tags: [], |
| 20 | + modulePath: '/tmp/observabilityBugRepro.js', |
| 21 | + host: 'hub-cloud.browserstack.com', |
| 22 | + sessionId: 'session-id-stub', |
| 23 | + sessionCapabilities: {} |
| 24 | + }, |
| 25 | + testcase: 'conditional test', |
| 26 | + testCaseData: () => '', |
| 27 | + settings: {desiredCapabilities: {'bstack:options': {osVersion: '11'}}}, |
| 28 | + envelope: { |
| 29 | + 'conditional test': { |
| 30 | + startTimestamp: 1700000000000, |
| 31 | + testcase: { |
| 32 | + endTimestamp: 1700000001000, |
| 33 | + commands, |
| 34 | + ...envelopeRollup |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + this.sandbox = sinon.createSandbox(); |
| 42 | + this.testObservability = new TestObservability(); |
| 43 | + |
| 44 | + this.sandbox.stub(this.testObservability, 'getTestBody').returns(''); |
| 45 | + this.sandbox.stub(this.testObservability, 'processTestRunData').resolves(); |
| 46 | + this.sandbox.stub(helper, 'getCloudProvider').returns('automate'); |
| 47 | + this.sandbox.stub(helper, 'getIntegrationsObject').returns({}); |
| 48 | + this.sandbox.stub(helper, 'isTestObservabilitySession').returns(true); |
| 49 | + this.sandbox.stub(helper, 'isAccessibilitySession').returns(false); |
| 50 | + this.sandbox.stub(TestMap, 'getSessionSnapshot').returns(null); |
| 51 | + |
| 52 | + this.uploaded = null; |
| 53 | + this.uploadStub = this.sandbox.stub(helper, 'uploadEventData').callsFake(async (payload) => { |
| 54 | + this.uploaded = payload; |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + this.sandbox.restore(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('marks the test passed when the only failing command opted into suppressNotFoundErrors (args as object)', async () => { |
| 63 | + const commands = [ |
| 64 | + {name: 'url', args: ['https://www.google.com'], status: 'pass'}, |
| 65 | + { |
| 66 | + name: 'isVisible', |
| 67 | + args: [{selector: '#may-or-may-not-exist', suppressNotFoundErrors: true, timeout: 2000}, null], |
| 68 | + status: 'fail', |
| 69 | + result: {message: 'Element not found', stack: '', name: 'Error'} |
| 70 | + } |
| 71 | + ]; |
| 72 | + |
| 73 | + await this.testObservability.sendTestRunEvent('TestRunFinished', buildTest(commands), 'uuid-1'); |
| 74 | + |
| 75 | + sinon.assert.calledOnce(this.uploadStub); |
| 76 | + assert.strictEqual(this.uploaded.event_type, 'TestRunFinished'); |
| 77 | + assert.strictEqual(this.uploaded.test_run.result, 'passed'); |
| 78 | + assert.ok(!('failure' in this.uploaded.test_run), 'expected no failure field on passed test'); |
| 79 | + assert.ok(!('failure_reason' in this.uploaded.test_run), 'expected no failure_reason field on passed test'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('marks the test passed when args[0] is a JSON-encoded string carrying suppressNotFoundErrors', async () => { |
| 83 | + // Some Nightwatch reporter paths serialize the options object to a JSON |
| 84 | + // string in command.args[0] — the customer's CHROME_148__observabilityBugRepro.json |
| 85 | + // is the canonical example. The fix must handle both shapes. |
| 86 | + const commands = [ |
| 87 | + { |
| 88 | + name: 'isVisible', |
| 89 | + args: ['{"selector":"#may-or-may-not-exist","suppressNotFoundErrors":true,"timeout":2000}', null], |
| 90 | + status: 'fail', |
| 91 | + result: {message: 'Element not found', stack: ''} |
| 92 | + } |
| 93 | + ]; |
| 94 | + |
| 95 | + await this.testObservability.sendTestRunEvent('TestRunFinished', buildTest(commands), 'uuid-2'); |
| 96 | + |
| 97 | + assert.strictEqual(this.uploaded.test_run.result, 'passed'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('still marks the test failed when a real assertion failure is present', async () => { |
| 101 | + // Envelope rollup says failed:1 — a real failure happened. The fix must |
| 102 | + // NOT suppress that. This is the contrast case that prevents the patch |
| 103 | + // from silently downgrading every failing test to passed. |
| 104 | + const commands = [ |
| 105 | + { |
| 106 | + name: 'assert.titleContains', |
| 107 | + args: ['Google'], |
| 108 | + status: 'fail', |
| 109 | + result: {message: 'Expected title to contain "Google"', stack: 'AssertionError', name: 'AssertionError'} |
| 110 | + } |
| 111 | + ]; |
| 112 | + |
| 113 | + await this.testObservability.sendTestRunEvent( |
| 114 | + 'TestRunFinished', |
| 115 | + buildTest(commands, {status: 'fail', failed: 1, errors: 0}), |
| 116 | + 'uuid-3' |
| 117 | + ); |
| 118 | + |
| 119 | + assert.strictEqual(this.uploaded.test_run.result, 'failed'); |
| 120 | + assert.strictEqual(this.uploaded.test_run.failure_type, 'AssertionError'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('still marks the test failed when a non-suppressed command failed alongside a suppressed one', async () => { |
| 124 | + // Mixed case: one suppressed isVisible + one real failure. Envelope rollup |
| 125 | + // disagrees with "all passed", so we must propagate the real failure. |
| 126 | + const commands = [ |
| 127 | + { |
| 128 | + name: 'isVisible', |
| 129 | + args: [{selector: '#optional', suppressNotFoundErrors: true}, null], |
| 130 | + status: 'fail', |
| 131 | + result: {message: 'Element not found'} |
| 132 | + }, |
| 133 | + { |
| 134 | + name: 'click', |
| 135 | + args: ['#mandatory'], |
| 136 | + status: 'fail', |
| 137 | + result: {message: 'Element #mandatory not found', stack: 'NoSuchElementError', name: 'NoSuchElementError'} |
| 138 | + } |
| 139 | + ]; |
| 140 | + |
| 141 | + await this.testObservability.sendTestRunEvent( |
| 142 | + 'TestRunFinished', |
| 143 | + buildTest(commands, {status: 'fail', failed: 0, errors: 1}), |
| 144 | + 'uuid-4' |
| 145 | + ); |
| 146 | + |
| 147 | + assert.strictEqual(this.uploaded.test_run.result, 'failed'); |
| 148 | + // failedCommand must skip the suppressed one and pick the real one. |
| 149 | + assert.strictEqual(this.uploaded.test_run.failure_type, 'UnhandledError'); |
| 150 | + }); |
| 151 | +}); |
0 commit comments