0

Hi I was trying to write a front-end testing using mocha chimp and webdriverio. as I had to work with different id in font end, in every block I had to define them for various purposes. I tried to define the ID outside the describe block then it shows browser is not defined. here is the sample code.

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);

    })

} )

can anybody tell me how to define the variable outside the describing block including browser notation, so that I don't need to define them every time in the different describing block.

Sanjida lina
  • 113
  • 10

2 Answers2

0

You can define variables in describe block and use beforeEach for initialization (from mocha documentation (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();
          });
        });
      });
    });
Sergii Vorobei
  • 1,422
  • 8
  • 18
0

you can define id selector as string in object outside describe. as your case, it will be

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

!!! 'browser.' command cannot run outside describe(), you will need to describe current file test case name for it and use before() or beforeEach() to use it. describe() can contain other nest 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(),'')
          })
         })
        })