Skip to content

Commit def42c4

Browse files
authored
Update Test and Issue Testing (#86)
* Update Test * Initial Implementation * Updates * Update Logging * Convert Tests to Module * Update Test Paths
1 parent 7491be9 commit def42c4

8 files changed

Lines changed: 328 additions & 165 deletions

File tree

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"sourceType": "module"
1212
},
1313
"rules": {
14-
"no-undef": "off"
14+
"no-undef": "off",
15+
"no-extra-semi": "off"
1516
}
1617
}

.github/workflows/issue.yaml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: "Issue"
2+
3+
on:
4+
issues:
5+
types:
6+
- opened
7+
8+
jobs:
9+
issue:
10+
name: "Issue"
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 5
13+
14+
steps:
15+
- name: "Checkout"
16+
uses: actions/checkout@v4
17+
18+
- name: "Debug Issue"
19+
run: |
20+
echo Issue number: '${{ github.event.issue.number }}'
21+
echo Issue title: '${{ github.event.issue.title }}'
22+
echo Issue body: '${{ github.event.issue.body }}'
23+
24+
- name: "Setup Node"
25+
uses: actions/setup-node@v4
26+
with:
27+
node-version: 20
28+
29+
- name: "Parse Issue"
30+
id: issue
31+
uses: cssnr/parse-issue-form-action@master
32+
with:
33+
body: ${{ github.event.issue.body }}
34+
35+
- name: "Debug Parsed Issue"
36+
run: |
37+
echo Site Link: '${{ steps.issue.outputs.site_link }}'
38+
echo Details: '${{ steps.issue.outputs.details }}'
39+
echo Support Information: '${{ steps.issue.outputs.support_information }}'
40+
41+
- name: "Install"
42+
run: |
43+
npm install
44+
45+
- name: "Process Issue"
46+
env:
47+
URL: ${{ steps.issue.outputs.site_link }}
48+
run: |
49+
npm run issue
50+
51+
- name: "Debug Files"
52+
run: |
53+
ls -lah tests/screenshots
54+
55+
- name: "Upload Image"
56+
id: image
57+
uses: McCzarny/[email protected]
58+
with:
59+
path: tests/screenshots/links.png
60+
uploadMethod: imgbb
61+
apiKey: ${{ secrets.IMGBB_API_KEY }}
62+
63+
- name: "Read Logs"
64+
id: logs
65+
uses: juliangruber/read-file-action@v1
66+
with:
67+
path: tests/screenshots/logs.txt
68+
69+
- name: "Debug Logs"
70+
run: echo "${{ steps.logs.outputs.content }}"
71+
72+
- name: "Add Comment"
73+
run: gh issue comment "$NUMBER" --body "$BODY"
74+
env:
75+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
76+
GH_REPO: ${{ github.repository }}
77+
NUMBER: ${{ github.event.issue.number }}
78+
BODY: |
79+
Link Extractor Results for: [${{ steps.issue.outputs.site_link }}](${{ steps.issue.outputs.site_link }})
80+
81+
![0](${{steps.image.outputs.url}})
82+
83+
Logs:
84+
```json
85+
${{ steps.logs.outputs.content }}
86+
```

.github/workflows/test.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ on:
55
branches: ["master"]
66
paths:
77
- "src/**"
8+
- "tests/**"
89
pull_request:
910
branches: ["master"]
1011
paths:
1112
- "src/**"
13+
- "tests/**"
1214
workflow_dispatch:
1315

1416
jobs:

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
"name": "link-extractor",
33
"scripts": {
44
"postinstall": "npx gulp",
5-
"lint:eslint": "npx eslint src/js",
5+
"lint:eslint": "npx eslint src/js tests/*.mjs",
66
"lint:web-ext": "npm run manifest:firefox && npx web-ext lint --source-dir ./src/ --ignore-files dist/**",
77
"lint": "npm run lint:eslint && npm run lint:web-ext",
8-
"test": "npx json-merger -p -am concat -o src/manifest.json manifest.json tests/manifest-test.json && node tests/test.js",
8+
"test": "npm run manifest:test && node tests/test.mjs",
9+
"issue": "npm run manifest:test && node tests/issue.mjs",
910
"chrome": "npm run manifest:chrome && web-ext run --source-dir ./src/ --target=chromium",
1011
"firefox": "npm run manifest:firefox && web-ext run --source-dir ./src/",
1112
"manifest:chrome": "npx json-merger -p -am concat -o src/manifest.json manifest.json manifest-chrome.json",
1213
"manifest:firefox": "npx json-merger -p -am concat -o src/manifest.json manifest.json manifest-firefox.json",
14+
"manifest:test": "npx json-merger -p -am concat -o src/manifest.json manifest.json tests/manifest-test.json",
1315
"build:chrome": "npm run manifest:chrome && npx web-ext build -n {name}-chrome-{version}.zip -o -s src",
1416
"build:firefox": "npm run manifest:firefox && npx web-ext build -n {name}-firefox-{version}.zip -o -s src",
1517
"build": "npm run build:chrome && npm run build:firefox"

tests/common.mjs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as puppeteer from 'puppeteer'
2+
import * as path from 'path'
3+
4+
const sourceDir = 'src'
5+
const timeout = 10000
6+
7+
/**
8+
* @function getBrowser
9+
* @return {puppeteer.Browser}
10+
*/
11+
export async function getBrowser() {
12+
const pathToExtension = path.join(process.cwd(), sourceDir)
13+
console.log('pathToExtension:', pathToExtension)
14+
return await puppeteer.launch({
15+
args: [
16+
`--disable-extensions-except=${pathToExtension}`,
17+
`--load-extension=${pathToExtension}`,
18+
// '--disable-blink-features=AutomationControlled',
19+
// '--disable-features=ChromeUserPermPrompt',
20+
],
21+
dumpio: true,
22+
// headless: false,
23+
// slowMo: 50,
24+
})
25+
}
26+
27+
/**
28+
* @function getWorker
29+
* @global browser
30+
* @global timeout
31+
* @return {puppeteer.Page}
32+
*/
33+
export async function getWorker(browser) {
34+
const workerTarget = await browser.waitForTarget(
35+
(target) =>
36+
target.type() === 'service_worker' &&
37+
target.url().endsWith('service-worker.js'),
38+
{ timeout }
39+
)
40+
return await workerTarget.worker()
41+
}
42+
43+
/**
44+
* @function getPage
45+
* @global browser
46+
* @global timeout
47+
* @param {puppeteer.Browser} browser
48+
* @param {String} name
49+
* @param {Boolean=} log
50+
* @param {String=} size
51+
* @return {puppeteer.Page}
52+
*/
53+
export async function getPage(browser, name, log, size) {
54+
console.debug(`getPage: ${name}`, log, size)
55+
const target = await browser.waitForTarget(
56+
(target) => target.type() === 'page' && target.url().includes(name),
57+
{ timeout }
58+
)
59+
const newPage = await target.asPage()
60+
await newPage.emulateMediaFeatures([
61+
{ name: 'prefers-color-scheme', value: 'dark' },
62+
])
63+
newPage.setDefaultTimeout(timeout)
64+
if (size) {
65+
const [width, height] = size.split('x').map((x) => parseInt(x))
66+
await newPage.setViewport({ width, height })
67+
}
68+
if (log) {
69+
console.debug(`Adding Logger: ${name}`)
70+
newPage.on('console', (msg) =>
71+
console.log(`console: ${name}:`, msg.text())
72+
)
73+
}
74+
return newPage
75+
}
76+
77+
export async function scrollPage(page) {
78+
await page.evaluate(() => {
79+
window.scrollBy({
80+
top: window.innerHeight,
81+
left: 0,
82+
behavior: 'instant',
83+
})
84+
})
85+
await new Promise((resolve) => setTimeout(resolve, 500))
86+
}

tests/issue.mjs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import * as fs from 'fs'
2+
import { getBrowser, getPage, getWorker } from './common.mjs'
3+
4+
const screenshotsDir = 'tests/screenshots'
5+
let page
6+
7+
async function addLogger(page, results, name) {
8+
page.on('console', async (msg) => {
9+
const text = msg.text()
10+
if (text.startsWith('links:')) {
11+
const value = await msg.args()[1].jsonValue()
12+
results.unshift(`links count: ${value.length}`)
13+
} else if (text.startsWith('domains:')) {
14+
const value = await msg.args()[1].jsonValue()
15+
results.unshift(`domains count: ${value.length}`)
16+
} else {
17+
results.push(`${name}: ${text}`)
18+
}
19+
})
20+
}
21+
22+
;(async () => {
23+
fs.rmSync(screenshotsDir, { recursive: true, force: true })
24+
fs.mkdirSync(screenshotsDir)
25+
26+
console.log('process.env.URL:', process.env.URL)
27+
const url = new URL(process.env.URL)
28+
console.log('url.href:', url.href)
29+
30+
// Get Browser
31+
const browser = await getBrowser()
32+
console.log('browser:', browser)
33+
34+
// Get Service Worker
35+
const worker = await getWorker(browser)
36+
console.log('worker:', worker)
37+
38+
const logs = []
39+
40+
page = await browser.newPage()
41+
// page.on('console', (msg) => logs.push(msg.text()))
42+
await addLogger(page, logs, 'site')
43+
await page.goto(url.href)
44+
await page.bringToFront()
45+
await page.waitForNetworkIdle()
46+
await new Promise((resolve) => setTimeout(resolve, 1000))
47+
48+
await worker.evaluate('chrome.action.openPopup();')
49+
page = await getPage(browser, 'popup.html')
50+
// page.on('console', (msg) => logs.push(msg.text()))
51+
await addLogger(page, logs, 'popup')
52+
await page.locator('a[data-filter=""]').click()
53+
54+
page = await getPage(browser, 'links.html', false, '768x1024')
55+
// page.on('console', (msg) => logs.push(msg.text()))
56+
await addLogger(page, logs, 'links')
57+
await page.waitForNetworkIdle()
58+
await page.screenshot({ path: `${screenshotsDir}/links.png` })
59+
60+
await browser.close()
61+
62+
console.log('logs:', logs)
63+
const content = JSON.stringify(logs)
64+
fs.writeFileSync(`${screenshotsDir}/logs.txt`, content)
65+
})()

0 commit comments

Comments
 (0)