如何将变量从浏览器传递到量角器

时间:2022-10-04 01:21:30

I'm testing a single page application which makes a GET request to a given api endpoint, expecting some result.

我正在测试一个页面应用程序,该应用程序向给定的api端点发出GET请求,希望得到一些结果。

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).

现在,我使用$ httpbackup对象对API进行了模拟,我需要断言正确的URL会传递给它(在执行GET请求时)。

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.

我的URL包含API需要知道的许多额外信息(启动时间、结束时间等)。我只是想测试是否传入了正确的内容。

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?)

问题是,如何传递$httpBackend.context。函数的变量?(以便查看ti是否与预期的URL匹配?)

1 个解决方案

#1


2  

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;
        });
    });

#1


2  

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;
        });
    });