【问题标题】:Protractor: Is it possible to check if an element doesn't contain certain text?量角器:是否可以检查元素是否不包含某些文本?
【发布时间】:2017-06-01 18:59:27
【问题描述】:

在我正在测试的页面上,用户可以拥有一种货币或多种货币(即欧元和美元),货币/货币将出现在页面顶部的同一个 div 中。 如果用户有多种货币,则每种货币的标签会在页面下方显示,如果用户只有一种货币,则不会出现标签(因为用户无需切换标签)。

我可以通过检查标题中包含的文本是否与货币选项卡中包含的文本匹配来测试多币种用户。

但是,由于没有显示单一货币的标签,我不确定如何测试。

例如,如果我只有“欧元”货币,有没有办法做类似的事情

if element(by.className("currencies"))contains 'EUR'

&& doesn't contain 'USD' && doesn't contain 'GBP' expect element(by.className("tabs").toDisplay.toBeFalsy()

这是页面对象文件的代码

this.checkCurrency = function(currency) {
 var checkBalance = element(by.className("balances"));
 checkBalance.getText().then(function (text) {
   if (text.indexOf("GBP" && "EUR")>= 0) {
  expect(element.all(by.linkText("GBP")).isDisplayed()).toBeTruthy();
   console.log("EUR GBP buyer");
   }
   else if (text.indexOf("GBP" && "USD")>= 0) {
  expect(element.all(by.linkText('USD')).isDisplayed()).toBeTruthy();
   console.log("USD GBP buyer");
   }
   else
   {
   console.log("false");
   }
   });
};

【问题讨论】:

  • expect(element(by.className("tabs").isDisplayed()).toEqual(false) 不适合你吗?
  • 感谢您的回复。问题是我需要能够检查
    是否包含 'EUR' 和 'USD'/'GBP' 但我还需要检查它是否只包含 'EUR' 而不包含 'USD /英镑'。以上在理论上可行,但 If Else 无法区分
    是否仅包含“EUR”或包含“EUR”和另一种货币。
  • 嗯,喜欢if (text.indexOf("string") === -1)?或者,如果不使用 if (text.indexOf("EUR") >= 0 && text.indexOf("USD") == -1 && text.indexOf("GBP") == -1) 而不是使用正则表达式,那么整个事情看起来会更干净:if (text.matches(\^EUR$\g) && text.matches(\^(?!.*(USD|GBP)).*$\g)
  • 谢谢你,我会试试看,看看效果如何

标签: node.js protractor automated-tests aurelia e2e-testing


【解决方案1】:

根据您的描述,我不太确定故障在哪里。通常,您希望将这种逻辑排除在页面对象之外。您的测试应该了解页面应该处于什么状态并调用不同的函数。我知道这并不总是可能的,但如果可以的话,效果会好得多。这是一些一般情况建议,应该有所帮助。

你可以捕捉到 promise 的成功状态和失败状态。大多数人使用 pass 函数,却忘记了 fail 函数。

promise.then(passFunction, failFunction)

您可以通过几种不同的方式使用它。如果您意识到量角器中的几乎所有内容都在返回一个承诺。 示例:

element(by.className("currencies")).getText()
.then(
function(text) {
  //check on something
},function(error){
  //don't check on something
  if(someCondition) {
    throw error;
  } else {
    //the test continues
  }
});

你甚至可以做到并期待

expect(element(by.className("currencies")).getText()).not.toContain("EUR")
.then(
function(passed) {
  //check on something
},function(failed){
  //don't check on something
  if(someCondition) {
    throw failed;
  } else {
    //the test continues
  }
});

或者一个简单的 findElement

element(by.className("currencies"))
.then(
function(element) {
  //check on something
},function(error){
  //don't check on something
  if(someCondition) {
    throw failed;
  } else {
    //the test continues
  }
});

【讨论】:

  • 谢谢你,我的 If Else 语句将有大约 6 或 7 个不同的场景来测试,使用上面你发布的内容,这将最适合这个。我是量角器的新手,虽然我很欣赏你上面的帖子,但我对如何实现它有点困惑。
猜你喜欢
相关资源
最近更新 更多
热门标签