github持续集成的设置_如何使用GitHub Actions和Puppeteer建立持续集成管道

github持续集成的设置

Lately I've added continuous integration to my blog using Puppeteer for end to end testing. My main goal was to allow automatic dependency updates using Dependabot. In this guide I'll show you how to create such a pipeline yourself.

最近,我使用Puppeteer在博客中添加了持续集成,以进行端到端测试。 我的主要目标是允许使用Dependabot自动更新依赖项。 在本指南中,我将向您展示如何自己创建这样的管道。

As my CI platform, I chose Github Actions, as it is super easy to work with. It also integrates beautifully with any Github repository you already have. The whole thing only took roughly two days of intermittent work, and I think the results are quite awesome.

作为我的CI平台,我选择了Github Actions ,因为它非常易于使用。 它还可以与您已经拥有的任何Github存储库完美集成。 整个过程大约只花了两天的间歇时间,我认为结果非常棒。

I do want to give a shout-out to Nick Taylor, who published his article on the subject, and laid the ground work for my efforts here. I encourage you to read his article as well.

我确实要大声疾呼尼克·泰勒(Nick Taylor),他发表了有关该主题的文章 ,并为我在这里的努力奠定了基础。 我鼓励您也阅读他的文章。

My tech stack is quite different though. I chose puppeteer as my end-to-end framework for several reasons. The first is that it is written and maintained by the folks behind the Chrome dev tools, so I'm guaranteed a life-time of support (until Chrome dies out, which is not in the near future), and it is really easy to work with.

我的技术栈却大不相同。 由于多种原因,我选择puppeteer作为我的端到端框架。 首先是它是由Chrome开发者工具背后的人编写和维护的,因此可以保证我将终身提供支持(直到Chrome淘汰,这不会在不久的将来),而且确实很容易一起工作。

Another reason is that at home I'm working on a windows laptop with WSL (on which I'm running zshell with oh-my-zsh). Setting up cypress is quite a bit more difficult (although in our world nothing is impossible). Both reasons led me to choose puppeteer, and so far I'm not regretting it.

另一个原因是在家里,我正在使用WSL(在其上运行oh-my-zsh的zshell)在Windows笔记本电脑上工作。 设置柏树要困难得多(尽管在我们这个世界上没有什么是不可能的)。 这两个原因都促使我选择了操纵up,到目前为止,我并不后悔。

端到端测试 (End to end testing)

End to end (or E2E) tests are different from other types of automated tests. E2E tests simulate a real user, performing actions on the screen. This kind of test should help fill the blank space between "static" tests - such as unit tests, where you usually don't bootstrap the entire application - and component testing, which usually runs against a single component (or a service in a micro-service architecture).

端到端(或E2E)测试与其他类型的自动化测试不同。 E2E测试可模拟真实用户,并在屏幕上执行操作。 这种测试应该有助于填补“静态”测试(例如,单元测试(通常不引导整个应用程序)和组件测试)之间的空白,其中,组件测试通常针对单个组件(或微型服务)运行服务架构)。

By simulating user interaction you get to test the experience of using your application or service in the same way a regular user would experience it.

通过模拟用户交互,您可以测试普通用户体验应用程序或服务的体验。

The mantra that we try to follow is that it does not matter if your code performs perfectly if the button the user should press is hidden due to some CSS quirk. The end result is that the user will never get to feel the greatness of your code.

我们尝试遵循的口头禅是,由于某些CSS怪癖,如果用户应按下的按钮处于隐藏状态,则代码是否可以完美执行并不重要。 最终结果是用户将永远不会感觉到您的代码的伟大之处。

木偶手入门 (Getting started with puppeteer)

Puppeteer has a few configuration options that make it really awesome to use for writing and validating tests.

Puppeteer有几个配置选项,使编写和验证测试使用起来真的很棒。

Puppeteer tests can run in a "head-full" state. This means you can open a real browser window, navigate to the site being tested, and perform actions on the given page. This way you - the developers writing the tests - can see exactly what happens in the test, what buttons are being pressed and what the resulting UI looks like.

木偶测试可以在“满头”状态下运行。 这意味着您可以打开真实的浏览器窗口,导航到要测试的站点,然后在给定页面上执行操作。 这样,您-编写测试的开发人员-可以准确查看测试中发生的情况,按下的按钮以及生成的UI外观。

The opposite of "head-full" would be headless, where puppeteer does not open a browser window, making it ideal for CI pipelines.

与“ head-full”的相反将是无头的,其中操纵up的人不会打开浏览器窗口,因此非常适合CI管道。

Puppeteer is quite easy to work with, but you'll be surprised with the number of actions you can perform using an automated tool.

Puppeteer的操作非常简单,但是您会惊讶于使用自动化工具可以执行的操作数量。

We'll start with a basic scraper that prints the page title when we go to https://dorshinar.me. In order to run puppeteer tests, we must install it as a dependency:

我们将从基本的刮板开始,当我们转到https://dorshinar.me时,该刮板将打印页面标题。 为了运行操纵up的测试,我们必须将其安装为依赖项:

npm i puppeteer

Now, our basic scraper looks like this:

现在,我们的基本刮板如下所示:

const puppeteer = require("puppeteer");(async () => {const browser = await puppeteer.launch();const page = await browser.newPage();await page.goto("https://dorshinar.me");console.log(await page.title());await browser.close();
})();

What we do here is very simple: we open the browser with puppeteer.launch(), create a new page with browser.newPage() and navigate to this blog with page.goto(), and then we print the title.

我们在这里做的是非常简单的:我们跟打开浏览器puppeteer.launch()创建一个新的页面browser.newPage()然后导航到该博客与page.goto()然后我们打印标题。

There are a bunch of things we can do with the puppeteer API, such as:

我们可以使用puppeteer API做很多事情,例如:

Running code in the context of the page:

在页面上下文中运行代码:

(async () => {await page.evaluate(() => document.querySelector(".awesome-button").click());
})();

Clicking on elements in the screen using a CSS selector:

使用CSS选择器单击屏幕上的元素:

(async () => {await page.click(".awesome-button");
})();

Making use of the $ selector (jQuery style):

利用$选择器(jQuery样式):

(async () => {await page.$(".awesome-button");
})();

Taking a screenshot:

截屏:

(async () => {await page.screenshot({ path: "screenshot.png" });
})();

There is a bunch more you can do with the puppeteer API, and I suggest you take a look at it before diving into writing tests. But the examples I've shown should give you a solid foundation to build from.

您可以使用puppeteer API做更多的事情,我建议您在开始编写测试之前先看一下它。 但是我展示的示例应该为您提供坚实的基础。

将木偶与Jest集成 (Integrating puppeteer with Jest)

jest is an awesome test runner and assertion library. From their docs:

笑话是一个了不起的测试运行程序和断言库。 从他们的文档:

Jest is a delightful JavaScript Testing Framework with a focus on simplicity.

Jest是一个令人愉悦JavaScript测试框架,专注于简单性。

Jest allows you to run tests, mock imports, and make complex assertions really easily. Jest is also bundled with create-react-app, so I use it often at work.

Jest允许您真正轻松地运行测试,模拟导入并进行复杂的断言。 Jest还与create-react-app捆绑在一起,因此我经常在工作中使用它。

编写您的第一个玩笑测试 (Writing your first Jest test)

Jest tests are super easy to write, and they might be familiar to those who know other testing frameworks (as Jest uses it, test, describe and other familiar conventions).

Jest测试非常容易编写,并且对于熟悉其他测试框架的人可能很熟悉(因为Jest使用ittestdescribe和其他熟悉的约定)。

A basic test could look like:

基本测试可能类似于:

function subtract(a, b) {return a - b;
}it("subtracts 4 from 6 and returns 2", () => {expect(subtract(6, 4)).toBe(2);
});

You can also group multiple tests under one describe, so you can run different describes or use it for convenient reporting:

您还可以将多个测试归为一个describe ,因此您可以运行不同的describe或将其用于方便的报告:

function divide(a, b) {if (b === 0) {throw new Error("Can't divide by zero!");}return a / b;
}describe("divide", () => {it("throws when dividing by zero", () => {expect(() => divide(6, 0)).toThrow();});it("returns 3 when dividing 6 by 3", () => {expect(divide(6, 3)).toBe(2);});
});

You can, of course, create much more complicated tests using mocks and other type of assertions (or expectations), but for now that's enough.

当然,您可以使用模拟和其他类型的断言(或期望)来创建更复杂的测试,但是到目前为止就足够了。

Running the tests is also very simple:

运行测试也非常简单:

jest

Jest will look for test files with any of the following popular naming conventions:

Jest将使用以下任何流行的命名约定查找测试文件:

  • Files with .js suffix in __tests__ folders.

    __tests__文件夹中带有.js后缀的文件。

  • Files with .test.js suffix.

    .test.js后缀的文件。

  • Files with .spec.js suffix.

    .spec.js后缀的文件。

玩笑木偶 (jest-puppeteer)

Now, we need to make puppeteer play nicely with jest. This isn't a particularly hard job to do, as there is a great package named jest-puppeteer that comes to our aid.

现在,我们需要使木偶戏与玩笑保持良好的配合。 这并不是一件特别困难的事情,因为有一个名为jest-puppeteer的出色软件包可以帮助我们。

First, we must install it as a dependency:

首先,我们必须将其安装为依赖项:

npm i jest-puppeteer

And now we must extend our jest configuration. If you don't have one yet, there are a number of ways to do it. I'll go with a config file. Create a file named jest.config.js in the root of your project:

现在,我们必须扩展我们的笑话配置。 如果您还没有,那么有很多方法可以做到。 我将使用一个配置文件。 在项目的根目录中创建一个名为jest.config.js的文件:

touch jest.config.js

In the file we must tell jest to use jest-puppeteer's preset, so add the following code to the file:

在文件中,我们必须告诉jest使用jest-puppeteer的预设,因此将以下代码添加到文件中:

module.exports = {preset: "jest-puppeteer"// The rest of your file...
};

You may specify a special launch configuration in a jest-puppeteer.config.js file, and jest-puppeteer will pass this configuration to puppeteer.launch(). For example:

您可以在jest-puppeteer.config.js文件中指定特殊的启动配置,并且jest-puppeteer会将此配置传递给puppeteer.launch() 。 例如:

module.exports = {launch: {headless: process.env.CI === "true",ignoreDefaultArgs: ["--disable-extensions"],args: ["--no-sandbox"],executablePath: "chrome.exe"}
};

jest-puppeteer will take care of opening a new browser and a new page and store them on the global scope. So in your tests you can simply use the globally available browser and page objects.

jest-puppeteer将负责打开新的浏览器和新的页面,并将它们存储在全局范围内。 因此,在测试中,您可以简单地使用全局可用的browserpage对象。

Another great feature we can use is the ability of jest-puppeteer to run your server during your tests, and kill it afterwards, with the server key:

我们可以使用的另一个出色功能是jest-puppeteer可以在测试期间运行服务器,然后使用server密钥将其杀死:

module.exports = {launch: {},server: {command: "npm run serve",port: 9000,launchTimeout: 180000}
};

Now jest-puppeteer will run npm run serve, with a timeout of 180 seconds (3 minutes), and listen on port 9000 to see when it will be up. Once the server starts the tests will run.

现在jest-puppeteer将运行npm run serve ,超时时间为180秒(3分钟),并在端口9000上监听以查看何时启动。 服务器启动后,测试将运行。

You can now write a full test suite using jest and puppeteer. The only thing left is creating a CI pipeline, for which we'll use GitHub actions.

您现在可以使用jest和puppeteer编写完整的测试套件。 剩下的唯一事情就是创建一个CI管道,我们将使用GitHub操作。

You can add a script to your package.json file to execute your tests:

您可以将脚本添加到package.json文件中以执行测试:

{"scripts": {"test:e2e": "jest"}
}

Github行动要点 (Github Actions in a gist)

Recently, Github released a big new feature called Actions. Basically, actions allow you to create workflows using plain yaml syntax, and run them on dedicated virtual machines.

最近,Github发布了一项名为Actions的重要新功能。 基本上,操作允许您使用简单的yaml语法创建工作流,并在专用虚拟机上运行它们。

In your workflow you can do pretty much anything you want, from basic npm ci && npm build && npm run test to more complicated stuff.

从基本的npm ci && npm build && npm run test到更复杂的工作,您都可以在工作流中做几乎所有您想做的事情。

I'll show you how to configure a basic workflow running your puppeteer test suite, and prevent merging if your tests don't pass.

我将向您展示如何配置运行puppeteer测试套件的基本工作流程,并在测试未通过的情况下防止合并。

The easiest way to start is to click on the Actions tab in your github repo. If you haven't configured any action before, you'll see a list of previously configured workflows, from which you can choose one with some predefined configuration.

最简单的开始方法是单击github存储库中的“ Actions选项卡。 如果您之前未配置任何操作,则将看到以前配置的工作流列表,您可以从中选择具有一些预定义配置的工作流。

github-actions-start-3

For our case, choosing the predefined Node.js action is good enough. The generated yaml looks like this:

对于我们的情况,选择预定义的Node.js操作就足够了。 生成的yaml如下所示:

name: Node CIon: [push]jobs:build:runs-on: ubuntu-lateststrategy:matrix:node-version: [8.x, 10.x, 12.x]steps:- uses: actions/checkout@v1- name: Use Node.js ${{ matrix.node-version }}uses: actions/setup-node@v1with:node-version: ${{ matrix.node-version }}- name: npm install, build, and testrun: |npm cinpm run build --if-presentnpm testenv:CI: true

In the file you can configure the workflow name, jobs to run, and when to run the workflow. You can run your workflow on every push, on new pull requests, or as a recurring event.

在文件中,您可以配置工作流名称,要运行的作业以及运行时间。 您可以在每次推送,新的拉取请求或重复发生的事件上运行您的工作流。

Jobs in a workflow run in parallel by default, but can be configured to run in sequence. In the above workflow, there is one job named build.

默认情况下,工作流中的作业并行运行,但可以配置为按顺序运行。 在上述工作流程中,有一个名为build作业。

You can also choose the OS on which your workflow will run (by default you can use Windows Server 2019, Ubuntu 18.04, Ubuntu 16.04 and macOS Catalina 10.15 - at the time of publishing) with the runs-on key.

您也可以选择在其工作流运行(默认情况下,你可以使用Windows Server 2019的,Ubuntu 18.04,Ubuntu的16.04和MacOS卡塔利娜10.15 -在出版时间)操作系统与runs-on键。

The strategy key can help us run our tests on a matrix of node versions. In this case we have the latest versions of the latest LTS majors - 8.x, 10.x and 12.x. If you are interested in that you can leave it as is, or simply remove it and use any specific version you want.

strategy密钥可以帮助我们在节点版本矩阵上运行测试。 在这种情况下,我们具有最新的LTS专业的最新版本8.x10.x12.x 如果您对此感兴趣,可以将其保留不变,也可以将其删除并使用所需的任何特定版本。

The most interesting configuration option is the steps. With it we define what actually goes on in our pipeline.

最有趣的配置选项是steps 。 通过它,我们定义了管道中实际发生的事情。

Each step represents an action you can perform, such as checking out code from the repo, setting up your node version, installing dependencies, running tests, uploading artifacts (to be used later or downloaded) and many more.

每个步骤都代表您可以执行的操作,例如从存储库中签出代码,设置节点版本,安装依赖项,运行测试,上传工件(稍后使用或下载)等等。

You can find a very extensive list of readily available actions in the Actions Marketplace.

您可以在Actions Marketplace中找到非常广泛的随时可用的动作列表。

The basic configuration will install dependencies, build our project and run our tests. If you need more (for example if you want to serve your application for e2e tests) you may alter it to your liking. Once done, commit your changes and you are good to go.

基本配置将安装依赖项,构建我们的项目并运行我们的测试。 如果您需要更多服务(例如,如果要为e2e测试提供服务),则可以根据自己的喜好进行更改。 完成后,提交您的更改,一切顺利。

强制检查在合并之前通过 (Forcing checks to pass before merge)

The only thing left for us is to make sure no code can be merged before our workflow passes successfully. For that, go to your repo's settings and click on Branches:

我们剩下的唯一事情就是确保在我们的工作流成功通过之前,不能合并任何代码。 为此,请转到您的仓库的设置,然后单击“分支”:

Github Settings > Branch

We need to set a Branch protection rule so that malicious code (or at least code that doesn't pass our tests) won't be merged. Click on Add rule, and under Branch name pattern put your protected branch (master, dev or whichever one you choose). Make sure Require status checks to pass before merging is checked, and you'll be able to choose which checks must pass:

我们需要设置分支保护规则,以使恶意代码(或至少没有通过测试的代码)不会被合并。 点击添加规则 ,然后在分支名称模式下放置受保护的分支(主,开发或您选择的任何一个)。 确保选中“ 需要状态检查才能通过合并” ,然后您可以选择必须通过哪些检查:

Require status checks

Click on Save changes below, and you're good to go!

点击下面的保存更改,一切顺利!

Thank you for reading!This article was previously published on my blog: dorshinar.me, If you want to read more content, you can check my blog as it would mean a lot to me.

谢谢您的阅读!本文先前发表在我的博客上: dorshinar.me ,如果您想内容,可以查看我的博客,因为这对我来说意义重大。

Buy Me a Coffee at ko-fi.com

翻译自: https://www.freecodecamp.org/news/continuous-integration-with-github-actions-and-puppeteer/

github持续集成的设置

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390724.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

shell与常用命令

虚拟控制台 一台计算机的输入输出设备就是一个物理的控制台 ; 如果在一台计算机上用软件的方法实现了多个互不干扰独立工作的控制台界面,就是实现了多个虚拟控制台; Linux终端的工作方式是字符命令行方式,用户通过键盘输入命令进…

Linux文本编辑器

Linux文本编辑器 Linux系统下有很多文本编辑器。 按编辑区域: 行编辑器 ed 全屏编辑器 vi 按运行环境: 命令行控制台编辑器 vi X Window图形界面编辑器 gedit ed 它是一个很古老的行编辑器,vi这些编辑器都是ed演化而来。 每次只能对一…

Alpha第十天

Alpha第十天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV、ZQ、ZC负责前端开发,由JP和LL负责建库和服务器。界面开发的教辅材料是《第一行代码》,利用And…

Streamlit —使用数据应用程序更好地测试模型

介绍 (Introduction) We use all kinds of techniques from creating a very reliable validation set to using k-fold cross-validation or coming up with all sorts of fancy metrics to determine how good our model performs. However, nothing beats looking at the ra…

X Window系统

X Window系统 一种以位图方式显示的软件窗口系统。诞生于1984,比Microsoft Windows要早。是一套独立于内核的软件 Linux上的X Window系统 X Window系统由三个基本元素组成:X Server、X Client和二者通信的通道。 X Server:是控制输出及输入…

lasso回归和岭回归_如何计划新产品和服务机会的回归

lasso回归和岭回归Marketers sometimes have to be creative to offer customers something new without the luxury of that new item being a brand-new product or built-from-scratch service. In fact, incrementally introducing features is familiar to marketers of c…

Linux 设备管理和进程管理

设备管理 Linux系统中设备是用文件来表示的,每种设备都被抽象为设备文件的形式,这样,就给应用程序一个一致的文件界面,方便应用程序和操作系统之间的通信。 设备文件集中放置在/dev目录下,一般有几千个,不…

贝叶斯 定理_贝叶斯定理实际上是一个直观的分数

贝叶斯 定理Bayes’ Theorem is one of the most known to the field of probability, and it is used often as a baseline model in machine learning. It is, however, too often memorized and chanted by people who don’t really know what P(B|E) P(E|B) * P(B) / P(E…

文本数据可视化_如何使用TextHero快速预处理和可视化文本数据

文本数据可视化自然语言处理 (Natural Language Processing) When we are working on any NLP project or competition, we spend most of our time on preprocessing the text such as removing digits, punctuations, stopwords, whitespaces, etc and sometimes visualizati…

linux shell 编程

shell的作用 shell是用户和系统内核之间的接口程序shell是命令解释器 shell程序 Shell程序的特点及用途: shell程序可以认为是将shell命令按照控制结构组织到一个文本文件中,批量的交给shell去执行 不同的shell解释器使用不同的shell命令语法 shell…

真实感人故事_您的数据可以告诉您真实故事吗?

真实感人故事Many are passionate about Data Analytics. Many love matplotlib and Seaborn. Many enjoy designing and working on Classifiers. We are quick to grab a data set and launch Jupyter Notebook, import pandas and NumPy and get to work. But wait a minute…

转:防止跨站攻击,安全过滤

转:http://blog.csdn.net/zpf0918/article/details/43952511 Spring MVC防御CSRF、XSS和SQL注入攻击 本文说一下SpringMVC如何防御CSRF(Cross-site request forgery跨站请求伪造)和XSS(Cross site script跨站脚本攻击)。 说说CSRF 对CSRF来说,其实Spring…

Linux c编程

c语言标准 ANSI CPOSIX(提高UNIX程序可移植性)SVID(POSIX的扩展超集)XPG(X/Open可移植性指南)GNU C(唯一能编译Linux内核的编译器) gcc 简介 名称: GNU project C an…

k均值算法 二分k均值算法_使用K均值对加勒比珊瑚礁进行分类

k均值算法 二分k均值算法Have you ever seen a Caribbean reef? Well if you haven’t, prepare yourself.您见过加勒比礁吗? 好吧,如果没有,请做好准备。 Today, we will be answering a question that, at face value, appears quite sim…

新建VUX项目

使用Vue-cli安装Vux2 特别注意配置vux-loader。来自为知笔记(Wiz)

衡量试卷难度信度_我们可以通过数字来衡量语言难度吗?

衡量试卷难度信度Without a doubt, the world is “growing smaller” in terms of our access to people and content from other countries and cultures. Even the COVID-19 pandemic, which has curtailed international travel, has led to increasing virtual interactio…

Linux 题目总结

守护进程的工作就是打开一个端口,并且等待(Listen)进入连接。 如果客户端发起一个连接请求,守护进程就创建(Fork)一个子进程响应这个连接,而主进程继续监听其他的服务请求。 xinetd能够同时监听…

《精通Spring4.X企业应用开发实战》读后感第二章

一、配置Maven\tomcat https://www.cnblogs.com/Miracle-Maker/articles/6476687.html https://www.cnblogs.com/Knowledge-has-no-limit/p/7240585.html 二、创建数据库表 DROP DATABASE IF EXISTS sampledb; CREATE DATABASE sampledb DEFAULT CHARACTER SET utf8; USE sampl…

视图可视化 后台_如何在单视图中可视化复杂的多层主题

视图可视化 后台Sometimes a dataset can tell many stories. Trying to show them all in a single visualization is great, but can be too much of a good thing. How do you avoid information overload without oversimplification?有时数据集可以讲述许多故事。 试图在…

一步一步构建自己的管理系统①

2019独角兽企业重金招聘Python工程师标准>>> 系统肯定要先选一个基础框架。 还算比较熟悉Spring. 就选Spring boot postgres mybatis. 前端用Angular. 开始搭开发环境,开在window上整的。 到时候再放到服务器上。 自己也去整了个小服务器,…