-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ai-model): remove dom info in assertion to make it reliable (#284)
--------- Co-authored-by: zhouxiao.shaw <zhouxiao.shaw@bytedance.com>
- Loading branch information
1 parent
69ce6ec
commit 4cad2e1
Showing
6 changed files
with
135 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/midscene/tests/ai/evaluate/ai-data/assertion/online_order.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"testDataPath": "test-data/online_order", | ||
"testCases": [ | ||
{ | ||
"prompt": "the 'select option' button is yellow", | ||
"expected": true | ||
}, | ||
{ | ||
"prompt": "there are three tabs in the page, named 'Menu', 'Reviews', 'Merchant'", | ||
"expected": true | ||
}, | ||
{ | ||
"prompt": "the 'select option' button is blue", | ||
"expected": false | ||
}, | ||
{ | ||
"prompt": "there are three tabs in the page, named 'Home', 'Order', 'Profile'", | ||
"expected": false | ||
}, | ||
{ | ||
"prompt": "there is a shopping bag icon on the top right of the page", | ||
"expected": true | ||
}, | ||
{ | ||
"prompt": "The shopping bag icon on the top left of the page", | ||
"expected": false | ||
}, | ||
{ | ||
"prompt": "There is a homepage icon on the top right of the page", | ||
"expected": false | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { readFileSync } from 'node:fs'; | ||
import path from 'node:path'; | ||
import { describe } from 'node:test'; | ||
import { AiAssert } from '@/ai-model'; | ||
import { afterAll, expect, test } from 'vitest'; | ||
import { | ||
type InspectAiTestCase, | ||
getPageTestData, | ||
repeatFile, | ||
} from './test-suite/util'; | ||
import 'dotenv/config'; | ||
|
||
const repeatTime = 2; | ||
const testSources = [ | ||
// 'todo', | ||
'online_order', | ||
// 'online_order_list', | ||
// 'taobao', | ||
// 'aweme_login', | ||
// 'aweme_play', | ||
]; | ||
|
||
describe('ai inspect element', () => { | ||
const testResult: { | ||
path: string; | ||
result: { | ||
score: number; | ||
averageTime: string; | ||
successCount: number; | ||
failCount: number; | ||
}; | ||
}[] = []; | ||
|
||
afterAll(async () => { | ||
console.table( | ||
testResult.map((r) => { | ||
return { | ||
path: r.path, | ||
...r.result, | ||
}; | ||
}), | ||
); | ||
}); | ||
repeatFile(testSources, repeatTime, (source, repeatIndex) => { | ||
const aiDataPath = path.join(__dirname, `ai-data/assertion/${source}.json`); | ||
const aiData = JSON.parse( | ||
readFileSync(aiDataPath, 'utf-8'), | ||
) as InspectAiTestCase; | ||
|
||
aiData.testCases.forEach((testCase, index) => { | ||
const prompt = testCase.prompt; | ||
test( | ||
`${source}-${repeatIndex}: assertion-${prompt.slice(0, 30)}...`, | ||
async () => { | ||
const { context } = await getPageTestData( | ||
path.join(__dirname, aiData.testDataPath), | ||
); | ||
|
||
const { prompt, expected } = testCase; | ||
const result = await AiAssert({ | ||
assertion: prompt, | ||
context, | ||
}); | ||
|
||
expect(typeof result?.content?.pass).toBe('boolean'); | ||
if (result?.content?.pass !== expected) { | ||
throw new Error( | ||
`assertion failed: ${prompt} expected: ${expected}, actual: ${result?.content?.pass}, thought: ${result?.content?.thought}`, | ||
); | ||
} | ||
|
||
console.log('assertion passed, thought:', result?.content?.thought); | ||
}, | ||
{ | ||
timeout: 30 * 1000, | ||
}, | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters