2013-03-23 23 views
-1

我是Ruby-Watir的新手..並且正在檢查來自Excel Sheet的多個值(無效用戶&密碼,有效用戶名&密碼)的一個應用程序的登錄功能。在Excel-Ruby Watir腳本中使用循環條件

reqire'watir-webdriver' 
require 'win32ole' 
require 'roo' 

b = Watir::Browser.new 

b.goto 'http://tech/mellingcarsweb/Admin/Login.aspx' 

xl = WIN32OLE.new('excel.application') 

wrkbook= xl.Workbooks.Open("C:\\Excel\\cars.xlsx") 

wrksheet= wrkbook.Worksheets(1) 

wrksheet.select 

$username= wrksheet.Range("A1").Value 

$password= wrksheet.Range("B1").Value 

b.text_field(:id, "MainContent_txtUsername").set($username) 


b.text_field(:id, "MainContent_txtPassword").set($password) 


b.button(:id, "MainContent_btnLogin").click 

b.alert.ok 

$username1= wrksheet.Range("A2").Value 

$password1= wrksheet.Range("B2").Value 

b.text_field(:id, "MainContent_txtUsername").set($username1) 


b.text_field(:id, "MainContent_txtPassword").set($password1) 


b.button(:id, "MainContent_btnLogin").click 

puts "Authorised Entry" 

此代碼工作fine.What我需要的是.. 使用循環語句我需要執行多次。

我不知道如何使用循環中Excel.And條件我已經看到了很多例子,但他們都不是clear.Sorry我不能understand.Will誰能解釋我在循環使用條件的正確方法在Excel的紅寶石。

感謝

+0

如果你只想要簡單的東西,這不是一個好地方得到它。這是解決複雜問題的好地方。否則,你會說「我不想投入任何努力來解決這個問題,你其他志願者必須爲我做這件事。」 IMO並不是一個好消息。 – 2013-03-25 18:37:34

回答

1

鑑於您正在測試有效和無效的登錄,我不確定它是最有意義的編寫循環。循環的問題在於,每次迭代都需要知道預期結果是什麼。您可能需要將這些信息包含在電子表格中。爲每個有效和無效的場景創建特定的單獨測試可能會更好(即每個測試一個斷言)。

要回答您的問題,您可以將excel範圍轉換爲二維數組,然後遍歷每行數據。要獲得2D陣列:

require 'win32ole' 

#Open the spreadsheet 
xl = WIN32OLE.new('excel.application') 
wrkbook= xl.Workbooks.Open('C:\Users\Someone\Desktop\Logins.xlsx') 
wrksheet= wrkbook.Worksheets(1) 

#Convert the range of cells to a 2D array 
login_data = wrksheet.range("A1:B2").value 

#Output of the 2D array 
p login_data 
#=> [["jsmith", "password1"], ["jdoe", "password2"]] 

2D陣列中的每個元素是Excel數據的一行。您可以使用each方法遍歷它。

#Start your browser 
b = Watir::Browser.new  

#Loop through the login data 
login_data.each do |data| 

    #Determine the user id and password for the row of data 
    user = data[0] 
    password = data[1] 

    #Go to the login page 
    b.goto 'http://tech/mellingcarsweb/Admin/Login.aspx' 

    #Input the fields and submit 
    b.text_field(:id, "MainContent_txtUsername").set(user) 
    b.text_field(:id, "MainContent_txtPassword").set(password) 
    b.button(:id, "MainContent_btnLogin").click 

    #Do something to validate the result 
end 
+0

感謝賈斯汀爲您的幫助..當輸入錯誤的輸入警告框出現..我用這個代碼------------ -----------------------如果b.alert.text。包括(「無效的用戶名和密碼」) b.alert.ok 其他 提出「未找到」 結束enering到應用程序得到錯誤像'搶救assert_exists'後:無法找到警報( Watir ::異常:: UnknownObjectException)..任何想法 – 2013-04-02 07:30:31

+0

它沒有出現警報沒有出現的情況下失敗(我假設成功登錄)?你可能想要一個if條件來檢查警報是否存在 - 'b.alert.exists?' – 2013-04-02 14:09:25

+0

感謝賈斯汀Everthing工作正常..首先,我不知道如何問這個問題..但是你抓住了問題在我心中..偉大的工作.. – 2013-04-03 04:39:34

0

谷歌的ruby loops。這看起來像一個很好的介紹:http://www.tutorialspoint.com/ruby/ruby_loops.htm

+0

我只想使用[for循環語句]在我的代碼上面運行.Plz以更簡單的方式解釋我... – 2013-03-23 10:19:14

+0

沒有更簡單的方法。你必須學習循環。 – 2013-03-25 10:57:12

+0

經過長時間的努力學習和執行仍然存在問題誰會給我解決方案: – 2013-03-29 09:40:19