有没有一种方法可以根据其他测试的结果来运行特定的量角器测试? [英] Is there a way to run particular Protractor test depending on the result of the other test?

查看:63
本文介绍了有没有一种方法可以根据其他测试的结果来运行特定的量角器测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种问题曾经被问过,但是大多数问题的背景都非常复杂. 场景很简单.假设我们正在测试我们最喜欢的TODO应用. 接下来是测试用例: TC00-'用户应该能够将TODO项目添加到TODO列表中' TC01-'用户应该能够重命名TODO项目' TC02-'用户应该能够删除TODO项目' 如果TC00失败,我不想运行TC01和TC02(未添加TODO项,因此我没有要删除或重命名的内容)

This kind of question has been asked before but most of this question has pretty complicated background. The scenario is simple. Let's say we are testing our favorite TODO app. Test cases are next: TC00 - 'User should be able to add a TODO item to the TODO list' TC01 - 'User should be able to rename TODO item' TC02 - 'User should be able to remove TODO item' I don't want to run the TC01 and TC02 if TC00 fails (the TODO item is not added so I have nothing to remove or rename)

因此,过去三天我一直在研究这个问题,这个问题最常见的答案是: •您的测试不应相互依赖 •量角器/茉莉花不具有动态打开/关闭测试的功能("it"块) 之所以在这里问这个问题,是因为它看起来非常普遍,并且仍然没有明确的建议来解决这个问题(我的意思是我找不到任何东西) 我的JavaScript技能很差,但是我知道我需要玩耍,比如说'通过'done'或在测试中添加if ......

So I've been researching on this question for the past 3 days and the most common answers fro this question are: • Your tests should not depend on each other • Protractor/Jasmine does not have feature to dynamically turn on/off tests ('it' blocks) There reason why I'm asking this question here is because it looks like a very widespread case and still no clear suggestion to handle this (I mean I could not find any) My javascript skills are poor but I understand that I need to play around, let's say' passing 'done' or adding the if with the test inside...

it('should add a todo' ()=> {
  todoInput.sendKeys('test')
  addButton.click();
  let item = element(by.cssContainingText('.list-item','test')
  expect(item.isPresent()).toBe(true)
}

在我的情况下,将项目添加到列表后,大约有15个测试("it"块).如果父级"测试失败,我想跳过某些测试. 请注意: 有一种解决方案可以在一个失败的情况下跳过所有剩余的测试.这不符合我的需求

In my case there are like 15 tests ('it' blocks) after adding the item to the list. And I want to skip SOME OF THE tests if the 'parent' test failed. PLEASE NOTE: There is a solution out there which allows to skip ALL remaining test if one fails. This does not suit my needs

推荐答案

伙计,我花了好几个星期研究这个问题,是的,没有明确的答案,直到我意识到量角器的工作原理.如果您也了解这一点,就会为您找到最佳选择.

Man, I spent good couple of weeks researching this, and yes there was NO clear answers, until I realized how protractor works in details. If you understand this too you'll figure out the best option for you.

采用简短理论后的解决方案

1)如果您尝试将异步功能传递给describe,您会发现它会失败,因为它仅接受同步功能

1) If you try to pass async function to describe you see it'll fail, because it only accepts synchronous function

这对您意味着什么,无论您希望传递给它什么条件,它都不能基于Promise(Promise ==可以解决问题,但不能立即解决).您实质上想做的是一个承诺(打开页面,做点什么,然后等待条件是否满足您的条件)

What it means for you, is that whatever condition you want to pass to it block, it can't be Promise based (Promise == resolves somewhen, but not immediately). What you're trying to do essentially IS a Promise (open page, do something and wait to see if the condition satisfies your criteria)

if (conditionIsTrue) { // can't be Promise
  it('name', () => {
  })
}

那是要考虑的第一件事...

Thats first thing to consider...

2)运行量角器时,它将拾取config中指定的规范文件,并建立describe/itbeforeAll/afterAll块的队列.重要的信息是在浏览器启动之前发生的.

2) When you run protractor, it picks up spec files specified in config and builds the queue of describe/it AND beforeAll/afterAll blocks. IMPORTANT DETAIL HERE IS THAT IT HAPPENS BEFORE THE BROWSER EVEN STARTED.

看这个例子

let conditionIsTrue; // undefined
  it('name', () => {
    conditionIsTrue = true;
  })
if (conditionIsTrue) { // still undefined
  it('name', () => {
  })
}

当量角器到达if()语句时,conditionIsTrue的值仍为undefined.当浏览器启动时(稍后),它可能会在it块内部被覆盖,但在构建队列时不会被覆盖.所以它跳过了.

By the time Protractor reaches if() statement, the value of conditionIsTrue is still undefined. And it maybe overwritten inside of it block, when browser starts, later on, but not when it builds the queue. So it skips it.

换句话说,量角器甚至在打开浏览器之前就知道它将描述哪些块,并且在执行期间无法修改此队列

In other words, protractor knows which describe blocks it'll run before it even opens the browser, and this queue can NOT be modified during execution

可能的解决方案

1.1在describe

let conditionIsTrue; // undefined
describe("describe", () => {
  it('name1', async () => {
    conditionIsTrue = await element.isPresent(); // NOW IT'S TRUE if element is present
  })

  it('name2', async () => {
    if (conditionIsTrue) {
      //do whatever you want if the element is present
    } else {
      console.log("Skipping 'name2' test")
    }
  })
})

因此您不会跳过it块本身,但是可以跳过it

So you won't skip the it block itself, however you can skip anything inside of it

1.2使用环境变量,可以使用相同的方法跳过不同规格的it块.示例:

1.2 The same approach can be used for skipping it blocks across different specs, using environment variable. Example:

spec_1.js

spec_1.js

describe(`Suite: 1`, () => {
  it("element is present", async () => {
    if (await element.isPresent()) {
      process.env.FLAG = true
    } else {
      process.env.FLAG = false
    }
  });
});

spec_2.js

spec_2.js

describe(`Suite: 2`, () => {
  it("element is present", async () => {
    if (process.env.FLAG) {
      // do element specific actions
    }
  });
});

  1. 我发现了另一种可能却没有机会检查的可能性是使用Grunt任务运行器,这可以帮助您实现以下方案

  1. Another possibility I found out, but never had a chance to check is to use Grunt task runner, which may help you implement the following scenario

  • 运行量角器以执行一项规范
  • 检查所需条件
  • 将此条件导出到环境变量
  • 量角器
  • 在您的Grunt任务中,通过再次启动量角器,实施条件逻辑以执行其余的条件规范

但是说实话,我不明白您为什么要走这条耗时的路线,这需要很多代码...但是就像仅供参考

But honestly, I don't see why you'd want to go this time consuming route, which requires a lot of code... But just as an FYI

这篇关于有没有一种方法可以根据其他测试的结果来运行特定的量角器测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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