2014-06-22 5 views
0

Robot Framework의 특정 바이트 위치에서 파일을 읽으려면 어떻게해야합니까?Robot Framework의 위치에서 파일 읽기

장기간 로그 파일을 작성하는 프로세스가 있습니다. 현재 파일 크기를 얻고 싶습니다. 그런 다음 프로세스의 동작에 영향을주는 것을 실행하고 로그 파일에 메시지가 나타날 때까지 기다립니다. 이전 파일 크기에서 시작하여 파일의 일부분 만 읽으 려합니다.

Robot Framework를 처음 사용했습니다. 나는 이것이 매우 일반적인 시나리오라고 생각하지만, 그것을하는 법을 찾지 못했습니다.

답변

1

이렇게하려면 내장 키워드가 없지만 파이썬으로 작성하는 것은 매우 간단합니다.

예를 들어, 다음과 명명 된 파일 "readmore.py"를 만들 :

from robot.libraries.BuiltIn import BuiltIn 

class readmore(object): 
     ROBOT_LIBRARY_SCOPE = "TEST SUITE" 

     def __init__(self): 
      self.fp = {} 

     def read_more(self, path): 
      # if we don't already know about this file, 
      # set the file pointer to zero 
      if path not in self.fp: 
        BuiltIn().log("setting fp to zero", "DEBUG") 
        self.fp[path] = 0 

      # open the file, move the pointer to the stored 
      # position, read the file, and reset the pointer 
      with open(path) as f: 
        BuiltIn().log("seeking to %s" % self.fp[path], "DEBUG") 
        f.seek(self.fp[path]) 
        data = f.read() 
        self.fp[path] = f.tell() 
        BuiltIn().log("resetting fp to %s" % self.fp[path], "DEBUG") 

        return data 

당신은 다음과 같이 사용할 수 있습니다 :

*** Settings *** 
| Library | readmore.py 
| Library | OperatingSystem 

*** test cases *** 
| Example of "tail-like" reading of a file 
| | # read the current contents of the file 
| | ${original}= | read more | /tmp/junk.txt 

| | # do something to add more data to the file 
| | Append to file | /tmp/junk.txt | this is new content\n 

| | # read the new data 
| | ${new}= | Read more | /tmp/junk.txt 
| | Should be equal | ${new.strip()} | this is new content 
관련 문제