【问题标题】:how to define a global variable outside the describe block in mocha?如何在 mocha 的描述块之外定义全局变量?
【发布时间】:2018-02-28 17:21:12
【问题描述】:

您好,我正在尝试使用 mocha chimp 和 webdriverio 编写前端测试。因为我必须在字体末尾使用不同的 id,所以在每个块中我都必须为各种目的定义它们。我试图在描述块之外定义 ID,然后它显示未定义浏览器。这是示例代码。

describe('password validation', function () {
    it('password should be empty @watch ', function () {
        const passwordInput = browser.element('#passwordInput');
        assert.equal(passwordInput.getValue(),"");

    });

    it("should identify weak password @watch",function () {
        const passwordInput = browser.element('#passwordInput');
        passwordInput.setValue("helloWordl");
        browser.waitForVisible(".has-warning",500);
        passwordInput.setValue("helloWordl@3");
    })

    it("should identify miss matched and matched password @watch",function () {
        const confirmpasswordInput = browser.element('#confirmpasswordInput');
        confirmpasswordInput.setValue("adofidlf"); //miss matched password given
        browser.waitForVisible(".has-error",50); //it should shows the error for worng password
        confirmpasswordInput.setValue("helloWordl@3");
        browser.waitForVisible(".has-success",50);

    })

} )

谁能告诉我如何在描述块之外定义变量,包括浏览器符号,这样我就不需要每次都在不同的描述块中定义它们。

【问题讨论】:

    标签: javascript mocha.js webdriver-io chimp.js


    【解决方案1】:

    您可以在 describe 块中定义变量并使用 beforeEach 进行初始化(来自 mocha 文档 (https://mochajs.org/)):

        describe('Connection', function() {
          var db = new Connection,
          tobi = new User('tobi'),
          loki = new User('loki'),
          jane = new User('jane');
    
          beforeEach(function(done) {
            db.clear(function(err) {
              if (err) return done(err);
              db.save([tobi, loki, jane], done);
            });
          });
    
          describe('#find()', function() {
            it('respond with matching records', function(done) {
              db.find({type: 'User'}, function(err, res) {
                if (err) return done(err);
                res.should.have.length(3);
                done();
              });
            });
          });
        });
    

    【讨论】:

      【解决方案2】:

      您可以在描述之外的对象中将 id 选择器定义为字符串。 就像你的情况,它会是

      const target = {
       input:{
        password:'#passwordInput',
        confirmPassword:'#confirmpasswordInput'
       }
      }
      describe('something',()=>{console.log(target.input.password)})
      

      !!! “浏览器。”命令不能在 describe() 之外运行,您需要为其描述当前文件测试用例名称并使用 before() 或 beforeEach() 来使用它。 describe() 可以包含其他嵌套 describe()

          describe('Test A',()=>{
               var element = {}
               before(()=>{
                browser.url('...')
                element['input']['password'] = browser.element('passwordInput')
               })
               describe('case A',()=>{
                it('should empty',()=>{
                 assert.equal(element.input.password.getValue(),'')
                })
               })
              })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-04
        • 2018-11-12
        • 1970-01-01
        • 2021-12-27
        • 2020-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多