How to Monkey-Patch a core Class in Ruby on Rails. Since I was creating a monkey-patch for the String class. 3 Ways to Monkey-patch Without Making a Mess. I mostly follow Rails’ monkey-patching convention. What’s the best monkey patch you’ve ever. Lets use a real world example. I want to monkey patch WillPaginate::LinkRenderer.to. So far I have tried: Created a file in folder: lib/monkeys/will. The Rails Command Line. After reading this guide, you will know: How to create a Rails application. How to generate models, controllers, database migrations, and unit. Love Your lib Directory By. The lib/ directory is the Red Headed Stepchild of your Rails application. If you must monkey patch. Put it into the lib/ directory. Ruby-monkey-patch - One million code monkeys, all at their keyboards, writing Ruby. Keep the Phantom. JS browser open between tests – Learnings in and around Sharetribe – Medium. At Sharetribe, we take integration testing seriously. We provide a platform for multiple marketplaces, each with its own configuration and set of customizations. At the same time, we are rapidly developing our platform and deploying new features every week. When we release a new feature, we hardly ever do any manual regression testing. We have implemented a comprehensive test set which we trust to catch regressions. As a small startup, we simply don’t have time to waste testing something manually that can be automated. Implementing integration tests with Cucumber is not always fun, especially if your tests are running slow. When talking about test speed, we’re talking about two factors that make up the overall speed: The test suite startup time. The test step execution time. When you are implementing a new Cucumber scenario, you probably end up running it multiple times before the scenario passes. You write the scenario, run it and see red. Then you fix the first step that failed, rerun it again and see red for the next step. Good job, you got one step further! You keep iterating until all steps pass. At this point, you want your test suite to start fast! Every second counts. At Sharetribe, we are running our Cucumber features with the Capybara + Selenium Webdriver + Phantom. JS stack. We have already spent a lot of effort to make the test suite start up quickly: we use Zeus to overcome the slowness of loading the Rails environment. We set up our test data during Zeus startup so that we don’t need to initialize the test database state each time the test suite is started. In fact, our test suite already starts pretty quickly. For a single simple scenario, running the test takes no more than 5. We noticed that Selenium Webdriver starts the Phantom. JS browser each time the test suite is started. We thought that it would be cool to start Phantom. JS once and keep it running between the test runs, thus saving some seconds from test startup time. Selenium Webdriver monkey- patch. In the Selenium Webdriver gem, the Phantom. JS: :Service. start method is responsible for starting Phantom. JS as a child process. If you take a look at the method, you see the two lines there: @process = create. Instead of really creating a new Phantom. JS process, we want to override the create. Here’s how: class Selenium: :Web. Driver: :Phantom. JS: :Service def create. We create a new Struct to stub the Child. Process and fake the two methods that the Selenium Webdriver uses, start and exited? By default, the Selenium Webdriver tries to start the Phantom. JS process to listen on port 8. If that port is taken, it tries 8. Since want to start Phantom. JS in the background to listen on port 8. Selenium Webdriver tries to use the next free port instead. To overcome this, we have to monkey- patch the Port. Prober class: class Selenium: :Web. Driver: :Port. Prober def self. We have placed those two pieces of code in lib/selenium. In your config/test. Capybara. register. In any case, here are the results of running a single Cucumber scenario: zeus cucumber features/admin/categories/admin. It’s not that much, but in a simple test scenario like this, it’s still more than 2. CI environment. This technique can be also used in your CI environment. At Sharetribe, we use Travis CI, which has Phantom. JS pre- installed. Before running the test set, start Phantom. JS in Travis as follows: phantomjs - -webdriver=8. Conclusion. Let’s admit it: this is a hack. Monkey- patching a gem is always a hack. But it’s a hack that has been working really well for us during the past couple of months. Due to us having the browser open between test executions, I had my doubts that we would eventually run into trouble with “dirty” browser state (e. However, we have not encountered any such issues. If you think this tip could help you, give it a try and drop us a comment if you found it useful!- Mikko.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
September 2016
Categories |