Testing 用于匹配节点和所有子体的自定义选择器
这是一个具体的问题。 我希望能够基于元素本身的属性值或子体中的任何位置选择元素。主要的要求是不需要异步调用就可以做到这一点 基本上,我正在尝试写以下内容:Testing 用于匹配节点和所有子体的自定义选择器,testing,automation,automated-tests,e2e-testing,testcafe,Testing,Automation,Automated Tests,E2e Testing,Testcafe,这是一个具体的问题。 我希望能够基于元素本身的属性值或子体中的任何位置选择元素。主要的要求是不需要异步调用就可以做到这一点 基本上,我正在尝试写以下内容: const findByTestName = (context: Selector, testName: string): Selector => { // if context itself matches [data-test~='${testName}'] return context; // return con
const findByTestName = (context: Selector, testName: string): Selector => {
// if context itself matches [data-test~='${testName}'] return context;
// return context.find('[data-test~='${testName}']');
};
可能吗
编辑:
为了提供更多的上下文:我本可以尝试使用css查询解决它,比如someid[data test='testname'],someid[data test='testname'],但我尝试创建嵌套页面模型,如下所示:
class Page {
constructor() {
this.root = new Selector('[data-test="page"]');
this.button = new Button(findByTestName(this.root, 'page__button'));
}
}
class Button {
constructor(context: Selector) {
this.root = findByTestName(context, 'button');
this.label = findByTestName(this.root, 'button__label');
}
}
其中DOM配置可以如下所示:
<div id="page">
<div data-test="page__button button">
<span data-test="button__label" />
</div>
</div>
或者这个。取决于实际布局
<div id="page">
<div data-test="page__button">
<div data-test="someotherstuff">
<div data-test="button>
<span data-test="button__label" />
</div>
</div>
</div>
</div>
这样,按钮页面模型将是一个可重用的东西,可以应用于任何按钮。您需要使用该方法。
请参见一个示例:
从“testcafe”导入{Selector};
固定装置`
.page'/index.html';
const findByTestName=SelectortestName=>{
var checkElement=函数el,checkedAttrValue,结果{
var testNameAttributeVale=el.getAttribute'data-test';
如果testNameAttributeVale===checkedAttrValue
结果:普希尔;
返回结果;
};
var rootElement=上下文;
var结果=[];
做{
checkElementrootElement;
rootElement=rootElement.parentElement;
}
同时进行远程医疗;
返回结果;
},{依赖项:{上下文:选择器'body'}};
测试'test',异步t=>{
const elms1=wait findByTestName'button';
const elms2=wait findByTestName.with{
依赖项:{context:Selector'div[data test=button]}
}“其他东西”;
};
你需要使用这个方法。
请参见一个示例:
从“testcafe”导入{Selector};
固定装置`
.page'/index.html';
const findByTestName=SelectortestName=>{
var checkElement=函数el,checkedAttrValue,结果{
var testNameAttributeVale=el.getAttribute'data-test';
如果testNameAttributeVale===checkedAttrValue
结果:普希尔;
返回结果;
};
var rootElement=上下文;
var结果=[];
做{
checkElementrootElement;
rootElement=rootElement.parentElement;
}
同时进行远程医疗;
返回结果;
},{依赖项:{上下文:选择器'body'}};
测试'test',异步t=>{
const elms1=wait findByTestName'button';
const elms2=wait findByTestName.with{
依赖项:{context:Selector'div[data test=button]}
}“其他东西”;
};
您甚至可以将Selector.find等筛选函数生成的选择器作为依赖项传递,以同步创建复杂选择器: 从“testcafe”导入{Selector}; const findByTestName=root,testName=>{ const children=root.find`[data test=${testName}]`; const context={root:root,children:children,testName:testName}; 返回选择器=>{ 常量元素=根; if element.getAttribute'data-test'==testName 返回el; 返回儿童; },{依赖关系:上下文}; } fixture`Selector`.page`./test.html`; 测试“依赖项”,异步t=>{ 常量根=选择器“[数据测试=页面按钮]”; console.logawait findByTestNameroot,button\u label.tagName; }; 请注意,您可以删除findByTestName函数中的所有常量,但速记属性: const findByTestName=root,testName=>Selector=>{ //... }, { 依赖项:{ 根:根, 孩子们:根。找到。。。 //... } ;
您甚至可以将Selector.find等筛选函数生成的选择器作为依赖项传递,以同步创建复杂选择器: 从“testcafe”导入{Selector}; const findByTestName=root,testName=>{ const children=root.find`[data test=${testName}]`; const context={root:root,children:children,testName:testName}; 返回选择器=>{ 常量元素=根; if element.getAttribute'data-test'==testName 返回el; 返回儿童; },{依赖关系:上下文}; } fixture`Selector`.page`./test.html`; 测试“依赖项”,异步t=>{ 常量根=选择器“[数据测试=页面按钮]”; console.logawait findByTestNameroot,button\u label.tagName; }; 请注意,您可以删除findByTestName函数中的所有常量,但速记属性: const findByTestName=root,testName=>Selector=>{ //... }, { 依赖项:{ 根:根, 孩子们:根。找到。。。 //... } ;
将findByTestName定义为一个单独的函数并将其与诸如:const findByTestName=context、testName=>Selectortn=>{}、{dependencies:context}之类的函数一起使用有什么好处吗testName;?将findByTestName定义为一个单独的函数并将其与诸如:const findByTestName=co之类的函数一起使用有什么好处吗 ntext,testName=>Selectortn=>{},{dependencies:context}testName?