量角器中的全局 beforeEach 和 afterEach [英] Global beforeEach and afterEach in protractor

查看:45
本文介绍了量角器中的全局 beforeEach 和 afterEach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每个规范中,我都有 beforeEachafterEach 语句.是否可以以某种方式全局添加它以避免规范之间的代码重复?

In each spec I have beforeEach and afterEach statements. Is it possible to add it somehow globally to avoid code duplication between specs ?

推荐答案

beforeEach()afterEach() 函数的目的是添加一段重复代码,每次开始或完成执行每个规范时都需要执行(it).还有其他方法可以添加通用代码以避免代码重复,这里有几个 -

Purpose of beforeEach() and afterEach() functions are to add a block of repetitive code that you would need to execute every time you start or complete executing each spec(it). There are other ways to add generalised code to avoid code repetition, here are few -

  • 如果您有一段代码在开始测试套件之前只需要运行一次 (describe),那么您可以使用 beforeAll()afterAll() 函数.
  • 如果你想在启动所有测试脚本之前运行一段你只想在执行开始时运行一次的代码,然后将它添加到你的onPrepare()onComplete() 函数.
  • 如果您想添加一段代码,该代码甚至应该在量角器开始实例化之前或关闭之后运行,然后使用 beforeLaunchafterLaunch.
  • If you have a piece of code that you would require to run only once before starting a test suite(describe), then you can use beforeAll() and afterAll() functions that jasmine provides.
  • If you want to run a piece of code that you want to run only once when the execution starts before starting all the test scripts, then add it in your onPrepare() and onComplete() function.
  • If you want to add a piece of code that should run even before protractor has started instantiating itself or after it has shut itself down, then use beforeLaunch and afterLaunch.

所以这一切都取决于你想使用它们的场景.希望它有所帮助.

So it all depends on the scenario that you want to use them in. Hope it helps.

这篇关于量角器中的全局 beforeEach 和 afterEach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆