I use Cucumber 3.x and I want get scenario name in Before hook. My code seems like this:
Before(function (scenario) {
let name = scenario.name;
console.log(scenario) //debug printing
return require("./driver").createDriver(desired);
});
And console.log in Cucumber 3.x shows only this:
{ sourceLocation:
{ uri: 'name_my_feature.feature',
line: 206 } } //206 - is line current scenario name
How I can get scenario name?
I found it under scenario.pickle.name
Related
I'm trying to test a ternary branch which is using window.location.
#Injectable()
export class ABCService {
private hostUrl = (window.location.host.indexOf('localhost') > -1) ? 'example.com' : window.location.host;
...
TestCase:
it('check alternate URL', () => {
const URL = 'http://example.org/postMethod';
service.getData().subscribe(data => {
expect(data.length).toBe(1);
});
const req = httpMock.expectOne(URL);
expect(req.request.url).toBe(URL);
req.flush({data:[1]});
});
Getting Error:
Error: Expected one matching request for criteria "Match URL: http://example.org/postMethod", found none.
Any ideas of how can I test if window.location.host is not localhost.
Tried (window as any).location = { ...window.location, host: 'http://example.org' };
still getting host == localhost:9876;
OR
Is there any way I can override window.location.host value of mock it?
Instead of referencing a global variable, which might not be defined depending on what environment you are running your app in (e.g. Angular Universal), use dependency injection.
In this case you can get the information you want from Angular's DOCUMENT constant (https://angular.io/api/common/DOCUMENT).
constructor(#Inject(DOCUMENT) private doc) {
console.log(this.doc.location.href);
}
And in your tests you could mock it with:
const service = new ABCService({location: {href: 'my-test-url'}} as any);
I need to have the whole code to understand yor question and test case more accurately, can you please supply a Stackblitz url for it?
Anyway, you can use jasmine's spyOn function to mock window.location. Thisway you can supply mock it's functions and their return values, without actually setting the host.
Look at Jasmine docs for more information.
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";
I am new to javascript and Node.js and having problems testing some code I wrote recently. I am trying to test code written in a file called "compareCrowe.js" with "testCrowe.js" using Node.js.
Here are the contents of testCrowe.js:
var compareCrowe = required['./compareCrowe'];
console.log('begin test');
var connection = {Type:1, Label:"label", linkTo:null};
var table1 = {name:"table1", body:"description1", out:[connection]};
var table2 = {name:"table2", body:"description2", out:null};
connection.linkTo = table2;
var crowe = [table1, table2];
var result = compareCrowe.compareCrowesFoot(crowe, crowe);
console.log(result.feedback);
where the function "compareCrowesFoot" is defined in compareCrowe.js. From the console on an Ubuntu virtual machine I ran:
node compareCrowe.js testCrowe.js
however, nothing was printed. There were no errors or warnings or explanation of any kind. It didn't even print the "begin test" line I placed at the top of testCrowe.js. If I run the command:
node testCrowe.js
it complains that compareCrowesFoot is undefined. How can I test the contents of compareCrowe.js?
Welcome to the party of JS.
I'm not sure where you're learning from, but a few of the resources that have helped me and many others are superherojs.com, nodeschool.io, the MDN developer docs, Node.js API docs, and Youtube (seriously).
The basic idea of Node.js is that it operates with modules (chunks of reusable code), which is what NPM is made up of. These can then be included in other modules and used anywhere else in your application.
So for a basic example, say you had compareCrowe.js, to make it includable/reusable in another file, you could write something like:
module.exports = function() {
var compareCrowesFoot = function(crowe1, crowe2) { /* compare crows feet and return something here */ }
return { compareCrowesFoot: compareCrowesFoot };
// return an object with a property of whatever you want to access it as , and the value as your function name
// e.g. - you could return { compare: compareCrowesFoot };
}
Then in testCrowe.js you could require compareCrowe like this:
var compareCrowe = require("./compareCrowe");
/* your code here... */
var result = compareCrowe.compareCrowesFoot(crowe1, crowe2);
// if your returned object was { compare: compareCrowesFoot };
// this would be compareCrowe.compare(crowe1, crowe1);
And to run your tests, you could then run node testCrowe.js from the command line.
In your case it seems like you've got your syntax a little messed up. It should be more like:
var compareCrowe = require('./compareCrowe.js');
That would make any methods you've exported in compareCrowe.js, such as your compareCrowe.compareCrowesFoot function, available to testCrowe.js.
Then, in your terminal, you would run the following:
node testCrowe.js
And that should do the trick provided you don't have any further errors in your code.
I'm trying to run Jasmine client integration tests on a meteor project. I'm using meteor 0.9.4, and the sanjo:jasmine package for Jasmine.
I have written a test which looks like:
describe("Template.dashboard.tasks", function() {
it("ela displays correct assessment", function() {
Session.set("selected_subject", "math");
Session.set('selected_grade', "1");
tasks = Template.dashboard.tasks();
expect(true).toBe(true);
});
});
I get an error before it can get to the end of the test:
Cannot read property 'tasks' of undefined
This means that Template.dashboard does not exist within the scope of this test.
Template.dashboard.tasks() is a helper function which works completely, and it is in a js file within a view folder. Regular Jasmine tests work as expected, but as soon as I try to use one of my own functions from another file, it doesn't work.
My question is: Is there something I need to do to give the Jasmine test access to my template helper functions?
In Meteor, Template helper functions used to be formatted like this:
Template.dashboard.tasks = function () {
...
};
But that has been deprecated, and the new format is:
Template.dashboard.helpers({
tasks: function(){
...
}
});
In Jasmine, with the previous formatting, you could access helper functions like:
Template.dashboard.tasks();
But now you must call helper functions like this:
Template.dashboard.__helpers[' tasks']();
Sanjo (the original author of the meteor-jasmine repo) suggested using a function like this to make it easier to call helper functions (especially if the syntax ends up getting changed again):
function callHelper(template, helperName, context = {}, args = []) {
template.__helpers[` ${helperName}`].apply(context, args);
}
An updated answer to this question for Meteor 1.3 (sorry I use mocha, but it does not affect the answer):
Template.foo and Template.foo helpers won't be eagerly set up when testing, so you need to import foo.html then foo.js.
Here is an example :
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Foo } from '/collections/foo.js';
import { assert } from 'meteor/practicalmeteor:chai';
import './foo.html'; // now Template.foo is defined
import './foo.js'; // now Template.foo.__helpers[' bar'] is defined
describe('foo handlers', () => {
it("Should test bar", () => {
// don't forget the space, helper key is ' bar' not 'bar'
var bar = Template.foo.__helpers[' bar'].apply({foo:"bar"},3);
assert.Equal(bar,'bar');
});
});
Of course as said before, you should definitely encapsulate the weird Template.foo.__helpers[' bar'].apply(context,args) into a nice, clean helper.
tests on server part are running nicely from start, and there is indeed one more thing to do in order to run tests on the frontend part. I'll try to find you that.
In addition, consider reading or reading again the famous and explicit article from Dr. Llama's Blog related to Jasmin/Meteor : Bullet-proof Meteor applications with Velocity, Unit Testing, Integration Testing and Jasmine
I'm trying to test my webapp with Buster.js but I can't get the HTTP proxy working.
When I try to run the test I only get a undefined message
$ buster-test --verbose
Running tests: Browser tests
undefined
undefined
I tried to find a solution to this problem online but It's kinda hard to search for an "undefined" message. It looks a bit like this problem node.js displays "undefined" on the console...
Anyone got any clue what might be the problem?
My config looks like this:
var config = exports;
config["Browser tests"] = {
environment: "browser",
sources: ["jquery.js"],
tests: ["proxy-test.js"],
resources: {
"/en": "http://docs.busterjs.org:80/en/"
}
};
And my test looks like this:
var strftime = window.strftime;
var assert = buster.assert;
buster.testCase("Ajax Testcase", {
setUp: function () {
this.timeout = 5000;
},
"Should get http://docs.busterjs.org/en/latest/": function (done) {
$.ajaxSetup({timeout:1000});
$.ajax("en/latest/").done(function(evt) {
done();
}).fail(function(evt) {
assert(false); // test still times out..
});
}
});
I forked the tests and added my own here: https://github.com/tiemevanveen/demos/tree/master/buster-proxy-tests
I also created an issue on the buster github: https://github.com/busterjs/buster/issues/382