2017-04-10 100 views
0

我目前正在爲Android和iOS的流程和設計開始一個應用程序的自動化項目。我在使用Cucumber和Cucumber框架。我如何爲Android和iOS使用相同的黃瓜步驟定義

我已經開始自動Android和本質是什麼,我需要做的就是每一步的定義有針對Android獨立的代碼和iOS有點像這樣的僞代碼:

Then (/^I click the Login Button$/) do 
if mobile_platform = android 
    #android locators and code here 
else 
    #iOS locators and code here 
end 
end 

我怎麼會去建立我的項目允許使用這種特定的步驟定義?

爲每個操作系統分別設置特徵和步驟定義,而不是試圖將它們融合在一起更好嗎?

感謝您的任何和所有幫助,可以給我。

回答

1

鑑於應用程序之間的共同性,共享功能文件是有意義的。管理特定於平臺的step_definitions的乾淨方法是將它們保存在不同的目錄中。

請看下面這個簡單的示例項目。

simple cucumber project

可以(需要)的選項,像這樣用黃瓜-r替代步驟定義文件夾之間切換:

cucumber -r features/step_definitions_android -r features/support 

需要注意的是自動加載,只要您使用禁用 - r選項,因此爲什麼您需要明確包含第二個需求,以便將特徵/支持文件夾中的任何代碼提取出來。

爲了方便兌替代終端上運行,你可以創建相應的配置文件:

# cucumber.yaml 
android: -r features/step_definitions_android -r features/support 
ios: -r features/step_definitions_ios -r features/support 

正如你可以看到下面,每個這些配置文件運行時,相關的特定於平臺的步驟定義調用。

run cucumber with alternative profiles

+0

這正是我一直在尋找的東西。謝謝。 –

0

我不會去爲不同的操作系統單獨的功能文件。您希望您的應用程序的行爲與操作系統無關。如果你有兩個,你的功能可能會發生分歧。

我的做法是執行兩次執行,並將目標環境分離到堆棧中。我會使用一個環境變量來指出我目前正在定位哪個操作系統。

在特定環境下執行某些操作的代碼將會非常不同,因此我將使用工廠來選擇當前要使用的實現。我不會考慮使用代碼中的多個條件來分離執行,就像你的小例子所表明的那樣。我將有一個這種類型的條件的唯一的地方將在工廠方法,創建將使用您的應用程序的實際類。

0

您應該使用與任何O.S.無關的單個功能文件。

如果你發現,你必須根據你O.S有加的it.Like你上面的代碼的

if mobile_platform = android 
    #android locators and code here 
else 
    #iOS locators and code here 

但95%表示應同時爲O.S.工作檢查,以分離出操作的任何這樣的情況下

0

你爲什麼不添加一行到表示是否要使用Android或iOS的cucumber.yml?

mobile_platform: 'android' 

而且在環境中的文件,你可以這樣做:

require 'yaml' 
cucumber_options = YAML.load_file('cucumber.yml') 
$mobile_platform = cucumber_options['mobile_platform'] 

然後在你的步驟定義文件,就可以開始這樣做:

Then (/^I click the Login Button$/) do 
if $mobile_platform = 'android' 
    #android locators and code here 
else 
    #iOS locators and code here 
end 
end 
0

正如托馬斯說,讓這個簡單的關鍵是把事情推下去。要做到這一點,你需要應用一個非常有紀律的簡單模式。

該模式是使每個步驟定義實現一個單一的調用輔助方法。一旦你在你的助手方法中,那麼你可以使用像使用環境變量,配置或一些條件來選擇實現的技術。

一個例子可能會說明這一點。可以說,這兩個應用程序都有能力添加一個朋友。當你第一次添加此功能,您將有一個像

When 'I add a friend' do 
    fill_in first_name: 'Frieda' 
    fill_in last_name: 'Fish' 
    ... 
end 

這需要成爲

When 'I add a friend' do 
    add_friend name: 'Frieda' 
end 

通過

module FriendStepHelper 
    def add_friend(...) 
    // Only start thinking about IOS or Android from here down. 
    ... 
    end 
end 

實現現在這似乎有點痛苦的一步,但你所做的就是將這個問題從Cucumber的領域(它不是爲了解決這類問題而設計的)中解決,並將它移植到Ruby的領域當中,這個領域當然是爲了處理這種類型的pr oblem。

現在,您已經掌握了編程語言,您可以使用各種技術來使用條件優雅而簡單的例如

#use hungarian prefix's 
def ios_add_friend 
def droid_add_friend 

#return early from os specific functions if wrong OS 
def ios_add_friend 
    return if droid? 
    ... 
end 

# run both implementations when they are different 
def add_friend 
    ios_add_friend 
    droid_add_friend 
end 

# loads of other alternatives 

... 
相關問題