using Axe-core inside Intern 4 - systemjs

I am trying to set up my accessibility testing using axe-core or axe-webdriverjs inside of Intern 4. I finally got something to compile but I get this socket hung up error. Let me walk you through my process.
Loading axe-core as a plugin. In my intern.json
I have my included a plugin axe-core. node_modules/axe-core/axe.min.js specified here https://axe-core.org/. This did not work. When running the test it did not find the module.
I then tried using the loader to import this module. In my intern.json I had the following:
"plugins": [
"node_modules/babel-register/lib/node.js"
],
installed the necessary plugins and then I imported axe in my test.js using:
`import axe from '/node_modules/axe-core/axe.min'`
still I get axe is undefined errors.
I tried to use axe-webdriverjs used the example code
const { suite, test, before } = intern.getInterface('tdd');
const { assert } = intern.getPlugin('chai');
var AxeBuilder = require('axe-webdriverjs');
var time = 1000;
var session;
suite('Accessibility', () => {
test('Accessibility Test 1', ({remote}) => {
remote.get('https://dequeuniversity.com/demo/mars/')
.then(function () {
AxeBuilder(driver)
.analyze(function (results) {
console.log(results);
});
});
});
});
still could not get it to work.
then I tried changing the
var AxeBuilder = require('axe-webdriverjs');
to
import AxeBuilder from 'node_modules/axe-webdriverjs/lib/index';
It is now running but I get a socket hang up error.
All I am trying to do is run axe-core or even axe-webdriver a thrid party library into tests suite within intern 4. Any sort of help will be appreciated.
Thanks

There is an Intern accessibility plugin that includes support for aXe. You can use that directly, or simply use it as an example of how to use aXe-core with Intern.
You wouldn't typically use axe-webdriverjs with Intern since Intern is using its own WebDriver library which isn't directly compatible with the Selenium JS library.

Related

Protractor. How to restart browser between tests?

I wrote a pack of tests and separately each of them works correctly (for each test I created separate file - it important for my case) , but when I try to run them all together I have some problems:
At the first I tried to restart browser after each test with “restartBrowserBetweenTests: true” option but as a result I receive next error:
Failed: This driver instance does not have a valid session ID (did you
call WebDriver.quit()?) and may no longer be used.
I read some advice for this situation, but nothing helped me.
Basically, I can get without restarting browser but in this case I want to close all active tabs after each test and I also don’t know ho to do it. With the help of this function:
browser.getAllWindowHandles().then((handles) => {
browser.driver.switchTo().window(handles[1]);
browser.driver.close();
browser.driver.switchTo().window(handles[0]);
});
I can close active tab and return for the previous but the function works only if I place this code in the test spec, that's not comfortable for me (copy it for each test) and if I call it from special file with functions it works incorrectly due to the
“Failed: no such window: target window already closed
from unknown error: web view not found”
errors (a consequence of a lost context).
https://www.protractortest.org/#/api?view=ProtractorBrowser.prototype.restart
use instead:
browser.restart()
also try to use await
it('Test Puppeteer screenshot', async function() {
await browser.get('https://www.google.com')
let current = await browser.getWindowHandle()
await browser.executeScript("window.open('https://www.indiatoday.com')")
handles = await browser.getAllWindowHandles()
await browser.switchTo().window(handles[1]);
await browser.close();
await browser.switchTo().window(handles[0]);
});
Note if you are using page object the nyou have to reinitialize modules:
if you are using javascript object notation for page then
https://www.npmjs.com/package/decache
const decache = require('decache');
let stage1 = require('../pageobjects/stage1.js');
beforeEach(async function () {
decache('../pageobjects/stage1.js');
stage1 = require('../pageobjects/stage1.js');
elements = stage1.elements;
});
If you are using nodejs class:
let stage1 = require('../pageobjects/stage1.js');
beforeEach(async function () {
stage1 = new PageObjects.stage1();
});
This because the reference for elements have changed you have to reinitialize them

Is it possible to create a file with all my functions and read from from it in my specs?

I have some specs that are using the same functions, I would like to make one single file only for functions and read from this file while executing my scripts, would be that possible? if so.. how to do that?
During google searchers I found the "exports" to add in config file but didn't work (also I don't know how to call it from the config)
For example, I would like to add 2 functions in my config file (or separated file only for functions) and during any point of my execution, call it from the spec file:
function loginAdminUser(userElement, passWordElement, userName, password){
var loginButton = element(by.id('logIn'));
browser.get('https://( ͡° ͜ʖ ͡°).com/');
userElement.sendKeys(userName);
passWordElement.sendKeys(password);
loginButton.click();
}
function accessHistoryViewDetail(){
menuForViews.then(function(selectview) {
selectview[3].click();
browser.sleep(500);
});
}
1 - How can I do that? (using "Suites" would be an option?)
2 - How to call them in my specs?
Thank you and have a good day!
As far as I know you cannot add utility functions that you want to use in your tests in the config file. The options in the config file are generally for setting up the testing environment.
You can however put your functions in a separate file and import that to use the functions. Below is an example of how to do that using js and Node's module exports, you can do something similar with ts using classes.
// utils.js
function loginAdminUser(userElement, passWordElement, userName, password){
var loginButton = element(by.id('logIn'));
browser.get('https://( ͡° ͜ʖ ͡°).com/'); // nice Lenny face :)
userElement.sendKeys(userName);
passWordElement.sendKeys(password);
loginButton.click();
}
function accessHistoryViewDetail() {
menuForViews.then(function(selectview) {
selectview[3].click();
browser.sleep(500);
});
}
module.exports = {
loginAdminUserloginAdminUser: loginAdminUser,
accessHistoryViewDetail: accessHistoryViewDetail
}
Then in your spec file
import * as utils from './utils.js';
...
it('should ...', () => {
...
utils.accessHistoryViewDetail();
...
});
});
I hope that helps.

Back4app issue in registering device with ionic 3

I m trying to use back4app for push notification. For this I have installed back4app's ionic sdk (https://www.back4app.com/docs/ionic/parse-ionic-sdk) like this
npm install parse
then in my app.component.ts I imported parse
import Parse from 'parse';
and in platform ready
Parse.serverURL = "https://parseapi.back4app.com/";
Parse.initialize("APP_ID_HERE", "JAVASCRIPT_KEY"); //I have used real keys from back4app dashboard.
let install = new Parse.Installation();
install.save(null, {
success: (install) => {
// Execute any logic that should take place after the object is saved.
// console.clear();
console.error('New object created with objectId: ' + install.id);
},
error: (install, error) => {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
//console.clear();
console.error('Failed to create new object, with error code:' + error.message.toString());
}
});
When I do Ionic serve or test it in device, it should register device/installation id to their backend but it gives 400 Bad Request error on https://parseapi.back4app.com/classes/_Installation the error was -
{"code":135,"error":"deviceType must be specified in this operation"}
I m not sure where to mention deviceType as their documentation doesn't seem that good.
Can anybody help me on this??
This is not mentioned in their documentation but I have found it in one of their example.
Replacing -
let install = new Parse.Installation();
with
let install = new Parse.Installation();
install.set("deviceType", platform.platforms().toString());
Solved the issue.
Here is the link to their repository
Parse SDK now supports Promises.
I'd recommend using it instead of passing in callbacks.
You can accomplish that by:
install.save().then(() => {
// success
}, err => {
// error
})

How do I use jest with coffeescript and ES6/ES2015 (e.g. via Babel)?

My team has been using both coffeescript and ES6/ES2015 (via Babel) in our project. Since the files are ultimately compatible due to Babel, it's been working great. But I can't figure out how we can write a jest test that allows both.
I've found examples of how to use jest with coffee, or ES6 features, but not both:
Example with coffeescript
Another example with coffeescript
Example with Babel
Somewhere someone suggested to set the preprocessor as babel-jest, which works fine for me if I only use it with ES6.
These all work. But again, I can't figure out how to combine them
What I've tried
I tried my own solution:
In package.json I have:
"jest": {
"scriptPreprocessor": "<rootDir>/jest-script-preprocessor",
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react",
"<rootDir>/node_modules/react-dom",
"<rootDir>/node_modules/react-addons-test-utils",
"<rootDir>/node_modules/fbjs"
],
"testFileExtensions": ["coffee", "cjsx", "js", "jsx"],
"moduleFileExtensions": ["coffee", "cjsx", "js", "jsx"]
}
In jest-script-preprocessor.js, I have:
var coffee = require('coffee-react');
var transform = require('coffee-react-transform');
var babelJest = require("babel-jest");
module.exports = {
process: function(src, path) {
if (coffee.helpers.isCoffee(path)) {
console.log(path);
return coffee.compile(transform(src), {
'bare': true
});
} else {
console.log(path);
return babelJest.process(src, {
filename: path,
stage: 0
});
}
}
};
If I run a test like npm test __tests__/sometest.jsx, it loads the ES6 test file fine. That test file will import the module under test, which is also ES6, and THAT'S where it blows up. It simply says Unexpected reserved word as soon as it hits an ES6-only syntax, like import, export, etc. There is no additional line information, but I know it's ES6 that causes the problem because if I change the module under test to be ONLY export default 'mystring', it blows up, and if I change it to non-ES6 syntax like var q = 5 + 5; module.exports = q;, it imports the module fine. (Of course, that's not really a testable module, but it doesn't need to be for this proof-of-concept.)
Note the console.log() lines in there. I never see them. So one reason this has been so tricky to track down is I can't put any of my own debug logic in. I'm sure these lines run, because if I throw in some random syntax on those lines, it'll choke. But no console output.
I've tried jest-webpack-alias, which is officially recommended by jest, and it sounds great in theory: you use jest with webpack, which then allows you to use whatever preprocessors you've already set up. It gives me the same error of Unexpected reserved word. I wrote up an issue on their github repo.
Notes
I found jestpack, but I don't want to use it as it requires Node >= 5, and I want to use Node 4.2.3. It also doesn't work with Jest >= 0.8, and I want to use Jest 0.8, as it's currently the latest and I assume has the best chance of being in sync with the live docs and react version (0.14.6).
Here's what I'm doing that's working for me.
npm install --save-dev babel-jest
npm install --save-dev coffee-react
In package.json:
"jest": {
"scriptPreprocessor": "<rootDir>/jest-script-preprocessor",
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/."
],
"testFileExtensions": ["coffee", "cjsx", "js", "jsx"],
"moduleFileExtensions": ["coffee", "cjsx", "js", "jsx"]
}
Take note of the unmockedModulePathPatterns. I set it to match everything in node_modules. You might not want that, but if you start getting errors like Error: Failed to get mock metadata:..., consider it, as recommended here.
In jest-script-preprocessor.js:
var babelJest = require('babel-jest');
var coffee = require('coffee-react');
module.exports = {
process: function(src, filename) {
if (filename.indexOf('node_modules') === -1) {
if (filename.match(/\.coffee|\.cjsx/)) {
src = coffee.compile(src, {bare: true});
} else {
src = babelJest.process(src, filename);
}
}
return src;
}
};

Is there a way to create and run a dynamic script from karma.conf.js

I'm using karma to run tests on an angularjs application.
There are a couple JavaScript functions that I would like to run at start-up, but they need to be dynamically created based on some system data. When running the app, this is handled with node.
Is there any way to create a script as a var and pass it to the files: [] rather than just using a pattern to load an existing file?
I can make this work by creating the file, saving it to disk then loading it normally, but that's messy.
You can create your own karma preprocessor script.
For a starting point use the following as example:
var fs = require('fs'),
path = require('path');
var createCustomPreprocessor = function (config, helper, logger) {
var log = logger.create('custom'),
// get here the configuration set in the karma.conf.js file
customConfig = config.customConfig || {};
// read more config here in case needed
...
// a preprocessor has to return a function that when completed will call
// the done callback with the preprocessed content
return function (content, file, done) {
log.debug('custom: processing "%s"\n', file.originalPath);
// your crazy code here
fs.writeFile(path.join(outputDirectory, name), ... , function (err) {
if (err) {
log.error(err);
}
done(content);
});
}
};
createCustomPreprocessor.$inject = ['config', 'helper', 'logger'];
module.exports = {
'preprocessor:custom': ['factory', createCustomPreprocessor]
};
Add a package.json with the dependencies and serve it as a module. ;)
For more examples have a look to more modules here: https://www.npmjs.org/search?q=karma%20preprocessor