Dmitry
Dmitry

Reputation: 1283

Is there a way to add some js into all pages after loading via selenium?

I am working on creating automated test using selenium. Currently i want execute some js code in every page/frame after it is loaded in order to have ability to call some testing functions in js. It is not nessecary to execute it immediatly after page loaded, I just want to be shure it will executed on every page.

To solve this problem I wrote my own implementator of WebDriver which, using delegate pattern, add js executing after method "get" is called. The same way I add script executing in all method of driver.switchTo() object that changes current frame.

The problem is in case when page reloads after clicking on some links/executing some js code. It is easy to undestand that in such case my script wouldn't be executed.

Is there a way to cover this behaviour using selenium WebDriver?

Any ideas or hacks are greating.

Upvotes: 2

Views: 648

Answers (3)

Dakshinamurthy Karra
Dakshinamurthy Karra

Reputation: 5463

I did something similar using WebSocket. Create a WebSocket server in the application. After the initial get, execute_script that opens a connection to the server. When the browser moves to a new page, the socket connection is closed - you can execute_script again. The whole thing is a bit more complicated because you need to check for any iframes, retrying the connection if it fails etc.

I was able to make this work well with Firefox, Chrome. Should work with Edge/IE also.

Upvotes: 0

PxP
PxP

Reputation: 31

You can have fiddler run in the background to proxy all HTTP methods..... At fiddler you can add a custom rule to alter response from server (by inserting JS ) for every specific type of HTTP call GET /POST (exclud PNG/IMG etc.)... I do this all time..to replicate hard to test scenarios.

Upvotes: 1

Fabien
Fabien

Reputation: 975

I would do this server-side : Extends all your servlets with a generic servlet that prepend your js code only when Selenium is running(set a static variable on server when selenium is starting)

Upvotes: 0

Related Questions