18

Every protractor example I can find on the internet seems to use browser.get with a web URI.

browser.get('http://localhost:8000');

I'd like to use Selenium to simple navigate to a file:// path so that I don't need a local web server running in order to perform tests. All I need is a simple HTML page and some assets.

That doesn't seem to work though.

browser.get('file:///Users/myusername/dev/mpproject/spec/support/index.html');

When I paste that URI into my browser window I get a HTML page. When I try to open it with protractor I get a timeout.

How can I run tests on this page with protractor? The ideal answer will work with a relative file path from the myproject root.

David Tuite
  • 22,258
  • 25
  • 106
  • 176

4 Answers4

16

I am posting the solution I've found in here which helped me run Protractor with a file protocol.

By default, Protractor use data:text/html,<html></html> as resetUrl, but location.replace from the data: to the file: protocol is not allowed (we'll get "not allowed local resource" error), so we replace resetUrl with one with the file: protocol:

exports.config = {
    // ...

    baseUrl: 'file:///absolute/path/to/your/project/index.html',

    onPrepare: function() {

        // By default, Protractor use data:text/html,<html></html> as resetUrl, but 
        // location.replace from the data: to the file: protocol is not allowed
        // (we'll get ‘not allowed local resource’ error), so we replace resetUrl with one
        // with the file: protocol (this particular one will open system's root folder)
        browser.resetUrl = 'file://';
    }

    // ...
};

If you want to run a relative path to your project folder, then you could just use Node.js tools, because Protractor runs in a Node.js environment. For example, __dirname will return an absolute path to a directory where your Protractor config file is saved. As a result use:

exports.config = {
    // ...

    baseUrl: 'file://' + __dirname + '/spec/support/index.html'

    // ...
};

Also, if you application does XHR requests to some endpoints, which are not allowed from file:, you may have to run your test browser with custom flags. In my case it was Chrome:

exports.config = {
    // ...

    capabilities: {
        browserName: 'chrome',
        chromeOptions: {
            // --allow-file-access-from-files - allow XHR from file://
            args: ['allow-file-access-from-files']
        }
    }

    // ...
}
Michael Radionov
  • 12,859
  • 1
  • 55
  • 72
  • 1
    Bless you for this wonderful fix! Everything is exactly what I needed – Henry Blyth Sep 16 '16 at 17:02
  • How do I open the local html file in my test specification then? I tried just not using `browser.get` and `browser.get()` without arguments, assuming it would then open the specified baseUrl. But neither worked it just doesn't load the page and consequently the tests fail. – stefanbschneider Jan 31 '18 at 14:48
  • 1
    Actually, it worked with `browser.get('')` but only after adding `browser.ignoreSynchronization = true;` inside `onPrepare`. – stefanbschneider Jan 31 '18 at 15:16
6

I had the same error and fixed by applying Michael Radionov's fix but removing the baseUrl. Here is my setup:

protractor.config.js:

exports.config = {

  capabilities: {
    browserName: 'chrome'
  },

  specs: [
    '*.js'
  ],

  onPrepare: function() {
    // By default, Protractor use data:text/html,<html></html> as resetUrl, but 
    // location.replace from the data: to the file: protocol is not allowed
    // (we'll get ‘not allowed local resource’ error), so we replace resetUrl with one
    // with the file: protocol (this particular one will open system's root folder)
    browser.ignoreSynchronization = true;
    browser.waitForAngular();
    browser.sleep(500); 
    browser.resetUrl = 'file:///';
  }

};

e2etest.js:

'use strict';

describe("Buttons' tests are started", function() {

    it('Should retrieve 20 records into table', function() {

        browser.get('file:///C:/Users/path_to_file/index.html');

        /* Test codes here */

    });

});
Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61
  • 1
    This solved my problem. I'd still combine it with a baseUrl to have nicer URLs in the test specifications. For me, it also worked without the sleep function, which probably speeds up the testing. – stefanbschneider Jan 31 '18 at 15:18
1

What is the error log?

This could be something related to 'Loading' angular. For that you can try browser.driver.ignoreSynchronization = true;

The error log will surely help in trying to understand the problem.

Sakshi Singla
  • 2,462
  • 1
  • 18
  • 34
-4

I think there is typo mistake. In "get" method you should include the URL in double quotes "".

Try using double quotes like below:

WebDriver driver=new FirefoxDriver();
driver.get('file:///E:/Programming%20Samples/HTML%20Samples/First%20Program.html');
Uday
  • 1,433
  • 10
  • 36
  • 57
  • [Double and single quotes are equivalent in JavaScript](http://stackoverflow.com/q/242813/574190) so that couldn't make a difference. I did try though and it doesn't work. – David Tuite Jul 19 '14 at 16:51
  • As far as i know, when you use dirver.get method you should provide double quotes... When we use single quotes it complains a compile error message. – Uday Jul 20 '14 at 05:57