如何将列数据读入变量并在 Test Cafe 中的所有测试函数(单一规范)中共享它
How to read a columns data into a variable and share it in all test functions (single spec), in Test Cafe
我在这里阅读了多个 post 关于读取 table 数据的内容,但是,每个 post 都是如何在单个测试函数中读取和使用该数据的。如何读取 table 并在测试规范中的任何测试函数中使用它?
如果我将其写入测试函数,它会工作并正确返回所有信息:
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 循环内,但只是 returned 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 中,这样它就可以在其他测试的其他地方使用。但我愿意让它在我的测试规范中工作,而不是在测试函数中,这样它就可以在规范文件中的所有测试中共享。
我试过做一个夹具上下文,但也有问题并且 returned 未定义。这是我尝试过的带有上下文的之前,但没有用。
.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);
})
这些控制台日志 return 未定义,或对象而不是文本。
如有任何帮助,我们将不胜感激。以下是我已经引用的资源:
How do I can get a text of all the cells of the table using testcafe
https://testcafe.io/documentation/402670/reference/test-api/domnodestate
编辑:我仍在寻找一种方法来共享我获得的数据的上下文,我无法 return 将数据返回到测试规范。也许如果我做更多的修补,我可以分享我获得的价值。
这是我的解决方案,它不共享上下文,但它确实让我可以防止在每个测试函数中重用代码。
页面对象模型
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
关键字调用异步方法:
const count = await tablePage.rowCount();
这是一个类似方法的例子:
table-page.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);
}
}
}
test.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);
});
我在这里阅读了多个 post 关于读取 table 数据的内容,但是,每个 post 都是如何在单个测试函数中读取和使用该数据的。如何读取 table 并在测试规范中的任何测试函数中使用它?
如果我将其写入测试函数,它会工作并正确返回所有信息:
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 循环内,但只是 returned 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 中,这样它就可以在其他测试的其他地方使用。但我愿意让它在我的测试规范中工作,而不是在测试函数中,这样它就可以在规范文件中的所有测试中共享。
我试过做一个夹具上下文,但也有问题并且 returned 未定义。这是我尝试过的带有上下文的之前,但没有用。
.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);
})
这些控制台日志 return 未定义,或对象而不是文本。
如有任何帮助,我们将不胜感激。以下是我已经引用的资源:
How do I can get a text of all the cells of the table using testcafe
https://testcafe.io/documentation/402670/reference/test-api/domnodestate
编辑:我仍在寻找一种方法来共享我获得的数据的上下文,我无法 return 将数据返回到测试规范。也许如果我做更多的修补,我可以分享我获得的价值。
这是我的解决方案,它不共享上下文,但它确实让我可以防止在每个测试函数中重用代码。
页面对象模型
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
关键字调用异步方法:
const count = await tablePage.rowCount();
这是一个类似方法的例子:
table-page.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);
}
}
}
test.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);
});