Skip to content

Commit

Permalink
[ci-visibility] Fix selenium when run outside of a supported test fra…
Browse files Browse the repository at this point in the history
…mework (#4330)
  • Loading branch information
juan-fernandez committed Jun 4, 2024
1 parent 8511979 commit 2439b33
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
30 changes: 30 additions & 0 deletions integration-tests/ci-visibility/test/selenium-no-framework.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { By, Builder } = require('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome')

async function run () {
const options = new chrome.Options()
options.addArguments('--headless')
const build = new Builder().forBrowser('chrome').setChromeOptions(options)
const driver = await build.build()

await driver.get(process.env.WEB_APP_URL)

await driver.getTitle()

await driver.manage().setTimeouts({ implicit: 500 })

const helloWorld = await driver.findElement(By.className('hello-world'))

await helloWorld.getText()

return driver.quit()
}

run()
.then(() => {
process.exit(0)
}).catch((err) => {
// eslint-disable-next-line no-console
console.error(err)
process.exit(1)
})
29 changes: 29 additions & 0 deletions integration-tests/selenium/selenium.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,34 @@ versionRange.forEach(version => {
})
})
})

it('does not crash when used outside a known test framework', (done) => {
let testOutput = ''
childProcess = exec(
'node ./ci-visibility/test/selenium-no-framework.js',
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
WEB_APP_URL: `http://localhost:${webAppPort}`,
TESTS_TO_RUN: '**/ci-visibility/test/selenium-test*'
},
stdio: 'pipe'
}
)

childProcess.on('exit', (code) => {
assert.equal(code, 0)
assert.notInclude(testOutput, 'InvalidArgumentError')
done()
})

childProcess.stdout.on('data', (chunk) => {
testOutput += chunk.toString()
})
childProcess.stderr.on('data', (chunk) => {
testOutput += chunk.toString()
})
})
})
})
19 changes: 13 additions & 6 deletions packages/datadog-instrumentations/src/selenium.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ addHook({
}, (seleniumPackage, seleniumVersion) => {
// TODO: do not turn this into async. Use promises
shimmer.wrap(seleniumPackage.WebDriver.prototype, 'get', get => async function () {
if (!ciSeleniumDriverGetStartCh.hasSubscribers) {
return get.apply(this, arguments)
}
let traceId
const setTraceId = (inputTraceId) => {
traceId = inputTraceId
Expand All @@ -40,15 +43,20 @@ addHook({
isRumActive
})

await this.manage().addCookie({
name: DD_CIVISIBILITY_TEST_EXECUTION_ID_COOKIE_NAME,
value: traceId
})
if (traceId && isRumActive) {
await this.manage().addCookie({
name: DD_CIVISIBILITY_TEST_EXECUTION_ID_COOKIE_NAME,
value: traceId
})
}

return getResult
})

shimmer.wrap(seleniumPackage.WebDriver.prototype, 'quit', quit => async function () {
if (!ciSeleniumDriverGetStartCh.hasSubscribers) {
return quit.apply(this, arguments)
}
const isRumActive = await this.executeScript(RUM_STOP_SESSION_SCRIPT)

if (isRumActive) {
Expand All @@ -58,10 +66,9 @@ addHook({
resolve()
}, DD_CIVISIBILITY_RUM_FLUSH_WAIT_MILLIS)
})
await this.manage().deleteCookie(DD_CIVISIBILITY_TEST_EXECUTION_ID_COOKIE_NAME)
}

await this.manage().deleteCookie(DD_CIVISIBILITY_TEST_EXECUTION_ID_COOKIE_NAME)

return quit.apply(this, arguments)
})

Expand Down

0 comments on commit 2439b33

Please sign in to comment.