Cannot assign an inst variable in Switch "--user-data-dir" in Selenium Wedriver Chrome

454 views Asked by At

I have the following code and i want to create a user Directory with the name 'mydir' where chrome should output console logs, but wat happens is it creates a directory #{dir} instead. I also tried using CONST Variables still the same result. Can some help me out?

require "selenium-webdriver"
class MyClass
dir='mydir'
@driver = Selenium::WebDriver.for :chrome, :switches => %w[--ignore-certificate-errors --user-data-dir=#{dir} --enable-logging]
end
1

There are 1 answers

0
Justin Ko On BEST ANSWER

The %w literal is similar to using single quotes for Strings. There is no interpolation. As a result, the --user-data-dir is exactly what was typed - "#{dir}". To enable interpolation, you need to use a capital W - ie %W:

You can see the difference with:

dir = 'mydir'

# Without interpolation
p %w[--user-data-dir=#{dir}]
#=> ["--user-data-dir=\#{dir}"]

# With interpolation
p %W[--user-data-dir=#{dir}]
#=> ["--user-data-dir=mydir"]

In the end, your script would become:

require "selenium-webdriver"
class MyClass
dir='mydir'
@driver = Selenium::WebDriver.for :chrome, :switches => %W[--ignore-certificate-errors --user-data-dir=#{dir} --enable-logging]