UI Test, Cucumberish

  1. 1. 軟體開發故事05 - UI Test,Cucumberish
    1. 1.1. 工具很多,只不過我選了 Cucumberish

軟體開發故事05 - UI Test,Cucumberish

工程師2 想要做 UI Testing, 又不想買機器的話呢?

那麼就看看測試的資訊時代,用程式測試程式是怎麼做的?

工具很多,只不過我選了 Cucumberish

你也可以選別的,比方說 calabash,appium,macaca,….

原因有機會再解釋。先來看一下 cucumberish吧

  • 安裝 cucumberish
1
2
3
4
5
6
7
8
9
10
11
12
13
target 'TestMe' do
use_frameworks!

target 'TestMeTests' do
inherit! :search_paths
pod 'Cucumberish'
end

target 'TestMeUITests' do
inherit! :search_paths
end

end

pod install 完成後開啟 workspace

設定的部份請參考
Github Cucumberish

主要有三個資料夾 Scenario(環境)、Steps(操作步驟)和Features(SPE文件)

完成後的檔案與資料夾(如圖)
Cucumberish01

在實作的一開始,工程師2拿到了一份 SPE 的 Feature 文件

1
2
3
4
5
6
7
8
9
10
11
Feature: TestMe Cucumberish Login Test Exapmle

Scenario Outline: Verify Login Process
Given the app is running
When I enter Account "<acc>" and Password "<pass>"
Then I should see "<result>"

Examples:
| acc | pass | result |
| aki | qaz | success |
| aki | 123 | 帳號密碼錯誤 |

接著到 Scenario 裡加上一個 LoginPage.swift 程式碼如下,大意是把操作的步驟給實作出來

Scenario 程式碼,以Login為例,Home的也要做

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import XCTest

class LoginPage: Page {
private lazy var account = findElement(.textField)["textAccount"]
private lazy var password = findElement(.secureTextField)["textPassword"]
private lazy var btnLogin = findElement(.button)["btnLogin"]

required init() {
waitFor(element: account)
}

@discardableResult func checkPage() -> LoginPage{
XCTAssertTrue(account.exists)
XCTAssertTrue(password.exists)
XCTAssertTrue(btnLogin.exists)
return self
}

@discardableResult func inputData(_ acc: String, _ pass:String) -> LoginPage{
account.tap()
account.typeText(acc)
password.tap()
password.typeText(pass)
return self
}

@discardableResult func clickLogin() ->LoginPage{
btnLogin.tap()
return self
}

@discardableResult func clickAlert(_ msg:String) ->LoginPage{
let msgResult = findElement(.alert).element.staticTexts.element(boundBy: 0).label

XCTAssertEqual(msg,msgResult)
return self
}
}

最後,Steps 把 Feature 的文件和 Scenario 裡的操作做結合

Steps 程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import XCTest
import Cucumberish

class StepDefinetion: NSObject {

var application : XCUIApplication!

func setup(_ application: XCUIApplication)
{
self.application = application

Given("the app is running") { (args, userInfo) -> Void in
LoginPage().checkPage()
}

When("I enter Account \"([^\\\"]*)\" and Password \"([^\\\"]*)\""){(args,userInfo)->Void in

guard let acc = args?[0] , let pass = args?[1]
else {
XCTAssert(false)
return
}
LoginPage().inputData(acc, pass)
LoginPage().clickLogin()

}

Then("I should see \"([^\\\"]*)\""){(args,userInfo)->Void in

guard let expectedResult = args?[0]
else {
XCTAssert(false)
return
}
if expectedResult == "success" {
HomePage().checkPage()
}else{
LoginPage().clickAlert(expectedResult)
}

}

}

class func setup(_ application: XCUIApplication)
{
StepDefinetion().setup(application)
}
}

— 此次範例的 Github TestMe 在此—

跑一下 UITest 看看結果

Cucumberish01