I have a js function which I want to unit test using QUnit. It is a simple function where I pass the field name that I want to hide from the form. I am also using a Mocking framework from here: [link] https://github.com/ambek/XrmPage-Mock
function HideField(field) {
Xrm.Page.getControl(field).setVisible(false);
};
My Tests.js file looks like this:
var Xrm;
QUnit.test("Testing Hide Field Function", function (assert) {
var PageControls = new Array();
PageControls.push(CreateSimpleControlWithAttribute("NewField", "M", "Type of Residence"));
var page = new XrmPageMock("{607C16D1-7C53-4023-B20B-13E4F1C6A9D3}", PageControls, 1);
Xrm = new XrmPageMock(page);
HideField('NewField');
assert.equal(XRM.page.getControl('NewField').getVisible(),false, 'Passsed');
});
On executing my tests, I get this error:
Unable to get property 'getControl' of undefined or null reference
Does anyone know why my mocking/faking is going wrong?
It was just a typo in the following statement:
assert.equal(XRM.page.getControl('NewField').getVisible(),false, 'Passsed');
The new statement is (Correction on the word "page")
assert.equal(XRM.Page.getControl('NewField').getVisible(),false, 'Passsed');
Related
I'm working on creating a Mocha unit test based on solution code that was given to me by someone else. (The goal here is to create an online code assessment for students that will be run against my unit test). It's a simple exercise and will not be extensible at all in the future.
I want to get the return value from a jQuery on-submit event and use that for my test case but am unsure how I can do that given the solution code that was given to me to work from.
I've gone through the document here (https://gist.github.com/soheilhy/867f76feea7cab4f8a84) but my particular case is different since we are using jQuery's on document ready and the on-submit.
I've also tried to do something like "export.validate = function(){}" to match an example from the docs but everything I've tried I either get that Mocha doesn't know the function, or Mocha doesn't know the boolean variable references.
solutionCode.js
$(document).ready(function() {
$("#form-submit").on("submit", function () {
var xValid = true;
var yValid = true;
//...Bunch of logic here that could change the boolean values...
return xValid && yValid;
});
});
And here is my Mocha test file.js
this.jsdom = require('jsdom-global')()
global.$ = global.jQuery = require('jquery');
var assert = require('assert');
var work = require('path/to/solutionCode.js');
describe('Validate Form', function() {
it('Form is valid', function(done) {
//Not sure how to get the return value here to do my assertion...
done();
});
});
If the value of both booleans in the return are True, the test should pass, otherwise it should fail.
Code :
var assert = require('assert');
describe('webdriver.io api page', function() {
it('should open login page', function () {
browser.url('/login');
browser.pause(10000);
})
it('Should enter login details and submit', function() {
browser.pause(10000);
browser.element(By.xpath("//input[#type='text' and #aria-label='user name']")).sendKeys("mikestr");
})
});
I am new to webdriverIO and trying to locate username textbox using xpath but it throws me error. I look around many webdriverIO material but did not get solution for this yet.
WebdriverIO's Selectors are different than Selenium Webdriver. It will accept the String value for either "XPath" or "CSS" selectors. The "By is not defined" Error you're seeing is because it's looking for a "String" value of a selector.
Documentation:
http://webdriver.io/guide/usage/selectors.html#CSS-Query-Selector
In your situation you would only need to pass something like:
browser.element('"//input[#type='text' and #aria-label='user name']"')
.setValue('mikestr')
Alternatively you could set the variable and pass a variable like:
var XPath = "//input[#type='text' and #aria-label='user name']"
browser.element(XPath).setValue('mikestr')
The locator has to be a string
For setting value , you can use setValue
browser.setValue("//input[#type='text' and #aria-label='user name']", 'mikestr');
I had a different issue using angular with the same By is not defined error. For anyone else that might end up here the solution for me was to add the import
import { By } from "#angular/platform-browser";
Im writing some Protractor testcases by using a pageobject. However im having trouble using elements directly from the Pageobject file, in the spec file.
Possibly im missing some JS nuance, since i havent written that much in JS.
i would like to use some elements defined in Pageobject like so:
var PageObject = function()
{
var loginUsername = element(by.id('loginusername'));
//other methods
};
module.exports = PageObject;
to be used in my spec file like so:
var PageObject = require('./pageObject.page.js');
describe( ' Login page ', function(){
it('type something in the usernamefield', function(){
var pageObject = new PageObject();
pageObject.get();
pageObject.loginUsername.sendKeys('Test');
});
});
Using methods (for example get but also others)works fine, but using elements directly causes some undefined errors.
Im trying to copy from something like this, where supposedly it should work.
https://ramonvictor.github.io/protractor/slides/#/33
Something you are missing,
bind your variable to object in pageObject.page.js, using this keyword
var PageObject = function()
{
this.loginUsername = element(by.id('loginusername'));
......
};
module.exports = PageObject;
In Spec first get the real application and do real work
it('type something in the usernamefield', function(){
browser.get('"https://....');
var pageObject = new PageObject();
pageObject.loginUsername.sendKeys('Test');
});
This works for me perfect,
I am trying to reference test data using a JSON but it appears as undefined. I am trying to use a Page Object Model and reference external test data. When referencing the values in my test data and passing into a function they are appearing as undefined
I have a simple JSON like:
[{
"userNameValue": "mrUserUser",
"passwordValue": "i_am_correct"
}]
My tests then look like:
'use strict';
var testData = require('./testData/testData.json');
var AngularPage = require('./pages/logon.page.js');
describe('travel portal logon page', function () {
var logonPage;
beforeEach(function () {
logonPage = new AngularPage();
});
it('should fail to logon', function() {
logonPage.loginToSite(testData.userNameValue, testData.passwordValue);
expect(logonPage.errorMessageText).toEqual('Invalid Login');
});
});
Both testData.userNameValue and testData.passwordValue are being written as undefined. I have also tried referencing using the other format of testData['userNameValue'].
Am I missing something?
Please help, thanks
Make sure you have exported your testData.json file as required using module.exports function. Also your test data is not an object in the first place, its an array. In order for your test script to work, change the test data into an object. Here's how -
var testData = {
"userNameValue": "mrUserUser",
"passwordValue": "i_am_correct"
};
If you want to keep the testData the way it is now(an array), then change the way you access the data in your test steps. Here's how -
logonPage.loginToSite(testData[0].userNameValue, testData[0].passwordValue);
Hope it helps.
I wan to test my library with nodeunit, and I use File Object in it, on website everything is working (FileAPI is implemented there) but when I'm trying to test it with nodeunit i get an error:
Fatal error: File is not defined
I assume that I have to add:
var FileAPI = require('file-api');
var File = FileAPI.File;
at the begging of the code, but I don't need that when I include that library to website, how to deal with that?
To use nodeunit I had to add module.exports at the end, is it necessary as well? (code sample on github)
What's more, when I'm trying to test this code:
https://github.com/GeoSmartCity-CIP/gsc-client/blob/feature/upload-data-file/src/upload/upload.js
with this test:
var gsc = require('../../src/upload/upload');
var FileAPI = require('file-api');
var File = FileAPI.File;
var exports = exports || {};
exports.isFileTypeCorrect = function(test) {
var file = new File('test.geojson');
var asd = new gsc.upload.Data(file);
test.ok(asd.isFileTypeCorrect(), 'this assertion should pass');
test.done();
};
I'm getting Fatal error: Cannot read property 'substr' of undefined error, what's the problem?
EDIT:
problem with substr is propably from isShapefileCorrect function, but still dont know why?
var asd = new gsc.upload.Data(file); Isn't it asynchronous function? Then probably asd.isFileTypeCorrect() should be called inside upload callback. Also you defining isFileTypeCorrect function as module exports and call itself inside itself. Isnt it an infinite loop?