How to replace remote resources with local resource in a page with Selenium?

0 votes

Is it possible to run an automatic test on a web page with Selenium and force it to replace the remote resource with another one in local?

Like replacing this:

<html>

  ...

  <script href="http://cnd.foo.bar/lib.js"/>

  ...

</html>

with

<html>

   ...

   <script href="http://localhost:3000/lib.js"/>

   ...

</html>

Aug 8, 2018 in Selenium by Martin
• 4,330 points
303 views

1 answer to this question.

0 votes

You could change URL for that script with a command:

<tr>

    <td>runScript</td>

    <td>document.querySelector('script[src=&quot;http://cnd.foo.bar/lib.js&quot;]').setAttribute('src', 'http://localhost:3000/lib.js')</td>

    <td></td>

</tr>

But when you execute your Selenium script browser is allready downloaded CDN javascript and nothing will happen.

You could delete reference to CDN js add reference to local javascript to your DOM :

<tr>

    <td>runScript</td>

    <td>var sc = document.querySelector('script[src=&quot;http://cnd.foo.bar/lib.js&quot;]');sc.parentNode.removeChild(sc);</td>

    <td></td>

</tr>

<tr>

    <td>runScript</td>

    <td>var js = document.createElement('script'); js.src = 'http://localhost:3000/lib.js'; document.head.appendChild(js);</td>

    <td></td>

</tr>

That will load new javascript. But maybe some scripts from CDN will be executed on page load.

IMO best solution is run localhost on port 80 and then set CDN url to 127.0.0.1 in hosts file.

answered Aug 8, 2018 by Samarpit
• 5,890 points

Related Questions In Selenium

0 votes
1 answer

How to create a test case with TestNG in Selenium?

Hey Deepak, to create a Test Case ...READ MORE

answered May 21, 2019 in Selenium by Abha
• 27,930 points
200 views
0 votes
1 answer

How to print text from a list of all web elements with same class name in Selenium?

@Rajni, following test case is somewhat similar ...READ MORE

answered Jun 20, 2019 in Selenium by Avantika
18,413 views
0 votes
1 answer

How to scroll up/down a page using Actions class in Selenium?

Hi Naetik, you can use Actions class ...READ MORE

answered Jul 4, 2019 in Selenium by Anvi
• 14,090 points
7,851 views
0 votes
2 answers

How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver ?

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy("X", " y ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
1,620 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
3,477 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,488 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,569 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
2,807 views
0 votes
2 answers

How to open a browser window in full screen using Selenium WebDriver with C#

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 5 in Selenium by Sri
• 2,970 points
9,346 views
0 votes
1 answer

How to login a forum using Selenium with Python

You should try to directly log in ...READ MORE

answered Apr 27, 2018 in Selenium by Samarpit
• 5,890 points
765 views