Beating legacy code into shape with the help of Selenium
By Sasha Pachev
Goals
How it all started
Challenge: To be able to make significant code changes in a legacy mission-critical CRM web app written in PHP with MySQL backend, and then have some degree of assurance that it still works as expected.
Mode of testing at the start of the project: Manual. Time consuming. Heavily reliant on the application knowledge of the testers.
Arguments For Automated Testing
Common Objections To Automated Testing
Addressing The Common Objections
Addressing The Common Objections - cont.
Our Solution to Automated Testing
Selenium!
Pipe dream
This does not work, because:
Actual solution
Challenges
Solutions - Headless testing
Firefox started by Selenium will now display its output on the dummy X display :99
Solutions - Optional remote visual display
Naive: install X on the remote machine, set DISPLAY inside the test driver to point to that X server
Problem with naive: every X event gets forwarded over the network, runs slow
Better: Run Selenium on the remote, then instantiate the driver with:
self.driver = webdriver.Remote(command_executor=self.remote,desired_capabilities=d)
self.remote would be something like http://mydesktophost:4444/wd/hub
Solutions - Properly Handling Timing Dependencies
Solutions: Working around Selenium/Firefox bugs/limitations
Solutions: Interacting with Angular JS
Solutions: Interactive Test Runs
Solutions: Extracting PHP configuration files data into Python
cmd = "(cat " + git_root + "/includes/global/constants.php; " + \� "echo '$a=get_defined_constants(); echo join(\"\\n\",array_map(function ($k,$v) { $v=trim($v);" + "return \"$k=\\\"$v\\\"\";}," + \� " array_keys($a),array_values($a)));') | php”�exec subprocess.check_output(cmd, shell=True)�
Conclusion
Have fun testing!