2013-07-24 5 views
-1

오이 테스트를 위해 셀레늄을 사용하여 스크린 샷을 찍고 있습니다. 내 단계 중 하나가 단계 + 타임 스탬프의 입력을 사용하여 생성 된 폴더 이름이있는 폴더에 스크린 샷 파일을 저장하려고합니다. 여기디렉토리 만들기 및 파일 이동

는 지금까지 성취 한 것입니다 : 디렉토리가 존재하지 않습니다

Then /^screen shots are placed in the folder "(.*)"$/ do |folder_name| 
    time = Time.now.strftime("%Y%m%d%H%M%S") 
    source ="screen_shots" 
    destination = "screen_shots\_#{folder_name}_#{time}" 

    if !Dir.exists? destination 
     Dir.new destination 

    end 
    Dir.glob(File.join(source, '*')).each do |file| 

     if File.exists? (file) 

       File.move file, File.join(destination, File.basename(file)) 
     end 
    end 
end 

경우, 나는 그것을 만들려고합니다. 그런 다음 모든 스크린 샷을 새 디렉토리에 저장하려고합니다.

폴더는 스크린 샷과 동일한 디렉토리에 만들어지고 모든 스크린 샷 파일은 폴더로 이동됩니다. 난 아직 루비를 배우고, 함께이를 넣어 내 시도는 전혀 운동을하지 않는 : 한마디로

Desktop > cucumber_project_folder > screenshots_folder > shot1.png, shot2.png

, 나는 screenshots에 새 디렉토리를 만들고 그것으로 shot1.pngshot2.png을 이동하려는. 그렇게하려면 어떻게해야합니까?

이 주어진 답변에 따라 소스 경로가 너무 다른 사용자가 정의를 수정할 필요가 없습니다 단계에 표시됩니다 (오이에 대한) 솔루션

Then /^screen shots are placed in the folder "(.*)" contained in "(.*)"$/ do |folder_name, source_path| 
    date_time = Time.now.strftime('%m-%d-%Y %H:%M:%S') 
    source = Pathname.new(source_path) 
    destination = source + "#{folder_name}_#{date_time}" 
    destination.mkdir unless destination.exist? 
    files = source.children.find_all { |f| f.file? and f.fnmatch?('*.png') } 
    FileUtils.move(files, destination) 
end 

입니다.

+1

당신이 보일 것입니다 [** Fileutils의 **] (http://www.ruby-doc.org/stdlib-2.0/libdoc/fileutils/rdoc/FileUtils.html#method-c-mkdir_p) 및 혼자서 시도해보십시오. 매우 쉽습니다 ..이 stdlib를 사용하여 모두 수행 할 수 있습니다. :)) –

+1

StackOverflow는 질문을하기에 좋은 장소이지만, 우리가 당신을 위해 일하기를 기대해서는 안됩니다. 우리가 가지고있는 것과 작동하지 않는 것을 보여주고 우리가 여러분을 도우 려 노력할 것입니다. 그러나 @Priti가 말했듯이, 이것은 Ruby 표준 라이브러리를 사용하여 작성하는 것이 매우 간단합니다. –

+0

감사! 나는 명심 할 것이다 :) – meggex

답변

2

나는 코드는 루비 코드가 아닙니다의로

Then /^screen shots are placed in the folder "(.*)"$/ do |folder_name| 

의 첫 번째 라인에 무슨 일이 일어나고 있는지 잘 모르겠지만, 나는 그것이 파일에서 개념적인 선으로 작업했습니다.

  • Pathname 클래스는 destination.exist? 대신 File.exist?(destination) 같은 것들을 할 수 있습니다. 또한 +으로 합성 경로를 구축하고 children 메소드를 제공합니다.

  • FileUtils 모듈은 move 기능을 제공합니다.

  • 루비에서는 슬래시를 Windows 경로에서 사용할 수 있으며 대개 어디에서나 백 슬래시를 이스케이프 처리하지 않고 대신 사용할 수 있습니다.

디렉토리 이름에 날짜와 시간 사이에 하이픈을 추가했습니다. 그렇지 않으면 거의 읽을 수 없습니다.

require 'pathname' 
require 'fileutils' 

source = Pathname.new('C:/my/source') 

line = 'screen shots are placed in the folder "screenshots"' 

/^screen shots are placed in the folder "(.*)"$/.match(line) do |m| 

    folder_name = m[1] 
    date_time = Time.now.strftime('%Y%m%d-%H%M%S') 

    destination = source + "#{folder_name}_#{date_time}" 
    destination.mkdir unless destination.exist? 
    jpgs = source.children.find_all { |f| f.file? and f.fnmatch?('*.jpg') } 
    FileUtils.move(jpgs, destination) 

end 
+0

고마워! 나는 이것을 작동시키기 위해서 조금 바꿨다. 첫 번째 라인은 Gherkin입니다. 제가 언급했듯이 오이 테스트를 위해이 글을 쓰고 있습니다. 나는 나의 수정을 게시 할 것이다. – meggex

관련 문제