我决定随机选择一个站点进行测试,所以在本例中,它是 www.nike.com
测试范围:
- 前往 www.nike.com
- 在页面右上角的快速搜索中输入“男士”。
- 回车
- 验证搜索结果是否大于 10
测试.js
fixture `Nike.com`// declare the fixture
.page `www.nike.com`; // specify the start page
const nameInput = Selector('.product-card.css-ucpg4q.ncss-col-sm-6.ncss-col-lg-4.va-sm-t.product-grid__card');
const searchGlass = Selector('.g72-search.fs20-nav-sm');
//then create a test and place your code there
test('Scenario-1: Validate Quick Search Functionality for Men', async t => {
await t
.maximizeWindow()
.typeText('#TypeaheadSearchInput', 'Men')
.pressKey('enter');
//.click(searchGlass);
const nameInputElement = await nameInput.with({ visibilityCheck: true })();
await t
.takeScreenshot()
// Use the assertion to check if the actual header text is equal to the expected one
.expect(Selector('.product-card.css-ucpg4q.ncss-col-sm-6.ncss-col-lg-4.va-sm-t.product-grid__card').count).gt(10, 'Search results must be more than 10');
});
Jenkins 文件
stage('Install') {
agent {
docker {
image 'node:10.11'
reuseNode true
}
}
environment {
HOME = '.'
}
steps {
sh 'npm config set unsafe-perm true'
sh 'npm install testcafe-reporter-html'
}
}
stage('Test'){
agent {
docker {
image 'testcafe/testcafe:alpha'
args '-e NODE_PATH=./node_modules'
args '--entrypoint=\'\''
reuseNode true
}
}
steps{
sh "node_modules/.bin/testcafe 'chromium:headless --no-sandbox --disable-dev-shm-usage' ./tests/test.js -s takeOnFails=true -r html:reports/results.html"
}
}
问题:
KeyPress('Enter') 不工作。在快速搜索中输入了文本,但未按下回车键。我能够通过截屏来验证这一点。
作为变通方法,我考虑避免按键,而是在 test.js
中的搜索图标(我共享的代码片段中的搜索玻璃)上单击鼠标
**虽然测试可以点击搜索图标,但实际上,在 html 报告中给出了错误**
页面“https://www.nike.com/w?q=Men&vst=Men”上的错误:
未捕获的类型错误:无法读取 null 的“已取消”属性
浏览器:HeadlessChrome 77.0.3865/Linux 0.0.
奇怪的是,我的笔记本电脑上运行的 testcafe docker 一切正常。 HTML 报告也显示了预期的结果。我似乎无法找到为什么通过 testcafe docker 代理从 Jenkins 运行测试时会出现奇怪的行为。
最佳答案
经过几次实验和试错,我解决了这个问题。我必须将 args '--net=host -e DISPLAY=":0"' 传递给 docker 代理。
如果有人感兴趣,我正在复制整个 jenkins 文件。
pipeline {
agent any
stages {
stage('Install') {
agent {
docker {
image 'node:10.11'
reuseNode true
}
}
environment {
HOME = '.'
}
steps {
sh 'npm config set unsafe-perm true'
sh 'npm install testcafe-reporter-html'
sh 'npm install testcafe-reporter-junit'
}
}
stage('Test'){
agent {
docker {
image 'testcafe/testcafe'
args '--net=host -e DISPLAY=":0"'
args '-e NODE_PATH=./node_modules'
args '--entrypoint=\'\''
reuseNode true
}
}
steps{
sh "node_modules/.bin/testcafe 'chromium:headless --no-sandbox --disable-dev-shm-usage' ./tests/test.js -s -r html:results.html,junit:report.xml"
}
}
}
post {
always {
echo 'Use this stage to do post run activities'
deleteDir() /* clean up our workspace */
}
}
}
关于docker - 在 Jenkins 中将 testcafe 设置为 docker 代理时,KeyPress 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58810367/