2015-10-12 109 views
3

我正在使用webdriver.iomocha.js,我需要多次創建一些動作,我不想複製我的代碼,所以我想創建自定義函數並在每個摩卡測試(它)中調用該函數。 。如何創建webdriver.io自定義函數?

爲例:

describe('Register', function() { 
     it('Login', function (done) { 
     client 
      .url('http://exmaple.site.com) 
      .setValue('input[name="username"]', login.username) 
      .setValue('input[name="password"]', login.password) 
      .call(done); 
     } 

     it('Login and logout', function (done) { 
     client 
      .url('http://exmaple.site.com) 
      .setValue('input[name="username"]', login.username) 
      .setValue('input[name="password"]', login.password) 
      .click('#logout') 
      .call(done); 
     } 
    } 

所以就像你可以在這裏看到林複製我的登入密碼...

有什麼辦法可以像登錄創建函數並調用它的測試(它):

function login(){ 
    client 
    .setValue('input[name="username"]', login.username) 
    .setValue('input[name="password"]', login.password) 
} 

謝謝。

+0

你在問什麼是如何編寫一個函數。你真的應該花一些時間閱讀一些教程,並教你自己使用的工具的基本原理。 – JeffC

回答

2

我真的不知道你的意圖與登錄/註銷,但這裏有一個通用的自定義命令,webdriver.io custom command

client.addCommand('goGetTitle', function() { 

    return client 
     .url('https://www.yahoo.com/') 
     .getTitle() 

}); 

describe('Test', function() { 
    it('should have gotten the title', function(done) { 

     client.goGetTitle().then(function(title) { 
       console.log('title', title); 
      }) 
      .call(done); 
    }); 
}); 
+0

有什麼最好的地方應該把這個代碼放入哪個文件? – user2010955

1

試試這個

function login(){ 
    return client 
    .setValue('input[name="username"]', login.username) 
    .setValue('input[name="password"]', login.password) 
} 

describe('Register', function() { 
    it('Login', function (done) { 
    client 
     .url('http://exmaple.site.com) 
     .then(login) 
     .call(done); 
    } 

    it('Login and logout', function (done) { 
    client 
     .url('http://exmaple.site.com) 
     .then(login) 
     .click('#logout') 
     .call(done); 
    } 
} 

基本上你會替換你的重複登錄通過.then(login)。因爲登錄返回客戶端的承諾,所有東西都可以工作。

相關問題