简体   繁体   English

node.js:使用Mocha在webdriverio中调用函数

[英]node.js: calling a function in webdriverio using mocha

I am writing many test cases. 我正在编写许多测试用例。 In all of them there is a common part (signing in the user and doing some other stuff). 所有这些都有一个共同的部分(登录用户并执行其他操作)。

So instead of writing that part in every test, I want a function to call it. 因此,我希望没有一个函数在每个测试中都编写该函数来调用它。

I have tried using .then and .call but it throws error: 我尝试使用.then和.call,但是会引发错误:

   .setValue('#signin_email', LogInEmail)
    ^
SyntaxError: Unexpected token .

How is this thing done? 这件事是怎么做的?

Do you mean this? 你是这个意思吗 http://webdriver.io/guide/usage/customcommands.html http://webdriver.io/guide/usage/customcommands.html

browser.addCommand("LogInEmail", function () {
    return browser
      .setValue('#signin_email', 'emailaddress')
      .setValue('#password', 'password');
});

// to invoke
browser.LogInEmail()

If you want to run your common part before every mocha test then put it in beforeEach() function like this. 如果您想在每次mocha测试之前运行您的公用部分,则将其放在这样的beforeEach()函数中。

    describe('some test', function() {

     beforeEach(function() { 
            // your common part here     
       }); 

    it('it should do something, function() {
       ...
       });
       ...
    it('it should do something else', function() {
       ...
       });


   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM