簡體   English   中英

Mocha測試順序文件級和webdriverjs實例持久性

[英]Mocha test order file-wise and webdriverjs instance persistence

我正在用Mocha和WebDriver測試我的Web應用程序。 我正在嘗試有關Mocha測試順序和驅動程序持續狀態的最佳實踐。

我想將測試分離到不同的文件,例如

test\
    index.js
    selenium\
        login.js
        search.js

因此,在執行方面,必須首先登錄login.js,因為它登錄到應用程序並獲得身份驗證。 只有在該search.js之后才可以執行。 但是如何? 在login.js中,我現在有了這個:

webdriverjs = require('webdriverjs');

describe 'UI/Selenium', ->
    client = {}

    before ->
        client = webdriverjs.remote
            desiredCapabilities:
                browserName: 'chrome'

        client.init()
        client.windowHandleSize({width: 1920, height: 1080})

    it 'should let us login', (done) ->
        client.url('http://127.0.0.1:1337/login')
        .setValue('#username', 'username')
        .setValue('#password', 'password')
        .buttonClick('button[type="submit"]')
        .waitFor '#search_results_user', 5000, (err) -> throw err if err
        .call done

如何將客戶端的狀態保持在其他測試中,而不必每次都重新初始化? 以及如何使用Mocha定義文件的執行順序?

如何將客戶端的狀態保持在其他測試中,而不必每次都重新初始化?

您可以在測試before掛鈎中設置要共享的任何內容(並在測試after掛鈎中將其拆解)。 這將意味着在測試中移動代碼以登錄到您的before hook。 假設您正在測試“ foo”視圖,則可以執行以下操作:

describe("foo view", function () {
    before(function () { /* create selenium driver */ });

    describe("when used by a logged in user", function () {
        before(function () { /* log in */ });

        it(...

        it(...

        after(function () { /* log out */ });
    });

    describe("when used by a logged out user", function () {
        it(...

        it(...
    });
    after(function () { /* shut down the driver */ });
});

以及如何使用Mocha定義文件的執行順序?

Mocha測試不應該相互依賴,因此不應該依賴於它們執行的順序。

如果您處於必須打破這一基本規則的情況,則可以按照需要的順序從帶有測試文件列表的命令行中調用Mocha。 或者,您可以以編程方式啟動Mocha並使用addFile按順序添加文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM