NodeJS 编剧:如何确保一个元素不存在?

fivyi3re  于 12个月前  发布在  Node.js
关注(0)|答案(2)|浏览(97)

我试图使用Playwright和Jest来自动化一些关于在Microsoft Teams频道中上传文件的测试。
这就是我所做的和工作:

it('should upload a file', async () => {
    // Listen to file chooser popup and set file for upload
    page.on('filechooser', async (fileChooser) => {
        await fileChooser.setFiles('test-files/example.txt');
    });

    // wait for the content iframe to be loaded
    const contentFrame = page.frameLocator('[name="embedded-page-container"]');

    // @TODO here I would like to first make sure the file does not exist
    // 
    // How could I do that ???
    // 

    // upload file and check for existence in file list displayed
    await contentFrame.locator('button[name="Upload"]').click();
    await contentFrame.locator('button[name="Files"]').click();
    await contentFrame.locator('button:has-text("example.txt")').waitFor({ state: "visible" });
});

**问题:**如何检查文件之前不存在?

基本上,我如何检查我等待在最后一行变得可见的button:has-text("example.txt")元素在上传操作之前是否存在?
谢谢.

uujelgoq

uujelgoq1#

我用python而不是JavaScript做了类似的事情,但它应该对你有用。
我在一个helper中定义了自己的函数:

def my_own_wait_for_selector(page, selector, time_out):
    try:
        page.wait_for_selector(selector, timeout=time_out)
        return True
    except:
        return False

然后,您可以使用它来了解您的元素是否在DOM中,并基于此做出决策/流程

if helper.my_own_wait_for_selector(page, "MySelector", 5000):
    #Whatever I want to do if element is there
else:
    #Whatever I want to do if element is NOT there
nbnkbykc

nbnkbykc2#

方法如下:

await expect(page.locator('span:has-text("Generate PDF")')).toBeVisible() // Just for context of how to do the opposite
await expect(page.locator('span:has-text("Generate ePub")')).not.toBeVisible()
await expect(page.locator('span:has-text("Generate Docx")')).not.toBeVisible()

来源:Github issue outlines solution

相关问题