Search code examples
bashawksedgrepxmllint

Get test number If the line above contains specific text


I have a junit file. How to get number from testIT?

If the line above contains the text "/system-err" or "/failure" then the test fails and writes to the file fail id test.

If the line above contains the text "/system-out" then the test is successful and writes to the passed file id test.

example "/system-err"

<system-err>Assertion Failure at Navigation.swift:88:Failed to get matching snapshot: No matches found for first query match sequence: `Descendants matching type NavigationBar`, given input App element pid: 21141 (no attribute values faulted in)</system-err>
    <system-out>testIT: C477997</system-out>

example "/failure"

>      <failure message='Assertion Failure at Element.swift:277:Failed to get matching snapshot: No matches found for Elements matching
> predicate &apos;&quot;One&quot; IN identifiers&apos; from input {(
>         Button, identifier: &apos;dictation&apos;, label: &apos;Диктовать&apos;, Disabled,
>         Button, label: &apos;Почему мне не приходит СМС?&apos;,
>         Button, label: &apos;Назад&apos;,
>         Button,
>         Button, label: &apos;Продолжить&apos;
>     )}'>
>         </failure>
>         <system-out>testIT: C478067</system-out>

passed test

<system-out>      Setting up automation session</system-out>
    <system-out>testIT: C478057</system-out>

fail file contains id

477997
478067

passed file contains id

478057

I tried

xmllint  $1 | awk -F'testIT: C|, | \\(' '/testIT:/{print $2"\n"$4"\n"$6}'| awk 'NF!=0' | sort| uniq|  2>/dev/null > successfullCases.xml

but I get

470836</system-out>
477975</system-out>
477997</system-out>
478057</system-out>
478067</system-out>
478098</system-out>

Full file https://disk.yandex.ru/d/DhdlvwfYj4J00A


Solution

  • Let's try this:

    arr=(`grep -En "C[0-9]{6}" $1 | cut -d":" -f 1 | xargs`)
    
    
    >fail.out
    >err.out
    >pass.out
    
    for i in ${!arr[@]}; do
        line_num="${arr[$i]}"
        if head -$line_num $1 | tail -2 | grep -q "/system-err"
        then
            echo -ne "$i) Line: $line_num\t\tError\t\tID: "
            head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}'
            head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}' >> err.out
            #--------------------------------------------------------
        elif head -$line_num $1 | tail -2 | grep -q "/failure"
        then
            echo -ne "$i) Line: $line_num\t\tFailure\t\tID: "
            head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}'
            head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}' >> fail.out
            # ----------------------------------------------------------
        elif head -$line_num $1 | tail -2 | grep -q "/system-out"
        then
            echo -ne "$i) Line: $line_num\t\tPass\t\tID: "
            head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}'
            head -$line_num $1 | tail -1 | grep -Eo '[0-9]{6}' >> pass.out
            # ----------------------------------------------------------
        fi
    done
    
    

    Save the script in a file and try to run it in this way:

    [user@host]# bash script.sh report.junit