0

Until now, I was testing my non-Angular website (I only have JS, no node, not even ES6) using Jasmine, Karma and Travis CI.

I am now trying to write functional tests (in case my vocabulary is off, I mean scenario tests for which I want to test "visual/UI" tests) and google directed me to Protractor.

Importing

For now, I will completely disregard Travis CI. The only way I have found to "reach" my page was with a local path browser.get('file:///C:/local/path/to/project/index.html');

For now, I have this test

describe('The main page', function () {

  beforeEach(function () {
    // jasmine.Ajax.install();  // unabled to reuse that
    dv.ignoreSynchronization = true;
  });
  /*
  afterEach(function () {
    jasmine.Ajax.uninstall();
  });
  // */

  it('should display an error message in the alert if no parameters are provided', function () {
    browser.waitForAngularEnabled(false);
    get('/index.html'); // edited
    browser.sleep(500);
    element(by.tagName('ajs-content')).getText().then(console.log);
    expect(element(by.className('ajs-content')).getText()).toEqual(new EXCEPTIONS.NoParametersDetectedInURI().description);

  });
});

Here, I get an error Failed: EXCEPTIONS is not defined but, unlike in karma, if I include my source files in the protractor.conf.js as such

specs: [
    'src/lib/**/*.js',
    'src/js/**/*.js',
    'test/protractor/**/*.spec.js',
],

I get errors like document is not defined and I don't think I'm supposed to import these at all since the index.html "hosted" (not even sure, I mean I am using a local, absolute, path... I am confused by this statement) on the Selenium webdriver is what imports all of these for it's own usage (I can see it working thanks to the console.log).

I want to use (import) my own code in order to use the EXCEPTIONS object and not hard-code toEqual("<some error message that I should never ever update in the exception.js file>") but, since I'm neither using node or ES6, I never once have some kind of module.export.

As you can see, my import isn't absolutely necessary but it felt "cleaner" to compare to the object's constant and not a string duplicate. And, maybe, these UI tests are meant to be "hard-coded" but I am still trying to find a way to import a file in a "vanilla JS" kinda way. If it's not meant to, so be it.

Ajax mocking

I need to intercept ajax requests and mock responses but jasmine.Ajax is undefined. I am perfectly able to use it in my "regular" tests (Jasmine+Karma) so I would like to know if I'm supposed to installed other npm-packages like protractor-http-client for instance, or if there is a special configuration needed to use jasmine.Ajax.

Travis

Lastly, I am relatively certain that using an absolute (windows) path won't work with Travis CI and, based on this SO question, I updated my code to try and reach the index.html with a relative path using global.basePath = __dirname; and use browser.get(global.basePath + '/index.html'); (also tried with '\\', with an initial file:/// etc... but, if I make the page sleep for a few second, I am always at the basePath, unlike when I use an absolute one).

I realise these wouldn't be "relative" paths but rather a "dynamic" absolute path but, in the end, even when replacing the "\" with "/" and literally having the exact same string as when I type it in myself:

let pagePath = global.indexPath.replace(/\\/g, "/");
console.log("trying to get", pagePath);
browser.get(basePath);
browser.sleep(5000);

Have you been confronted to this ? Will 'file:///C:/local/path/to/project/index.html' automatically be "parsed" into the proper path once running in Travis ? How can I use a relative path ?

Should I separate each title into a question ?

Edit:

exception.js sample. They are basically constructors for errors where the description attribute is always defined. I knew I forgot something when I posted ahah

let EXCEPTIONS = {
  DefaultException: function(_type, _description, _msg) {
    this.type = _type;
    this.description = _description;
    this.message = _msg || undefined;
  },

  NoParametersDetectedInURI: function (msg) {
    EXCEPTIONS.DefaultException.call(this,
      "NoParametersDetectedInURI",
      "No URI parameters detected",
      msg);
  },
  .
  .
  .
};

Edit 2:

Answered the relative path part (though haven't tested on Travis yet). I had to set `baseUrl:

exports.config = {
    baseUrl: 'file:///' + __dirname,
    .
    .
}

in the config file and then get('/index.html'); in the test.

DixiPoowa
  • 133
  • 12
  • Can you provide code in `exceptions.js` for more detailed answer about import? And i think separate it to 3 questions would be more appropriate. – RedVarVar Jun 25 '19 at 10:33
  • @RedVarVar ok I added exception.js' code. As you can see they are just constructors and I want to "import" it so that I don't have to manually write my expected result. I might have "real" usage for imports later though. – DixiPoowa Jun 25 '19 at 12:12
  • Ok so, for the relative path, it turns out that you have to set `baseUrl` in your config and not just **any** name (and not in global either) so I set it to `baseUrl: 'file:///' + __dirname,` and simply do `browser.get('/index.html');`. Thank https://stackoverflow.com/questions/24824491/opening-a-file-with-protractor too for that (I guess I'll remove it from the question so as to reduce the spam ^^) – DixiPoowa Jun 25 '19 at 12:30

1 Answers1

0

I'm not sure, is it Node.js related syntax, but as for import/export you can either use export/require like this

export let EXCEPTIONS = {
---- your code ----
}
const EXCEPTIONS = require(pathToExceptionsFile)

describe('The main page', function () {
    ---- Test code here ---
    expect(element(by.className('ajs-content')).getText()).toEqual(new EXCEPTIONS.NoParametersDetectedInURI().description);
})

or like this

let EXCEPTIONS = {
---- your code ----
}

module.exports = EXCEPTIONS
const EXCEPTIONS = require(pathToExceptionsFile)

describe('The main page', function () {
    ---- Test code here ---
    expect(element(by.className('ajs-content')).getText()).toEqual(new EXCEPTIONS.NoParametersDetectedInURI().description);
})

I think it's should work in vanila JS as well. But as long as you using Protractor, you already use Node.js, so i think it should work anyway.

RedVarVar
  • 572
  • 2
  • 7
  • Isn't this ES6 ? Both cause errors. `export` --> `unexpected token` and `module.exports` --> `module is undefined`. On top of that, when I add `type=module` to the html imports, I get an error saying the file's MIME is incorrect (`"text/html"`) and there is no plan for updating the server anytime soon I was told :/ From [here](https://stackoverflow.com/questions/38296667/getting-unexpected-token-export/40021867): it **should've worked** but since I can't add `type=module`... I have to support older browsers (ik polyfills exist) and am limited to my own code. – DixiPoowa Jun 25 '19 at 13:54
  • `export` may be ES6, but not `module.exports`. What version of Node do you use? – RedVarVar Jun 25 '19 at 14:07
  • Currently `node -v v10.15.3` – DixiPoowa Jun 25 '19 at 14:16
  • Hmm. And how you run your test? With which command? And why you use html imports? – RedVarVar Jun 26 '19 at 06:39
  • I first start the selenium server with `webdriver-manager start` and then run, from a powershell command prompt, my test code with `protractor.cmd .\protractor.conf.js`. Since I do `get('/index.html')` and it loads ok \[though, right now, I'm looking for a way to start it with a fake URL, something like "http://://index.html?", because I need it in order for the page's initialisation to start (it requires some specific URL parameters), because "file:////index.html?" won't work for my tests\], the html imports are OK. Which... 1/2 – DixiPoowa Jun 26 '19 at 06:52
  • ... is normal, I had just misunderstood the context of protractor, who is basically a real user using a real browser. For now, I really need to find a way to "host" the `index` file on a specific URL so I'll be using hard-coded values for the tests and nvm imports. If I can't set the URL, the app will never do anything other than provide an error because of the invalid URL. If you have *keywords* (not sure about host, not sure if this has to be done selenium or protractor side, etc...) to help me in my research, I'd like to hear them. Else I was legit going to ask yet another question - 2/2 – DixiPoowa Jun 26 '19 at 06:55
  • For context, my `index.html` is local but, when when integrated to the rest of the program, it is served on some specific URLs depending on the connected account's permissions etc... and this page uses said URL to dynamically construct some informations. **In short**, it is never actually ran locally. Which is why need to be able to set a custom URL, basically a way to say "the file is but let it be accessed via URL". I thought the `httpProxy` was what I was looking for, I'm still experimenting with it but I am not even sure which "side" (selenium/protractor) to edit – DixiPoowa Jun 26 '19 at 07:07
  • Oh, now i see what you want and how it works, but unfortunately, i don't know how to help you in this case, i'm sorry. The only thought is that the problem occurs because you load html page as file from local machine, since Selenium in most cases works with pages on localhost or URL in general, but i don't know what exactly cause the problem and how to solve this. – RedVarVar Jun 26 '19 at 07:46