Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

testing - Access Clipboard in TestCafe

How can I access the clipboard in TestCafe? I am unable to use the navigator.clipboard API since we run in headless chrome --allow-insecure. (This is not something I can change).

Any ideas?

Thank you!

question from:https://stackoverflow.com/questions/66051800/access-clipboard-in-testcafe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

TestCafe does not have built-in clipboard tools at the moment. However, you can use the client function to emulate the clipboard:

import { Selector, ClientFunction } from 'testcafe';
fixture `Example`
    .page `http://devexpress.github.io/testcafe/example/`;

test('Clipboard test', async t => {
    const text = 'Value for copy-paste';

    const emulateClipboard = ClientFunction(() => {
        let buffer = '';

        document.addEventListener('keypress', event => {
            if (event.ctrlKey) {
                if (event.key === 'c')
                    buffer = document.getSelection().toString();
                
                if (event.key === 'v')
                    document.activeElement.value = buffer;
            }
        });
    });

    await emulateClipboard();
    await t
        .typeText('#developer-name', text)
        .selectText('#developer-name')
        .pressKey('ctrl+c')
        .click('#tried-test-cafe')
        .click('#comments')
        .pressKey('ctrl+v')
        .expect(Selector('#comments').value).eql(text)
});

This example works correctly in Chrome. It may not work in other browsers due to the difference in accessing the content of <textarea> elements.

You may be able to glean more information from this discussion: https://github.com/DevExpress/testcafe/issues/2668


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...