简体   繁体   中英

Selenium Webdriver using Ruby: write text to file

I would like to write text to a file multiple times (appending). Currently I am using: In this scenario I captured the system time in endtime and starttime.

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

I would like to continue this multiple times in various places in my script but it does not seem to be working correctly. The text file seems to be writing over itself. Is there a way in a script to write text in different rows?

Thanks, Scott

Here's a better example

#------------------------------------------------------------------------------------------------      ---------------------
#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"

You probably want to store the open file object in a variable so that when you write a new line to your file it is written to the end. Your previous code would open the given file write the line at position 0 in the file then close the file.

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

file.write "hello world"

Or you can use the "a" flag which opens a file for write at the end of the file.

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

You are using w which truncates the file before writing to it.

Use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM