Ekaterina Salazkina
Ekaterina Salazkina

Reputation: 93

How to get One value from an array (protractor)?

Is it possible to get One value from an array such as:

Array
(
    [id] = 100917
    [sid] = SM0ddb860df74148f19f57b314bfc80b39
    [date_created] = 2016-02-10 12:04:08
    [date_updated] = 2016-02-10 12:04:08
    [date_sent] = 
    [to] = +996559138088
    [from] = +15674434357
    [body] = Confirmation code: 75ba76
    [status] = queued
    [direction] = outbound-api
    [price] = 
    [price_unit] = USD
)

I need somehow to get a value of "75ba76" (in confirmation code string) using:

[to] = +996559138088

When i try to select it, i see:

http://prntscr.com/a1cngn

Any ideas?.. Thanks.

Upvotes: 1

Views: 1308

Answers (2)

Ekaterina Salazkina
Ekaterina Salazkina

Reputation: 93

With a strong brainstorm i solved the problem using the next code. It's maybe seems to be huge, but it works great!

    it("get temp password", function () {        
    browser.get("http://api.test.yalla.ng/sms.php?db=stena_kg");

    var preElement = element.all(by.css('pre'));
    var matchPhone =  browser.params.phones.matchPhone;
    var matchPass;

    var elementIndex = 0;
    parseNextElement();

    function parseNextElement() {
        preElement.get(elementIndex).getText().then(function (text) {
            parseElementData(text);
            if(!matchPass){
                elementIndex++;
                parseNextElement();
            }else{
                onMatch();
            }
        });
    }

    function parseElementData(data){
        var dataArr = data.split("\n");
        var phoneTxt = "";
        var passTxt = "";

        for(var i = 0; i<dataArr.length; i++){
            var str = dataArr[i];
            var splitStr = str.split(" ");
            if(str.match(/\[to\]/)){
                phoneTxt = splitStr[splitStr.length - 1];
            }
            if(str.match(/\[body\]/)){
                passTxt = splitStr[splitStr.length - 1];
            }
        }

        console.log(phoneTxt, passTxt);

        if(phoneTxt == matchPhone){
            matchPass = passTxt;
        }
    }

    function onMatch(){
        console.log("COMPLETE!!!", matchPass);
    }

}, 600000);

}); 

Upvotes: 0

giri-sh
giri-sh

Reputation: 6962

You can use string and array operations to get the value that you need, as text in html elements are stored as strings by default. filter() function can be used to get the <pre> element that has the number you specify in arrString variable below. Here's how -

var arrString = "[to] = +996559138088";
var reqValue;
element.all(by.css('pre')).filter(function(ele){
    return ele.getText().then(function(str){
        return str.search(arrString) > -1;
    });
}).getText().then(function(val){ //returns array as a string
    reqValue = val.split('\n').filter(function(eachEle){return eachEle.search(/body/i)>-1}).toString().split(' ').pop();
    //reqValue holds the data that you require
});

Though code looks a little weird, it should do the job. Hope it helps.

Upvotes: 1

Related Questions