【问题标题】:Read a file from a position in Robot Framework从 Robot Framework 中的某个位置读取文件
【发布时间】:2014-08-12 11:05:13
【问题描述】:

如何从 Robot Framework 中的特定字节位置读取文件?

假设我有一个运行了很长时间的进程写入一个长日志文件。我想获取当前文件大小,然后执行一些影响进程行为的操作,然后等待日志文件中出现一些消息。我只想读取从前一个文件大小开始的文件部分。

我是机器人框架的新手。我认为这是一个非常常见的场景,但我还没有找到如何去做。

【问题讨论】:

    标签: file-io robotframework


    【解决方案1】:

    没有内置关键字可以做到这一点,但是用 python 编写一个非常简单。

    例如,使用以下内容创建一个名为“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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 2016-07-06
      • 2018-05-03
      • 2018-06-13
      • 1970-01-01
      相关资源
      最近更新 更多