diff --git a/src/clis/36kr/article.test.ts b/src/clis/36kr/article.test.ts new file mode 100644 index 00000000..00de82d6 --- /dev/null +++ b/src/clis/36kr/article.test.ts @@ -0,0 +1,41 @@ +import { beforeAll, describe, expect, it, vi } from 'vitest'; + +import { getRegistry } from '../../registry.js'; + +beforeAll(async () => { + await import('./article.js'); +}); + +describe('36kr/article', () => { + it('waits for capture before scraping the rendered article', async () => { + const command = getRegistry().get('36kr/article'); + expect(command?.func).toBeTypeOf('function'); + + const page = { + installInterceptor: vi.fn().mockResolvedValue(undefined), + goto: vi.fn().mockResolvedValue(undefined), + waitForCapture: vi.fn().mockResolvedValue(undefined), + wait: vi.fn().mockResolvedValue(undefined), + evaluate: vi.fn().mockResolvedValue({ + title: 'OpenCLI ships faster 36kr article scraping', + author: 'opencli', + date: '2026-03-31', + body: 'A useful article body', + }), + } as any; + + const result = await command!.func!(page, { id: 'https://www.36kr.com/p/321654987' }); + + expect(page.installInterceptor).toHaveBeenCalledWith('36kr.com/api'); + expect(page.goto).toHaveBeenCalledWith('https://www.36kr.com/p/321654987'); + expect(page.waitForCapture).toHaveBeenCalledWith(6); + expect(page.wait).toHaveBeenCalledWith({ selector: '.article-title, h1', timeout: 3 }); + expect(result).toEqual([ + { field: 'title', value: 'OpenCLI ships faster 36kr article scraping' }, + { field: 'author', value: 'opencli' }, + { field: 'date', value: '2026-03-31' }, + { field: 'url', value: 'https://36kr.com/p/321654987' }, + { field: 'body', value: 'A useful article body' }, + ]); + }); +}); diff --git a/src/clis/36kr/article.ts b/src/clis/36kr/article.ts index e8d24ef2..74765591 100644 --- a/src/clis/36kr/article.ts +++ b/src/clis/36kr/article.ts @@ -31,7 +31,8 @@ cli({ await page.installInterceptor('36kr.com/api'); await page.goto(`https://www.36kr.com/p/${articleId}`); - await page.wait(5); + await page.waitForCapture(6); + await page.wait({ selector: '.article-title, h1', timeout: 3 }); const data: any = await page.evaluate(` (() => { diff --git a/src/clis/sinafinance/rolling-news.ts b/src/clis/sinafinance/rolling-news.ts index 9e0cac9d..8f40f7b4 100644 --- a/src/clis/sinafinance/rolling-news.ts +++ b/src/clis/sinafinance/rolling-news.ts @@ -14,7 +14,7 @@ cli({ columns: ['column', 'title', 'date', 'url'], func: async (page, _args) => { await page.goto(`https://finance.sina.com.cn/roll/#pageid=384&lid=2519`); - await page.wait({ selector: '.d_list_txt li', timeout: 10000 }); + await page.wait({ selector: '.d_list_txt li', timeout: 10 }); const payload = await page.evaluate(` (() => {