Reputation: 11678
I'm testing a single page application which makes a GET request to a given api endpoint, expecting some result.
Now, I've mocked the API using the $httpbackend object and I need to assert that the correct URL is passed to it (when performing the GET requet).
My URL contains a number of extra information the API needs to know (startTime, endTime and more). I just want to test that the right stuff is passed in.
This is my current end to end test:
var chai = require('chai');
var chaiPromise = require("chai-as-promised");
var HttpBackend = require('http-backend-proxy');
var utils = require('../utils.js');
var expect = chai.expect;
var dateTimeSupport = require('../dateTimeFillSupport.js');
var support = require('../uhhSupport.js');
chai.use(chaiPromise);
var steps = function(){
var proxy = null;
var urlFound = "";
this.Before(function(event, callback){
proxy = new HttpBackend(browser);
callback();
});
this.After(function(event, callback){
proxy.onLoad.reset();
callback();
});
this.Given(/^my given$/, function(){
// Set up the context for the proxy - to be able to pass stuff back and forth
var simpleChartData = require('data.json');
proxy.context = {
chartData : simpleChartData,
foundUrl : urlFound
};
// Allow components and directived to pass through
proxy.onLoad.whenGET(/\.\/components\/.+/).passThrough();
proxy.onLoad.whenGET(/directives\/.+/).passThrough();
proxy.onLoad.whenGET(/.+\/api\/pvValues\/.+/).respond(function(method, url){
$httpBackend.context.foundUrl = url;
return [200, $httpBackend.context.chartData];
});
// perform action
browser.get(utils.baseUrl);
$('.dateLabel').click();
return browser.controlFlow().execute(function(){});
});
this.When(/^a card is clicked$/, function(){
return dateTimeSupport.clickTheFirstCard().then(function(){
$('.etChart').isDisplayed();
});
});
this.Then(/^the correct URL is passed to the mocked API$/, function(){
var expectedUrl = "myexpectedurl";
// here I want to check expectedUrl against $httpBackend.context.foundUrl
return browser.controlFlow().execute(function(){});
});
}
module.exports = steps;
So the question is, how can I pass the $httpBackend.context.foundUrl
variable to my then
function? (in order to see if ti matches the expected URL?)
Upvotes: 2
Views: 762
Reputation: 609
You could write a global variable to the browser window in the respond
function inside the Given
:
proxy.onLoad.whenGET(/.+\/api\/pvValues\/.+/).respond(function(method, url){
window.foundUrl = url;
return [200, $httpBackend.context.chartData];
});
In the Then
, you get the browser to execute a script to return the global to protractor:
this.Then(/^the correct URL is passed to the mocked API$/, function(){
var expectedUrl = "myexpectedurl";
// here I want to check expectedUrl against $httpBackend.context.foundUrl
return browser.executeScript('return window.foundUrl').then(function(theUrl){
expect(theUrl === expectedUrl).to.be.true;
});
});
Upvotes: 2