Więc mam problem z pobraniem wartości JSON, a następnie zaktualizowaniem ich do wartości JSON. Zasadniczo przykład

    {
        browserName: "Safari",
        os: "OS X",
        resolution: "1920x1080",
        os_version: "Catalina",
        project: "Selenium-Test",
        build: "Build T-Hour",
        name: "Safari - OS X",
        "browserstack.debug": "true"
    },

I chciałbym mieć

    {
        browserName: "Safari",
        os: "OS X",
        resolution: "1920x1080",
        os_version: "Catalina",
        project: "Selenium-Test",
        build: "Build T-Hour",
        name: "browserName + os_version - os", <--- Change to this 
        "browserstack.debug": "true"
    },

Zasadniczo pobieramy wartości z json i stosujemy je do „nazwy”.

Tak wygląda cały mój kod:

 var myConfig = {

        "browserstackUser": "xxxxxxxxxx",
        "browserstackKey": "xxxxxxxxxx",

        multiCapabilities: [
            //Apple

            // Safari
            {
                browserName: "Safari",
                os: "OS X",
                resolution: "1920x1080",
                os_version: "Catalina",
                project: "Selenium-Test",
                build: "Build T-Hour",
                name: "Safari - OS X",
                "browserstack.debug": "true"
            },

            //Firefox

            {
                browserName: "Firefox",
                os: "OS X",
                resolution: "1920x1080",
                os_version: "Catalina",
                project: "Selenium-Test",
                build: "Build T-Hour",
                name: "Firefox - OS X",
                "browserstack.debug": "true"
            },

            //Chrome

            {
                browserName: "Chrome",
                os: "OS X",
                resolution: "1920x1080",
                os_version: "Catalina",
                project: "Selenium-Test",
                build: "Build T-Hour",
                name: "Chrome - OS X",
                "browserstack.debug": "true"
            },

            //Edge

            {
                browserName: "Edge",
                os: "OS X",
                resolution: "1920x1080",
                os_version: "Catalina",
                project: "Selenium-Test",
                build: "Build T-Hour",
                name: "Edge - OS X",
                "browserstack.debug": "true"
            },


            //Windows 10

            //Chrome

            {
                browserName: "Chrome",
                os: "Windows",
                resolution: "1920x1080",
                os_version: "10",
                project: "Selenium-Test",
                build: "Build T-Hour",
                name: "Chrome - Windows 10",
                "browserstack.debug": "true"
            },

            //Firefox

            {
                browserName: "Firefox",
                os: "Windows",
                resolution: "1920x1080",
                os_version: "10",
                project: "Selenium-Test",
                build: "Build T-Hour",
                name: "Firefox - Windows 10",
                "browserstack.debug": "true"
            },

            //Edge

            {
                browserName: "Edge",
                os: "Windows",
                resolution: "1920x1080",
                os_version: "10",
                project: "Selenium-Test",
                build: "Build T-Hour",
                name: "Edge - Windows 10",
                "browserstack.debug": "true"
            }
        ],

        maxSessions: 1, 

        specs: [
            "pagesDesktop/testScript.js",
        ],

        jasmineNodeOpts: {
            showColors: true,
            defaultTimeoutInterval: 60000,
        },

        SELENIUM_PROMISE_MANAGER: false,

        framework: "jasmine2",

        plugins: [{
            "package": "protractor-browserstack-reporter"
        }],

        reporters: ["browserstack"],

        params: {
            cardType: {
            }
        }
    }

myConfig.multiCapabilities = myConfig.multiCapabilities.map(m => ({...m, name: ${m.browserName} + ${m.os_version} - ${m.os}}));

exports.config = myConfig;

Jak mogę dodać dla każdego obiektu json browserName, os_version i os wewnątrz name dla każdego obiektu?

0
ProtractorNewbie 3 kwiecień 2020, 14:55

3 odpowiedzi

Najlepsza odpowiedź

Możesz użyć tablicy map() na przykład:

const multiCapabilities = [{browserName:"Safari",os:"OS X",resolution:"1920x1080",os_version:"Catalina",project:"Selenium-Test",build:"Build T-Hour",name:"Safari - OS X","browserstack.debug":"true"},{browserName:"Firefox",os:"OS X",resolution:"1920x1080",os_version:"Catalina",project:"Selenium-Test",build:"Build T-Hour",name:"Firefox - OS X","browserstack.debug":"true"},{browserName:"Chrome",os:"OS X",resolution:"1920x1080",os_version:"Catalina",project:"Selenium-Test",build:"Build T-Hour",name:"Chrome - OS X","browserstack.debug":"true"}]; 

const result = multiCapabilities
  .map(m => ({...m, name: `${m.browserName} + ${m.os_version} - ${m.os}`}));
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
2
palaѕн 3 kwiecień 2020, 12:03

Możesz użyć Array.map(), aby odwzorować wartości i zmodyfikować pole nazwy.

const result = multiCapabilities.map(x => {
    x.name = `${x.browserName} - ${x.os_version} - ${x.os}`;
    return x;
}) 

Uruchom poniższy fragment, aby sprawdzić wynik

const multiCapabilities = [
        //Apple

        // Safari
        {
            browserName: "Safari",
            os: "OS X",
            resolution: "1920x1080",
            os_version: "Catalina",
            project: "Selenium-Test",
            build: "Build T-Hour",
            name: "Safari - OS X",
            "browserstack.debug": "true"
        },

        //Firefox

        {
            browserName: "Firefox",
            os: "OS X",
            resolution: "1920x1080",
            os_version: "Catalina",
            project: "Selenium-Test",
            build: "Build T-Hour",
            name: "Firefox - OS X",
            "browserstack.debug": "true"
        },

        //Chrome

        {
            browserName: "Chrome",
            os: "OS X",
            resolution: "1920x1080",
            os_version: "Catalina",
            project: "Selenium-Test",
            build: "Build T-Hour",
            name: "Chrome - OS X",
            "browserstack.debug": "true"
        }
    ]
    
const result = multiCapabilities.map(x => {
x.name = `${x.browserName} - ${x.os_version} - ${x.os}`;
return x;
})    

console.log(result);
0
Vandesh 3 kwiecień 2020, 12:01
multiCapabilities.forEach(item => {
     item.name = item.browserName + '-' + item.os_version + '-' + item.os;
});
0
uiTeam324 3 kwiecień 2020, 12:32