javascript - 如何将列数据读入变量并在所有测试函数(单一规范)中共享,在 Test Cafe
问题描述
我在这里阅读了多篇关于读取表数据的文章,但是,每篇文章都是关于如何在单个测试函数中读取和使用该数据。如何读取表格,并在测试规范中的任何测试函数中使用它?
如果我将它写入一个测试函数,它可以正常工作并正确返回所有信息:
import { sAdmin } from "..authentication";
import TablePage from "../TablePage";
const tablePage = new TablePage();
fixture `A fixture`
.page`https://subdomain.example.com/#/table`
.beforeEach( async t => {
await t
.resizeWindow(1284, 722)
.useRole(sAdmin);
});
// this will work, but this won't share context with any other test function in the spec
// this should be initialized at the start to be able to verify search clears
test(`A test`, async t => {
const tableColumn = await tablePage.woNumber;
const tableCount = await tablePage.woNumber.count;
const cellData = [];
for (let i = 0; i < tableCount; i++) {
const text = await tableColumn.nth(i).innerText;
cellData.push(text);
}
console.log('in spec - tableCount', tableCount);
console.log('in spec - cellData', cellData);
});
两个控制台日志的输出都是正确的:
in spec - tableCount 4
in spec - cellData [ '0007', '0003', '0005', '0006' ]
我在我的测试规范和我的页面对象模型(POM)中尝试了一个异步函数。除非在测试函数中,否则异步函数在我的规范中不起作用。POM 中的那个可以工作,它会被调用,但是我做不到const tableCount = await tablePage.woNumber.count;
它会冲我大喊我不能使用这样的选择器。这是因为修饰符 .count。我将 .count 调整为在 for 循环内,但这只是返回了 undefined 或其他没有帮助的数据。
我的页面对象模型(TablePage)中的异步函数示例
async rowCount() {
const tableColumn = await tablePage.fooSelector;
const tableCount = await tablePage.fooSelector;
const cellData = [];
for (let i = 0; i < tableCount.count; i++) {
const text = await tableColumn.nth(i).innerText;
cellData.push(text);
}
console.log('in page - tableColumn', tableColumn);
console.log('in page - tableCount', tableCount);
console.log('in page - cellData', cellData);
return tableCount;
};
它在我的规范文件中被调用,但不确定在哪里调用它:
const count = tablePage.rowCount();
我需要在页面加载后运行它以获取单元格数据,并允许我至少在此规范文件中的所有测试中共享上下文。我更愿意把它放在我的 POM 中,这样它就可以在其他测试的其他地方使用。但是我会满足于它在我的测试规范中工作,而不是在测试函数中,因此它可以在规范文件中的所有测试中共享。
我试图做一个夹具上下文,但这也有问题并且返回未定义。这是我尝试过的带有上下文的之前,但没有用。
.before( async ctx => {
const tableColumn = await tablePage.fooSelector;
const tableCount = await tablePage.fooSelector;
for (let i = 0; i < tableCount.count; i++) {
const text = await tableColumn.nth(i).innerText;
ctx.cellData.push(text);
}
// console.log('in spec - tableCount', tableCount);
// console.log('in in spec - cellData', cellData);
})
这些控制台日志返回未定义或对象而不是文本。
任何帮助将不胜感激。以下是我已经引用的资源:
TestCafe - 将 Selector 的结果存储在变量中
https://testcafe.io/documentation/402670/reference/test-api/domnodestate
编辑:我仍在寻找一种方法来共享我获得的数据的上下文,我无法将数据返回到测试规范。也许如果我做更多的修补,我可以分享我获得的价值。
这是我不共享上下文的解决方案,但它确实让我可以防止每个测试函数中的代码重用。
页面对象模型
import { Selector, t } from "testcafe";
class TablePage{
constructor() {
this.searchInput = Selector('#searchInput');
this.tableCount = Selector('.class-selector');
};
async validateSearchResults(selector, searchText) {
await t
.typeText(this.searchInput, searchText)
.pressKey('enter');
const rowCount = await this.tableCount.count;
let searchResults = []
for (let i = 0; i < rowCount; i++) {
let text = await selector.nth(i).innerText;
searchResults.push(text);
await t.expect(searchResults[i]).contains(searchText);
}
};
}
export default TablePage;
规格文件
import { sAdmin } from "..authentication";
import TablePage from "../TablePage";
const tablePage = new TablePage();
fixture `Test search functionality`
.page`https://examplepage.com`
.beforeEach( async t => {
await t
.useRole(sAdmin)
});
test(`User can search via order number`, async t => {
await tablePage.validateSearchResults(tablePage.tableCount, 'foo');
});
解决方案
const tableCount = await tablePage.selector.count;
看起来正确,应该可以工作。此外,需要使用await
关键字调用 async 方法:
const count = await tablePage.rowCount();
以下是类似方法的示例:
表页.js:
import { Selector } from 'testcafe';
export class TablePage {
constructor () {
this.tableCells = Selector('#ContentHolder_grid_DXDataRow0 td');
this.cellData = [];
this.cellCount = 0;
}
async initCellData () {
this.cellCount = await this.tableCells.count;
for (let i = 0; i < this.cellCount; i++) {
const text = await this.tableCells.nth(i).innerText;
this.cellData.push(text);
}
}
}
测试.js:
import { TablePage } from './table-page';
let page = null;
fixture `New Fixture`
.page `https://demos.devexpress.com/ASPxGridViewDemos/DataBinding/QueryBuilderControl.aspx`
.beforeEach(async () => {
page = new TablePage();
await page.initCellData();
});
test(`New Test`, async t => {
console.log('cells count: ', page.cellCount);
console.log('cells data: ', page.cellData);
});
推荐阅读
- php - ldap_modify 不工作 PHP
- python - JSON dict将键保存为字符串而不是int
- php - 数组值不适用于 preg_replace
- javascript - 如何在 JQuery 中获取总数
- npm - 在没有 Webpack 的情况下从 NPM 模块解析 Sass @import
- jquery - Bootstrap v4(Tempus Dominus)的 Eonasdan DateTimePicker 不工作
- java - BarcodeDetector 不起作用并返回 false
- hyperledger - 什么是新颖的共识算法
- angular6 - 如何在 rxjs 的地图运算符中以角度处理 array.map
- spring - 我们可以为同一个bean创建两个bean id吗