Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pckbls opened this issue Oct 23, 2017 · 1 comment
Open

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

pckbls opened this issue Oct 23, 2017 · 1 comment

Comments

@pckbls
Copy link

pckbls commented Oct 23, 2017

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?

@hekra01
Copy link
Contributor

hekra01 commented Oct 25, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants