Home > Back-end >  Rspec: do a mkdir and then and then pass screenshots to the new directory
Rspec: do a mkdir and then and then pass screenshots to the new directory

Time:03-11

I've created an rspec test where I've created a directory inside of an it block and am also taking screenshots of the various states of the test.

It's a form entry I'm testing so the it block looks like this:


 ...
 it "confirm that a user can successfully sign up" do
    timestamp = Time.now.to_i 
    dir = Dir.mkdir("dir_#{timestamp}")

    driver = Selenium::WebDriver.for :firefox
    driver.navigate.to "go/to/url"

    username_field = driver.find_element(id: "user_username")
    username_field.send_keys("user #{timestamp}")
    driver.save_screenshot("./#{dir}/screen_username.png")
    ...
  end

So if timestamp is 1234, then I'm assuming a directory named dir_1234 will be created and it will, at some point, put an image inside of it named screen_username.png inside of it. But when I run rspec, I get the following error:


Failure/Error: driver.save_screenshot("./#{dir}/screen_username.png")

     Errno::ENOENT:
       No such file or directory @ rb_sysopen - ./0/screen_username.png
     ...

Any ideas? Thanks in advance.

CodePudding user response:

Dir::mkdir always returns 0

dir = Dir.mkdir("dir_#{timestamp}") # => 0

That's your problem

You can save path to some variable

dir_path = File.join(__dir__, "dir_#{timestamp}")

Dir.mkdir(dir_path)

# your code

driver.save_screenshot(File.join(dir_path, "screen_username.png"))
  • Related