qtwebdriver icon indicating copy to clipboard operation
qtwebdriver copied to clipboard

How to find an element using its id specified in the QML file?

Open pckbls opened this issue 6 years ago • 1 comments

Hi there,

I'm currently playing around with qtwebdriver and the calqlatr example from the official Qt docs. I've sucessfully managed to cross-compile qtwebdriver for Android and bake the qtwebdriver into the calqlatr Android APK. Pressing the calculator buttons and reading the display values also works fine. Thank you for creating qtwebdriver!

Now I'm facing the following situation: Inside Display.qml I can find the following QML definition:

Image {
    anchors.right: rect.left
    source: "images/paper-edge-left.png"
    height: parent.height
    fillMode: Image.TileVertically
}
Image {
    anchors.left: rect.right
    source: "images/paper-edge-right.png"
    height: parent.height
    fillMode: Image.TileVertically
}
Image {
    id: grip
    source: "images/paper-grip.png"
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.bottom: parent.bottom
    anchors.bottomMargin: 20
}

Now I want to find the element with the grip ID. Right now I'm using the following piece of Python code:

def find_grip(self):
    items = self._display_element.find_elements_by_xpath('Display/Item/Image')
    assert 3 == len(items)
    return items[2]

which works but it does not actually uses the element's ID for lookup. If someone changed the order of the Image definitions in the QML file, my code would break.

According to some of the other issues posted here on GitHub one workaround would be, assigning a objectName property to the Item and then executing some custom JavaScript, e.g.:

def find_grip(self):
    return self._driver.execute_script('var v = ObjectNameUtils.findChild("grid"); return v.WHAT_PROPERTY_AM_I_SUPPOSED_TO_USE_HERE;')

What am I supposed to do with v inside the JavaScript environment to yield the Python item that I need for further interaction?

Would it be possible to avoid the objectName workaround? Is it somehow possible to find the grip Element using its ID grip?

pckbls avatar Oct 23 '17 14:10 pckbls

What am I supposed to do with v inside the JavaScript environment to yield the Python item that I need for further interaction?

See example in the wiki: https://github.com/cisco-open-source/qtwebdriver/wiki/QML-Specifics Use v to get attributes of the object. then èxecuteScript` returns their value, and you can assert.

Would it be possible to avoid the objectName workaround? Is it somehow possible to find the grip Element using its ID grip?

Not possible. We looked into it but Qt provides no programmatic access to id. To query id, only way is with objectName.

hekra01 avatar Oct 25 '17 02:10 hekra01