【问题标题】:how to verify if any one expect is true out of multiple expect conditions如何在多个期望条件中验证任何一个期望是否为真
【发布时间】:2016-10-14 11:08:42
【问题描述】:

我有一个场景,我在其中搜索一个文本字符串,它可能是返回结果中任何字段的一部分,它可能位于标题中,也可能位于返回的多个结果的摘要或描述中。我想编写一个可以匹配这三个字段的测试,如果其中任何一个为真,那么我的测试应该通过。

如何将多个期望条件与 OR 条件结合使用。

【问题讨论】:

标签: javascript selenium automation protractor


【解决方案1】:

你可以用protractor.promise.all()解决它:

var title = element(by.id("title")),
    summary = element(by.id("summary")),
    description = element(by.id("description"));

protractor.promise.all([
    title.isPresent(),
    summary.isPresent(),
    description.isPresent()
]).then(function (arrExists) {
    expect(arrExists.reduce(function(a,b) { return a || b; })).toBe(true);
});

如果 3 个字段中至少有一个存在,则此测试将通过


如果您特别询问等待其中一个元素出现,您可以使用protractor.ExpectedConditions.or()

var title = element(by.id("title")),
    summary = element(by.id("summary")),
    description = element(by.id("description"));

browser.wait(EC.or(
    EC.presenceOf(title), 
    EC.presenceOf(summary),
    EC.presenceOf(description)), 5000);

【讨论】:

  • 我不想检查元素是否存在,我想检查搜索的文本是否与返回的结果匹配,并且搜索文本可以在这 3 个字段中的任何一个(标题、摘要、描述) 结果。我该怎么做?
【解决方案2】:

在Java中,我们可以像下面这样使用OR

      String expected="cool"; //this is my expected value

      String actual1="cool"; //get title from driver
      String actual2="xyz"; //get summary from driver
      String actual3="abc"; //get required text from driver

    Assert.assertTrue((expected.equals(actual1)) | (expected.equals(actual2)) | (expected.equals(actual3)));

如果您正在寻找检查标题或摘要句子中的特定单词,以下方法会有所帮助。

      String actual1="its very cool"; //get title from driver
      String actual2="xyz"; //get summary from driver
      String actual3="abcd"; //get required text from driver

    //here i am checking for cool      
    Assert.assertTrue((actual1.matches(".*cool.*")) | (actual2.matches(".*cool.*")) | (actual3.matches(".*cool.*")));

【讨论】:

  • 感谢您的回复@murali,但我正在量角器中寻找解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多