如何将变量从浏览器传递到 Protractor
How to pass variable from browser to Protractor
我正在测试一个单页应用程序,它向给定的 api 端点发出 GET 请求,期待一些结果。
现在,我已经使用 $httpbackend 对象模拟了 API,我需要断言传递给它的 URL 是正确的(在执行 GET 请求时)。
我的 URL 包含一些 API 需要知道的额外信息(开始时间、结束时间等)。我只是想测试是否传入了正确的内容。
这是我当前的端到端测试:
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;
所以问题是,如何将 $httpBackend.context.foundUrl
变量传递给我的 then
函数? (为了查看ti是否符合预期URL?)
您可以在 Given
:
中的 respond
函数中向浏览器写入一个全局变量 window
proxy.onLoad.whenGET(/.+\/api\/pvValues\/.+/).respond(function(method, url){
window.foundUrl = url;
return [200, $httpBackend.context.chartData];
});
在Then
中,你让浏览器执行脚本到return全局到量角器:
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;
});
});
我正在测试一个单页应用程序,它向给定的 api 端点发出 GET 请求,期待一些结果。
现在,我已经使用 $httpbackend 对象模拟了 API,我需要断言传递给它的 URL 是正确的(在执行 GET 请求时)。
我的 URL 包含一些 API 需要知道的额外信息(开始时间、结束时间等)。我只是想测试是否传入了正确的内容。
这是我当前的端到端测试:
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;
所以问题是,如何将 $httpBackend.context.foundUrl
变量传递给我的 then
函数? (为了查看ti是否符合预期URL?)
您可以在 Given
:
respond
函数中向浏览器写入一个全局变量 window
proxy.onLoad.whenGET(/.+\/api\/pvValues\/.+/).respond(function(method, url){
window.foundUrl = url;
return [200, $httpBackend.context.chartData];
});
在Then
中,你让浏览器执行脚本到return全局到量角器:
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;
});
});