electron 如何使用Playwright在电子桌面应用程序上进行E2E测试

iqih9akk  于 2023-01-28  发布在  Electron
关注(0)|答案(1)|浏览(1011)

我试着用剧作家写E2E测试,但出了问题。
当我初始化测试时,测试通过了,但实际上,块没有进入括号内。当我输入错误的选择器时,它也通过了测试。

  • 代码如下:*
import { _electron as electron } from 'playwright';
import { test, expect, ElectronApplication, Page, BrowserContext, Locator } from '@playwright/test';

test.describe('Add Connection', async() => {
let electronApp: ElectronApplication;
let firstWindow: Page;
let context: BrowserContext;

    test.beforeAll(async() => {
        electronApp = await electron.launch({ args: ['.']} );
    
        const appPath = await electronApp.evaluate(async({ app }) => {
            return app.getAppPath();
        });
        console.log(appPath);
    });
    
    test('Try Connection', () => {
        electronApp.on('window', async(page) => {
    
            await page.getByTestId('settings').click({delay: 1000});
            await page.getByTestId('connection').click({delay: 1000});   
    
        });
        
    });
    
    test.afterAll(async() => {
        await electronApp.close();
    });

});

我还没有看到足够的文档关于E2E测试的电子。我们如何编写一个测试,我们可以去到不同的页面,通过点击按钮?

x8diyxa7

x8diyxa71#

我解决了这个问题。'Try Connection'测试的内部不工作,因为electronApp.on()是一个回调函数。要编写ElectronJS & Playwright测试,我选择以下步骤:
我得到了一个Page对象(firstWindow),然后单击按钮并转到我想要测试的地方。
最终代码:

import { _electron as electron } from 'playwright';
import { test, expect, ElectronApplication, Page } from '@playwright/test';

test.describe('Add Connection', async() => {
    let electronApp: ElectronApplication;
    let firstWindow: Page;

    test.beforeAll(async() => {
        electronApp = await electron.launch({ args: ['.']} );
        firstWindow = await electronApp.firstWindow();
    });

    test('Try Connection', async() => {
        await firstWindow.title();
        await firstWindow.click('xpath=//*[@id="sidemenu-container"]/a[3]', {delay: 1500});
        await firstWindow.click('xpath=//*[@id="***"]/app-settings/div/div[1]/button[1]', {delay: 1500});
        await firstWindow.click('xpath=//*[@id="***"]', {delay: 1500});
        await firstWindow.getByPlaceholder('***').fill('emir connection');
        await firstWindow.locator('#***').selectOption({label: '***'});
        await firstWindow.click('xpath=//*[@id="***"]', {delay: 2000});
        // for the wait, (fake click)
        await firstWindow.click('xpath=//*[@id="***"]', {delay: 7000});
    });

    test.afterAll(async() => {
        await electronApp.close();
    });

});

相关问题