Protractor 測試框架的可重用函數 (Reusable functions for Protractor test framework)


問題描述

Protractor 測試框架的可重用函數 (Reusable functions for Protractor test framework)

我使用 Protractor 測試框架,需要針對不同的情況使用一些相同的操作。像身份驗證過程。

問題是:在量角器中使用自己的函數的正確方法是什麼?

我記得這個工具有效:1)異步和2) 它的函數返回promise。

可重用函數必須返回promise 以使可能做.then() 操作,否則這個函數可能沒有返回值?

一個例子(正確或否):

describe('Login procedure', function() {
  it('Login Username', function () {
    browser.get('anurl.com');
    auth('username', 'password').then(function(){console.log('NICE TO MEET YOU')});
});


var auth = function(loginstr, passwordstr) {
  return element(by.css('div[class="login"]')).isDisplayed().then(function (result) {
    if (result) {
      element(by.css('input[name="email"]')).clear().sendKeys(loginstr).then(
        function () {
          element(by.css('input[name="password"]')).clear().sendKeys(passwordstr).then(function () {
            element(by.css('button[class="submit"]')).click().then(function () {
              return element(by.id('welcome')).isPresent();
            });
          });
        });
    }
  });
}

謝謝!


參考解法

方法 1:

Your example looks fine.

It is good practice to return a promise from your utility functions (so callers can then off of it if they want). But it is not required.

In practice, the methods you are invoking that create promises (e.g., click or isDisplayed, etc) implicitly register the created promise with the control flow, so the implicit synchronization will not be impacted by refactoring the calls into your own functions.

See the Protractor Page Objects for a similar approach.

方法 2:

Our team uses Orchid‑js with Jasmine and Protractor.

It's an extension that makes defining your own functions automatic.

In this case your code will still work, you would just be able to automatically reuse your 'Login procedure' and 'Login Username' functions as well.

Describe('Login procedure', function(username,password) {
    It('Login Username', function (username,password) {
        browser.get('anurl.com');
        auth(username, password).then(function(){console.log('NICE TO MEET YOU')});
    })(username,password);
})('username','password');

Then reuse it later

Describe('Login procedure')('differentUsername','differentPassword');

(by Volodymyr NabokP.T.Marshall Bean)

參考文件

  1. Reusable functions for Protractor test framework (CC BY‑SA 2.5/3.0/4.0)

#Testing #protractor






相關問題

在線庫報告的錯誤 (Online repository for reported bugs)

INSTAL_PARSE_FAILED_NO_CERTIFICATES - 試圖在 robotsium 上測試不是我的 .apk 文件 (INSTAL_PARSE_FAILED_NO_CERTIFICATES - trying to test not my .apk file on robotium)

沒有模擬器的Android測試 (Android tests without emulator)

Protractor 測試框架的可重用函數 (Reusable functions for Protractor test framework)

隨機失敗的驗收測試錯誤:斷言失敗:在銷毀對像上調用集合 (randomly failing acceptance-tests with Error: Assertion Failed: calling set on destroyed object)

如何在嵌入式系統中執行回歸測試 (How to perform regression tests in embedded systems)

移動應用測試 (Mobile application testing)

互聯網瀏覽器測試 (Internet explorer testing)

在 VS2010 中創建帶有斷言的編碼 UI 測試的工作流程是什麼? (What is the workflow for creating a coded UI test with assertions in VS2010?)

谷歌播放保護阻止安裝我的調試 apk (Google play protect is preventing the installation of my debug apk)

如何使用 codeception seeRecord 函數來測試列的內容? (How can I use codeception seeRecord function for testing contents a column?)

將 JSON 文件作為 Post Request Flask 發送 (Sending JSON File as Post Request Flask)







留言討論