如何使用TestCafe访问应用程序操作?

Yul*_*lia 2 javascript automated-tests web-testing e2e-testing testcafe

我试图寻找答案,但找不到。我想编写一个函数来删除以前使用的测试组织,然后再在testcafe中开始测试。

如果要通过UI进行操作,这将是一个非常耗时的操作。因此,我想知道是否可以使用应用程序操作并编写一个函数删除我的测试数据?

我的想法是执行以下步骤:1.找到要删除的所有测试组织2.遍历每个测试组织,调用ShowDeleteOrgModal()方法,然后再调用DeleteOrganisation()方法。

我看到其他测试工具使用window()提供对应用程序操作的访问。有什么办法可以在testCafe中实现它?

按钮选择器如下所示。

<button class="button_class" onclick="OurApplicationName.ShowDeleteOrgModal('organisation_id');
return false;">Delete Organisation</button>
Run Code Online (Sandbox Code Playgroud)

我们以这种方式在柏树中实现了类似的想法:

CleanUpOrgs() {
         cy.window().then((win) => {
         let numberOfOrgs = win.window.$('tr:contains(' + Cypress.env('testOrgName') + ')').length;
            while (numberOfOrgs > 0) {
                cy.get('table').contains('tr', Cypress.env('testOrgName')).then(elem => {
                    let orgId = elem[0].id.replace('OurApplicationName_', '');
                    cy.window().then((win) => {
                        win.window.OurApplicationName.ShowDeleteOrgModal(orgId);
                        win.window.OurApplicationName.DeleteOrganisation();
                        cy.wait(2000);
                    });
                });
                numberOfOrgs--;
            }
        });
    },
Run Code Online (Sandbox Code Playgroud)

如何使用TestCafe访问窗口?

Ars*_*sov 7

尝试使用ClientFunction。例如,您可以使用以下代码打开模态:

import { ClientFunction } from 'testcafe';

const showDeleteOrgModal = ClientFunction(organizationId => {
    OurApplicationName.ShowDeleteOrgModal(organizationId);
});

fixture`My fixture`
    .page`http://www.example.com/`;

test('My Test', async t => {
    await showDeleteOrgModal('organisation_id');
});
Run Code Online (Sandbox Code Playgroud)

您也可以通过这种方式在客户端运行异步代码


更新我无法访问测试页面,无法为您提供确切的测试。但我创建了一个示例,该测试看起来如何

import { ClientFunction } from 'testcafe';

const showDeleteOrgModal = ClientFunction(organizationId => {
    OurApplicationName.ShowDeleteOrgModal(organizationId);
});

fixture`My fixture`
    .page`http://www.example.com/`;

test('My Test', async t => {
    await showDeleteOrgModal('organisation_id');
});
Run Code Online (Sandbox Code Playgroud)

我使用了ClientFunctionsSelectors和配置文件作为testOrgName变量(您可以在FAQ中了解有关使用配置和环境变量的更多信息)。

  • 感谢您的回答。我今天将尝试这种方法! (2认同)