测试咖啡如何根据不同的浏览器运行不同的测试。

3

我想知道是否有一种方法可以传递参数来让你的fixture或者所有的测试都知道它们正在运行哪个浏览器。

在我的情况下,我会使用这个参数来简单地为我的测试分配相应的值到一个变量中。

例如,

    switch(browser) {
            case 'chrome':
                chrome = 'chrome.com';
                break;
            case 'firefox':
                link = 'firefox.com';
                break;
            case 'safari':
                link = 'safari.com';
                break;            
            default:
                break;
}

目前,我通过添加全局节点变量实现了类似的功能,看起来像这样:

"chrome": "BROWSER=1 node runner.js"

然而,这让我为每个浏览器创建一个单独的运行程序(safari-runner, chrome-runner等),而我希望所有内容都能够放在一起。
因此,最终我需要让这项工作实现:
const createTestCafe = require('testcafe');

let testcafe         = null;

createTestCafe('localhost', 1337, 1338)
    .then(tc => {
        testcafe     = tc;
        const runner = testcafe.createRunner();

        return runner
            .src('test.js')
            .browsers(['all browsers'])
            .run({
                passBrowserId: true // I guess it would look something like this
            });
    })
    .then(failedCount => {
        console.log('Tests failed: ' + failedCount);
        testcafe.close();
    })
    .catch(error => {
        console.log(error);
        testcafe.close();
    });
2个回答

3

获取浏览器信息的方法有多种:

  • Get navigator.userAgent from the browser using ClientFunction. Optionally you can use a module to parse an user agent string, for example: ua-parser-js.

    import { ClientFunction } from 'testcafe';
    import uaParser from 'ua-parser-js';
    
    fixture `get ua`
        .page `https://testcafe.devexpress.com/`;
    
    const getUA = ClientFunction(() => navigator.userAgent);
    
    test('get ua', async t => {
        const ua = await getUA();
    
        console.log(uaParser(ua).browser.name);
    });
    
  • Use RequestLogger to obtain browser info. For example:

    import { RequestLogger } from 'testcafe';
    
    const logger = RequestLogger('https://testcafe.devexpress.com/');
    
    fixture `test`
        .page('https://testcafe.devexpress.com')
        .requestHooks(logger);
    
    test('test 1', async t => {
        await t.expect(logger.contains(record => record.response.statusCode === 200)).ok(); 
    
            const logRecord = logger.requests[0];
    
            console.log(logRecord.userAgent);
    });
    
  • The TestCafe team is working on the t.browserInfo function, which solves the issue in the future.


1

仅更新此问题,testcafe现在已实施t.browser,这将允许您检查browser.namebrowser.alias以确定您正在运行的浏览器。

import { t } from 'testcafe';

const browserIncludes = b => t.browser.name.includes(b);
const isBrowserStack = () => t.browser.alias.includes('browserstack');

fixture `test`
    .page('https://testcafe.devexpress.com')

test('is Chrome?', async () => {
    console.log(t.browser.name);
    await t.expect(browserIncludes('Chrome').ok();
    await t.expect(isBrowserStack()).notOk();
});


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接