2017-06-17 50 views
0

基本上我寫了一個叫 @When("I go to {url}")如何從Python-Behave步驟傳遞變量?

然後我用 When I go to http://youtube.com把它稱爲從特徵文件的步驟和它的工作

但我想用叫它 When I go to YouTube

同樣會用CSS發生選擇器(Then logo is visible看起來比Then div#id.class is visible漂亮)

如何鏈接地圖文件contai寧這個CSS選擇器和URL的變量爲我的步驟使用? 事情是這樣的:

YouTube = "http://youtube.com" 
logo = "div#id.class" 

我想這

def before_all(context): 
    global YouTube 
    YouTube = "http://youtube.com" 

然後,我會eval(url)步驟裏面,但它不停地說YouTube上沒有定義

回答

1

您應該使用的字典預定義的網址而不是變量。添加到您的步驟實現文件:

websites = {'youtube': 'http://youtube.com', 'somesite': 'http://somesite.com'} 

@When("I go to {website}") 
def when_i_go_to_website(context, website): 
    context.url = websites[website] 

context.url將在所有以下步驟可用。

您可能想用try/except來包圍代碼行以捕獲KeyErrors。

相關問題