2014-11-03 30 views
0

我想多次將文本寫入文件(附加)。 目前我正在使用:在這種情況下,我在endtime和starttime中捕獲了系統時間。使用Ruby的Selenium Webdriver:將文本寫入文件

File.open("c:\\temp\\myfile.txt", "w").write("My first input: #{endtime-starttime} seconds \n") 

我想在我的腳本的各個地方繼續多次,但它似乎沒有正常工作。該文本文件似乎在自己寫。腳本中有不同的行寫文本的方法嗎?

感謝, 斯科特

這裏有一個更好的例子

#------------------------------------------------------------------------------------------------  --------------------- 
#Log into System 
#--------------------------------------------------------------------------------------------------------------------- 
starttime=Time.new 
LoginButton = driver.find_element(:xpath, "/html/body/div[3]/div/div[2]/ul/li[3]/a") 
LoginButton.click 

option = driver.find_element(:xpath, "/html/body/div/div[1]/div/div[3]/div[1]/form/div[3]/ul/li[1]/input") 
option.send_keys"blah" 

option = driver.find_element(:id, "password") 
option.send_keys"blah" 

option = driver.find_element(:xpath, "/html/body/div/div[1]/div/div[3]/div[1]/form/div[3]/ul/li[3]/input") 
option.click 

endtime=Time.new 
puts"Login: #{endtime-starttime} seconds" 
File.open("c:\\temp\\myfile.txt", "w").write("Login: #{endtime-starttime} seconds \n") 

puts"Login Done" 

#--------------------------------------------------------------------------------------------------------------------- 
#Performance Test Course 
#--------------------------------------------------------------------------------------------------------------------- 
starttime=Time.new 
driver.switch_to.frame "contentFrame" #Sets focus on the "UR Courses Online Module" 
option = driver.find_element(:link_text, "Performance Test Course") 
option.click 

endtime=Time.new 
puts"Performance Test Course Link: #{endtime-starttime} seconds" 
File.open("c:\\temp\\myfile.txt", "w").write("Performance Test Course Link: #{endtime-starttime}  seconds \n") 

puts"Performance Test Course Done" 
+0

附加代碼示例: – user3531858 2014-11-03 14:45:16

回答

0

你可能想打開的文件對象存儲在一個變量,這樣,當你寫一個新行到您的文件時,它被寫入結束。您以前的代碼將打開給定的文件,在文件的位置0處寫入行,然後關閉該文件。

file = File.open("text.txt", "w")

file.write "hello world"

或者你也可以用「a」標誌,該標誌在文件末尾打開了寫一個文件。

File.open("c:\\temp\\myfile.txt", "a+").write("My first input: #{endtime-starttime} seconds \n")

0

您正在使用W的寫入之前截斷該文件。

用途:

File.open("c:\\temp\\myfile.txt", "a+").write("My first input: #{endtime-starttime} seconds \n") 
+0

謝謝大家誰回答我的問題。 「a +」是我需要的答案。 – user3531858 2014-11-03 16:24:47

相關問題