Using Capybara-Webkit, Inject Javascript before cl

2019-07-09 17:36发布

问题:

Is there a way to inject the javascript sooner?

visit("/path/to/my/page")
page.execute_script("document.getElementsByTagName('html')[0].className += ' hybrid-app';")

I'm testing some things that only effect a web app when it is used from within a hybrid mobile app. My hybrid app related code does not run unless this class exists. From the native iOS code I inject some javascript which adds a class to the HTML tag before the page loads.

The problem is that when do the same from the Capybara test, it is added too late.

Currently I am working around this by explicitly calling my hybrid app setup code. This is not ideal because this is not how the hybrid app works and could cause side effects.

visit("/path/to/my/page")
script = %q(
     document.getElementsByTagName('html')[0].className += ' neeman-hybrid-app';
     hybridAppSetup();
)
page.execute_script(script)