零基础快速入门testcafe

【Environment】
macos

【Tool】
VScode

【Steps】
以下是从 0 开始使用 TestCafe 的步骤:

1、安装 Node.js 和 NPM:TestCafe 是基于 Node.js 开发的,因此在开始使用之前需要先安装 Node.js 和 NPM。
(可以在终端输入node -vnpm -v查看是否安装成功)

2、创建一个新的项目目录:可以使用命令行或者其他任何喜欢的方式创建一个新的项目目录。

3、在项目目录中初始化 npm:使用命令行进入到项目目录中,然后运行 npm init 命令来初始化项目的 NPM 配置文件 package.json。
(可自行输入信息,或一直回车即可)

Press ^C at any time to quit.
package name: (testcafe1)
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: XXX
license: (ISC) 
About to write to /Users/xxx/Git/Testcafe1/package.json:

4、安装 TestCafe:在命令行中运行 npm install testcafe 命令来全局安装 TestCafe。
请注意,在macOS上,需要在命令前加入“sudo”以获取管理员权限,如下所示:sudo npm install -g testcafe

5、编写测试用例:在项目目录下创建一个 tests 文件夹,并在其中创建一个 example.js 文件。

6、在 example.js 文件中编写测试脚本:例如,编写一个简单的测试用例,打开 Google 搜索页面并搜索 TestCafe。

import { Selector } from 'testcafe';

fixture `Example`
    .page `https://www.google.com`;

test('Search TestCafe on Google', async t => {
    const searchBox = Selector('[name="q"]');
    await t.typeText(searchBox, 'TestCafe');
    await t.pressKey('enter');
    await t.expect(Selector('#search').innerText).contains('TestCafe');
});

7、运行测试脚本:使用命令行运行 testcafe chrome tests/example.js 命令来运行测试脚本。

【Others】

在 macOS 上使用 TestCafe 的其他注意事项包括:

如果需要在 Safari 中运行测试,需要先启用 Safari 的自动化测试功能。详细信息可以在 TestCafe 官方文档中找到。
在终端中运行 npm install testcafe-live 命令可以安装 TestCafe Live 插件,以便在浏览器中实时查看测试结果。

猜你喜欢

转载自blog.csdn.net/weixin_46475607/article/details/130503678