Skip to content

Commit

Permalink
test: better helpers (#462)
Browse files Browse the repository at this point in the history
* test: better helpers

* refactor: use inline http server

instead of httpbin.org, it's slow

* refactor: unify pattern

* refactor: remove/add deps

* refactor: remove only

* test: skip inconsistent case
  • Loading branch information
Kikobeats committed May 17, 2023
1 parent 911c5ac commit b976152
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 160 deletions.
25 changes: 12 additions & 13 deletions packages/browserless/test/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
'use strict'

const { initBrowser, getBrowser } = require('@browserless/test/util')
const http = require('http')
const { createBrowser, getBrowserContext, getBrowser } = require('@browserless/test/util')
const { request, createServer } = require('http')

const test = require('ava')

const browser = getBrowser()

require('@browserless/test')(browser)
require('@browserless/test')(getBrowser())

test('pass specific options to a context', async t => {
const proxiedRequestUrls = []

const proxy = http
.createServer((req, res) => {
const serverUrl = (() => {
const server = createServer((req, res) => {
proxiedRequestUrls.push(req.url)

const proxyRequest = http.request(
const proxyRequest = request(
req.url,
{
method: req.method,
Expand All @@ -29,21 +27,22 @@ test('pass specific options to a context', async t => {
)

req.pipe(proxyRequest, { end: true })
})
.listen()
}).listen()

const proxyServer = `http://[::]:${proxy.address().port}`
return `http://[::]:${server.address().port}`
})()

const browserless = await browser.createContext({ proxyServer })
const browserless = await getBrowserContext(t, { proxyServer: serverUrl })
const page = await browserless.page()
t.teardown(() => page.close())

await browserless.goto(page, { url: 'http://example.com' })

t.deepEqual(proxiedRequestUrls, ['http://example.com/'])
})

test('ensure to destroy browser contexts', async t => {
const browserlessFactory = initBrowser()
const browserlessFactory = createBrowser()

const browser = await browserlessFactory.browser()

Expand Down
21 changes: 8 additions & 13 deletions packages/goto/test/e2e/evasions.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict'

const { getBrowser } = require('@browserless/test/util')
const { getBrowserContext } = require('@browserless/test/util')
const fpscanner = require('fpscanner')
const pWaitFor = require('p-wait-for')
const test = require('ava')

const browser = getBrowser()

test('arh.antoinevastel.com/bots/areyouheadless', async t => {
let assertion = false

const fn = async () => {
const browserless = await browser.createContext()
const browserless = await getBrowserContext(t)
const content = await browserless.text('https://arh.antoinevastel.com/bots/areyouheadless')
await browserless.destroyContext()
assertion = content.includes('You are not Chrome headless')
Expand All @@ -28,7 +26,7 @@ test('fingerprintjs', async t => {

const fn = async () => {
const getFingerprint = async userAgent => {
const browserless = await browser.createContext()
const browserless = await getBrowserContext(t)
const fingerprint = await browserless.evaluate(page =>
page.evaluate("document.querySelector('.giant').innerText")
)
Expand Down Expand Up @@ -62,7 +60,7 @@ test('fingerprintjs', async t => {

test('fpscanner', async t => {
const waitForAssertion = async () => {
const browserless = await browser.createContext()
const browserless = await getBrowserContext(t)
const getFingerprint = browserless.evaluate(page =>
page.evaluate('fpCollect.generateFingerprint()')
)
Expand All @@ -84,8 +82,7 @@ test('fpscanner', async t => {
})

test('bot.sannysoft.com', async t => {
const browserless = await browser.createContext()
t.teardown(browserless.destroyContext)
const browserless = await getBrowserContext(t)

const getReport = browserless.evaluate(page =>
page.evaluate(() => {
Expand Down Expand Up @@ -128,16 +125,14 @@ test('bot.sannysoft.com', async t => {
})
})

test('amiunique.org/fp', async t => {
const browserless = await browser.createContext()
t.teardown(browserless.destroyContext)
test.skip('amiunique.org/fp', async t => {
const browserless = await getBrowserContext(t)
const content = await browserless.text('https://amiunique.org/fp', { waitForTimeout: 3000 })
t.true(content.includes('You are unique'))
})

test('browserleaks.com/webgl', async t => {
const browserless = await browser.createContext()
t.teardown(browserless.destroyContext)
const browserless = await getBrowserContext(t)
const getGpuInfo = browserless.evaluate(page =>
page.evaluate(() => {
return {
Expand Down
16 changes: 4 additions & 12 deletions packages/goto/test/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
'use strict'

const { getBrowser } = require('@browserless/test/util')
const { getBrowserContext } = require('@browserless/test/util')
const test = require('ava')

const browser = getBrowser()

test('setup `scripts`', async t => {
const browserless = await browser.createContext()

t.teardown(browserless.destroyContext)
const browserless = await getBrowserContext(t)

const getVersion = browserless.evaluate(async page => page.evaluate('jQuery.fn.jquery'))

Expand All @@ -20,9 +16,7 @@ test('setup `scripts`', async t => {
})

test('setup `modules`', async t => {
const browserless = await browser.createContext()

t.teardown(browserless.destroyContext)
const browserless = await getBrowserContext(t)

const getVersion = browserless.evaluate(async page => page.evaluate('jQuery.fn.jquery'))

Expand All @@ -34,9 +28,7 @@ test('setup `modules`', async t => {
})

test('setup `styles`', async t => {
const browserless = await browser.createContext()

t.teardown(browserless.destroyContext)
const browserless = await getBrowserContext(t)

const getStyle = browserless.evaluate(async page =>
page.evaluate('window.getComputedStyle(document.body).fontFamily')
Expand Down
Loading

0 comments on commit b976152

Please sign in to comment.