Getting rid of two problems
I’m on my posting spree. In this post i’m going to try to solve the problems i brought up in my earlier post called “Getting rid of global variables”.
Solution for problem #1
First problem was the problem where i would like to run my specs without using my formatter. Currently it is not possible because specs are using some methods from WatirHtmlFormatter. There are some methods, which are dependent of this formatter class. For example there are the methods called formatter, file_path and absolute_file_path.
The easiest solution for this problem would be to create similar functionality to specs also and not use formatter’s methods directly so we can just use one if statement to determine if using formatter or not. I’ve also renamed module ApplicationHelper to SpecHelper:
And in my spec i’m just removing the usages of formatter:
These changes make our specs runnable with any kind of formatter or IDE - problem solved
Solution for problem #2
The second problem was related with the fact that there was a need to use RSpec’s options to set Watir’s browser object in 3 places - in before :all, after :each and after :all. Since it is a limitation of RSpec itself then there is always a solution to modify RSpec and change it’s behaviour. For this core functionality I don’t think that this is the best idea. I’m rather thinking that having a setter method for formatter would be a better one:
This means that i can delete the code from WatirHtmlFormatter where example_failed method sets the browser object. I can also simplify my spec file considerably and only set the browser object when open_browser_at method is used. I can also delete some unneeded methods like set_browser_for_rspec, close_browser and delete before :each and after :each blocks. Since only open_browser_at method is responsible for setting the browser object for formatter then i just have to make sure that this method is used in case browser object is modified - currently only in before :all block. I could also modify this method to work when attaching new browsers (popups for example), but i’m not going to do that currently. Before i had to set the browser object at least in 3 places, but now i’m doing it only at one place - problem solved.
You can see all the changes from GitHub’s commit log.




2 years ago