捕获 WebdriverJs 值以在块之外使用



我正在尝试研究如何在使用 WebDriverJs + Mocha 时使用承诺检索值的块中返回值。

有这个示例代码来尝试显示我面临问题的地方,摩卡测试 1) 失败,因为它不使用嵌套test.it块中的值,而测试 2) 通过。

import assert from 'assert';
import test from 'selenium-webdriver/testing';
import webdriver from 'selenium-webdriver';
test.describe( 'Capture value from promise', function() {
    this.timeout( 20000 );
    let title, driver;
    test.before( 'Start Browser', function() {
        driver = new webdriver.Builder().forBrowser( 'chrome' ).build();
    } );
    test.describe( '1) Capture page title without block', function() {
        test.it( 'Get and use page title', function() {
            driver.get( 'https://WordPress.com' );
            title = driver.getTitle().then( ( innerTitle ) => {
                return innerTitle;
            } );
            console.log( title ); //promise
            return assert.equal( title, 'WordPress.com: Create a free website or blog' );
        } );
    } );
    test.describe( '2) Capture page title with block', function() {
        test.it( 'Get page title', function() {
            driver.get( 'https://WordPress.com' );
            return driver.getTitle().then( ( innerTitle ) => {
                title = innerTitle;
            } );
        } );
        test.it( 'Use page title', function() {
            console.log( title ); // actual title
            return assert.equal( title, 'WordPress.com: Create a free website or blog' );
        } );
    } );
} );

如果我想使用返回的值而不嵌套另一个test.it块,有没有办法等待承诺得到解决?

为了进一步了解路易斯的答案,您可以直接使用控制流机制来保证步骤将按给定顺序执行:

test.it( '3) Capture page title with controlFLow', function() {
  let flow = driver.controlFlow();
  driver.get( 'https://WordPress.com' );
  let title = "";
  flow.execute( function() {
    return driver.getTitle().then( ( innerTitle ) => {
      title = innerTitle;
      } );
  } );
  flow.execute( function() {
    console.log( title ); // actual title
    assert.equal( title, 'WordPress.com: Create a free website or blog' );
  } );
} );

在第二个测试中,您正在执行 Mocha 不支持的操作:嵌套对 it 的调用。当我在此处运行您的代码时,测试 2 通过,但测试标题 'Use page title' 不会输出到控制台,并且不会执行console.log

摩卡完全不支持这种嵌套。这在实践中意味着,如果你嵌套对it的调用,你会得到未定义的行为。当我之前尝试嵌套it调用时,我实际上能够运行嵌套测试。它只是以意想不到的顺序运行。但是未定义的行为意味着过去有效的方法可能不再有效。

因此,您必须使第一次测试起作用。为此,您只需将断言移动到.then调用中即可。当断言失败时,承诺将被拒绝,测试将失败。

test.it( '1) Capture page title without block', function() {
    driver.get( 'https://WordPress.com' );
    driver.getTitle().then( ( innerTitle ) => {
        assert.equal(innerTitle, 'WordPress.com: Create a free website or blog');
    } );
} );

如果这是"库存"摩卡(摩卡不是通过硒使用的),那么你必须将调用链接到driver.getdriver.getTitle,并且你必须从测试中返回结果的承诺。然而,你通过selenium-webdriver/testing得到的是一种改良的摩卡,它与Selenium的"控制流"机制挂钩。因此,您不必链接呼叫或返回承诺。(关于"控制流"的良好解释可以在Selenium关于承诺的文档中找到。

摩卡允许您返回一个承诺,必须解决该承诺才能通过测试。如果承诺被拒绝,它将失败。如果超时,它将失败。如果assert为假,它将失败。

  test.describe("1) Capture page title without block", function() {
    test.it("Get and use page title", function(){
      driver.get("https://WordPress.com");
      return driver.getTitle().then(function(title) {
        assert.equal(title, "WordPress.com: Create a free website or blog");
      });
    });
  });

最新更新