Accessing Components Children.

It is often extremenly useful to test components that are comprised of multiple children to achieve the desired functionality. This can be accomplished by using findChild() to access a components children based on an ObjectName.

ObjectNames are a string value that can be added to any component, and must be unique to each object. findChild() will recursively search a component for the object name that it is given.

Example:

Component Code:

import QtQuick 2.0
Rectangle{
    id:background
    objectName:"background"
    color:"black"
    MouseArea{
        id:clickable
        objectName:"clickable"
        anchors.left:parent.left
        anchors.top:parent.top
        width:500
        height:500
        Rectangle{
            id:clickRepresentation
            objectName:"clickRep"
            anchors.fill:parent
            color:clickable.pressed?"green":"yellow"
        }
    }
}

Test Code

import QtQuick 2.0
    import QtTest 1.2
    TestCase{
        id:backgroundTests
        name:"TestBackground"
        when:windowShown
        Background{
            id:bg
        }
        property color expectedBG:"yellow"
        function test__non_clicked_color(){
            let clickRep = findChild(bg,"clickRep");

            compare(clickRep.color,expectedBG);
        }
    }