2016-03-04 49 views
0

我有一個很難試圖調整使用node.js中對異步我在使用selenium-webdriver和頁面對象模式時遇到了一個問題。在進行自動化測試時,我覺得有些東西必須是同步的,否則因爲在插入數據之前單擊按鈕,測試將失敗。我遇到類似這樣的問題。我想添加一名員工,然後搜索員工,但在添加員工之前搜索員工正在執行。頁對象模式異步使用Node.js的硒

var employee = new Employee('grimlek', 'Charles', 'Sexton', 'TitleTitle', 
    'Upper Management', 'Company Admin', 'Contractor', '-7', 'Remote', 
    '05212016', '3369407787', '3368791234', '[email protected]', 
    'charles.sexton', 'Skype', 'abcdefgh'); 

driver.get('https://website.com/login') 
.then(function() { 
    //This behaves as intended 
    loginPage.login('company.admin', 'password') }) 
.then(function() { 
     //Add employee 
     employeePage.addEmployee(employee) }) 
.then(function() { 
    //Search for employee after employee is added 
    employeePage.searchEmployee(employee)}); 

EmployeePage對象

var EmployeePage = function (driver) { 

this.addEmployee = function (employee) { 
    driver.findElement(webdriver.By.css('button[class=\'btn btn-default\']')).then(function (element) { 
     // 
     //Search employee function is done before the line below this 
     // 
     element.click(); 
    }).then(function() { 
     setTimeout(function() { 
      driver.findElement(webdriver.By.id('employee_username')).then(function (element) { 
       element.sendKeys(employee.username); 
      }); 

      driver.findElement(webdriver.By.id('employee_first_name')).then(function (element) { 
       element.sendKeys(employee.firstName); 
      }); 

      driver.findElement(webdriver.By.id('employee_last_name')).then(function (element) { 
       element.sendKeys(employee.lastName); 
      }); 

      driver.findElement(webdriver.By.id('employee_title_id')).then(function (element) { 
       element.sendKeys(employee.title); 
      }); 

      driver.findElement(webdriver.By.id('employee_role')).then(function (element) { 
       element.sendKeys(employee.role); 
      }); 
     }, 5000); 
    }); 
// 
// 
//Search employee should occur when the thread leaves the function 
// 
}; 

this.searchEmployee = function (employee) { 
    driver.findElement(webdriver.By.css('input[class=\'form-control ng-pristine ng-valid\']')).then(function(element) { 
     element.sendKeys(employee.firstName + ' ' + employee.lastName); 
    }); 
}; 

};

module.exports = EmployeePage;

我知道searchEmployee和addEmployee函數都沒有返回一個promise,我試圖用.then函數來鏈接它們。我確實相信這是我的問題,但我需要如何完成它的幫助,而不是我如何操縱它。我應該使用回調?我已經在這個問題上工作了四個小時,現在我已經嘗試了Google搜索並對各種主題進行了研究。如果我沒有提供足夠的代碼,請告訴我,我將提供一個簡化的可運行示例。

回答

1

一個值得稱讚的目標是使相互獨立的測試。如果對應用程序進行更改(例如,錯誤修復),則只需執行受影響的測試。此外,它使移動到可以想象的網格。

但是,這是在實踐中難以實現。您的測試必須包含滿足先決條件所需的所有測試。

Cucumber具有包含場景的功能文件每個場景都是測試。方案按照它們在功能文件中列出的順序執行。因此,要整理東西的一種方法是包括所有先決條件的情況之前您在要素文件測試,您可以將功能語句前加上tag(s)這樣,當你執行該標籤的整個功能文件運行。也許第一個場景將數據庫(的一個子集)重置爲知道狀態。

訣竅是在多臺機器上並行運行功能。如果您將這些多個客戶端指向同一服務器,請注意這些功能不應創建或更新由服務器寫入數據庫時​​可能發生衝突的重疊實體。例如。 「你這個用戶'tom'是什麼意思?」每個功能都需要創建一個唯一的用戶名。

+0

是promise在node.js中正常嗎?我開發的每個解決方案最終都會在測試結束時一個接一個地鏈接在一起。 – Grim

+0

這是否[Nodejs /問:鏈接承諾順序](http://stackoverflow.com/questions/27322994/nodejs-q-chaining-promises-sequentially)幫助? – MikeJRamsey56

+0

也看看這個[幻燈片集]。(OPT沒有提及黃瓜。)(http://pt.slideshare.net/alan_parkinson/cross-browser-testing-java-script-47642180) – MikeJRamsey56

0

使用黃瓜的方法是將每個操作的步驟分開。

例:

Given I am on XYZ Form 
And I provide all form details 

在上述情況下,步And I provide all form details你會包括步驟定義的所有領域,並開始填充字段說名字,姓氏,地址在單一步驟定義。

取而代之的是,我們要分步爲每一個人場,如:

Given I am on XYZ Form 
And I provide name details in XYZ Form 
And I provide last name details in XYZ Form 
And I provide address details in XYZ Form 

然後我們會寫這當然會按順序運行3步驟定義。

您可能會覺得打字工作增加了,而且步驟定義不必要地增加了,但這實際上可以幫助您將字段從應用程序本身中刪除,您只需要從將來的文件中刪除相關步驟。 只需評論功能文件中的某一步,就可以輕鬆測試字段驗證。 隨着每個步驟獨立工作,您的代碼將更容易維護。

當然順序工作將會實現。

+0

OP沒有提及黃瓜。儘管如此,我仍然感興趣,因爲我正在嘗試將Cucumber與節點一起使用。即使分離出來了,我的異步調用selenium守護進程也不會執行。每個黃瓜步驟似乎排隊命令,並繼續前進。程序在一些異步請求被髮送到服務器之前結束。 – Akron

+0

我相信你正在爲功能中的每一步創建相應/單獨的步驟定義。 –

+0

這可能會讓你感興趣 https://spin.atomicobject.com/2014/12/17/asynchronous-testing-protractor-angular/ –