如何解决“失败:在量角器测试中将圆形结构转换为JSON”

时间:2019-01-23 18:38:42

标签: arrays json protractor

我正在尝试从我们的Web应用程序中提取一些值,并将这些值存储在JSON文件中。下面是我的代码。

运行脚本时,出现错误“失败:将圆形结构转换为JSON”。但是我没有在此脚本上找到任何循环引用。变量是独立的,或者在我看来。

require('..\\waitAbsent.js');
require("../node_modules/jasmine-expect/index.js");
var EC = protractor.ExpectedConditions;
var G = globalVariables;
var util = require('util')

var fs = require('fs');

let StudentData = 'StudentData.json';

describe('Test for Json Data', function (){

    beforeAll(function () {
        browser.driver.manage().window().maximize();
        browser.get(G.loginMain);
    });

    it('test for Json file creation', function(){

        var SubmitButton = $('#submitButton').getAttribute('id').then(function(SB){
        return(SB)

        });

        var UserName = $('#username').getAttribute('id').then(function(UN){
        return UN;
        });

        let Data = {    
            a: UserName,
            b: SubmitButton,    
        };        
        let DT = JSON.stringify(Data);    
        fs.writeFileSync(StudentData ,DT);    
        });    

    it('test for Submit', function(){

        let Uploaded_data = fs.readFileSync(StudentData);

        let Data = JSON.parse(Uploaded_data);

        let c = Data['Submit'];

        console.log(c);

        expect(c).toBe('submitButton');

    })

});

我该如何克服?该脚本中确实有任何循环引用吗?

1 个答案:

答案 0 :(得分:0)

update-json-file的帮助下,我可以解决此问题。现在,我将每个值分别写入Json文件中,并且以某种方式不显示该循环引用错误。

我更新的代码如下:

    require('..\\waitAbsent.js');
require("../node_modules/jasmine-expect/index.js");
var EC = protractor.ExpectedConditions;
var G = globalVariables;
var util = require('util')
const updateJsonFile = require('update-json-file')

var fs = require('fs');

let StudentData = 'StudentData.json';

describe('Test for Json Data', function (){

    beforeAll(function () {
        browser.driver.manage().window().maximize();
        browser.get(G.loginMain);


    });

    it('test for Json file creation', function(){

        var SubmitButton = $('#submitButton').getAttribute('id').then(function(SB){

        let Data = { SubmitButton : SB };

            let DT = JSON.stringify(Data);

            fs.writeFileSync(StudentData ,DT);

            return(SB)


        });

        var UserName = $('#username').getAttribute('id').then(function(UN){
            updateJsonFile(StudentData, (data) => {

                data.UserName = UN
                return data
            })
;
        return UN;
        });

    });


    it('test for b', function(){


        let Uploaded_data = fs.readFileSync(StudentData);

        let Data = JSON.parse(Uploaded_data);

        let c = Data['SubmitButton'];

        console.log(c);

        expect(c).toBe('submitButton');

    })

});