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

Support for locating elements By XPath #31

Open
gian-te opened this issue Sep 20, 2016 · 9 comments
Open

Support for locating elements By XPath #31

gian-te opened this issue Sep 20, 2016 · 9 comments

Comments

@gian-te
Copy link

gian-te commented Sep 20, 2016

Hi, Searching for an element by xpath throws unknown strategy in By constructor.

image

will there be support for xpath locators?

@NickAb
Copy link
Contributor

NickAb commented Sep 20, 2016

There are no concrete plans on implementing XPath support in mobile drivers at the moment. You can take a look at notes in 2gis/Winium.Mobile#29 and try implementing it yourself.

One of the reasons we have not implemented it yet is the fact that System.Xml.XPath was not available in Silverlight or Store Apps and one would had to use some third party repackaging of this assembly for targeted platforms.

In our usage of the driver we were able to use mostly Id and Name strategies, and rarely ClassName strategies. Seems that XPath strategy is need in rare cases in complex UI or when there are no Id or Names specified in the elements, which might lead to the problem of tests breaking too often due to minor changes to UI structure.

@gian-te
Copy link
Author

gian-te commented Sep 20, 2016

Yes , we are encountering 'breaks' in our tests due to sudden changes in UI tree of the AUT for web and mobile apps. We constantly have to update the locators depending on the changes if that happens, Thanks for the link, I'll investigate on what we can do with this 'cause most of our locators are in XPath

@gian-te gian-te closed this as completed Sep 20, 2016
@gian-te gian-te reopened this Sep 20, 2016
@gian-te
Copy link
Author

gian-te commented Sep 20, 2016

May I know if you have tested codeduitestproject on Hybrid windows phone applications using Webview?

@NickAb
Copy link
Contributor

NickAb commented Sep 20, 2016

It was not tested extensively. I am not sure if I have run any scenarios against any complex hybrid app. If I remember correctly I've run a scenario against single view app that had some native buttons and a single WebView. I've run scenario against preinstalled web browser in the emulator.

WebViews are accessible by CodedUI and the driver (as it uses CodedUI), but it will not be how it works in Appium on Android or iOS, where you basically have two different contexts, one of which is web context that works like regular web page testing using selenium on desktop.
In case of Winium.CodedUI you will be able to access web view elements just like any other native UI element, but this leads to a few limitations, you can not access html tag attributes. You will have a limited means to locate web elements or interact with them.

If you have hybrid app, you can run an dummy test that prints out page source using PageSource command. All elements that you can see in outputted page source will be accessible by driver using class, id or name as locators.

@gian-te
Copy link
Author

gian-te commented Sep 20, 2016

So if I understood correctly, we cannot access html tags in the webview in the hybrid app (which is the reason why xpath doesn't and will not work) because the automation tree when using codedui in the app is different from the html tree when opening in browser?

Also how do I create a dummy PageSource command? Is it similar to how we retrieve the page source using selenium webdriver? I am currntly away from my machine so I can't test our PageSource command for webdriver yet

@gian-te
Copy link
Author

gian-te commented Sep 20, 2016

Do you think it's possible to parse xpath locators to its equivalent codedui "strategy" and "value" ?

@NickAb
Copy link
Contributor

NickAb commented Sep 21, 2016

You can not access html tags in a way you do with selenium, they will be accessible like other native UI elements and will have the same set of properties like id, name and class, which are mapped form html tag attributes by CodedUI.

By dummy page source test I meant an empty test case that simple prints page source of the screen you are interested in without any assertions. Something like

class Test(unittest.TestCase):
    desired_capabilities = {
        'deviceName': 'Emulator',
        'app': 'aut.appx'
    }

    def setUp(self):
        self.driver = webdriver.Remote(
            command_executor='http://localhost:9999',
            desired_capabilities=self.desired_capabilities)

    def tearDown(self):
        self.driver.quit()

    def test_page_source(self):
        # assuming you want a page source of the first screen in the app
        # if not, you can simple add break point in test, manually navigate to the screen
        # that has WebView and release the break point
        source = self.driver.page_source
        print(source)

XPath can be converted into combination of basic strategies and some code. For example, if you used XPath to find element with id="Submit" that is nested into element with Id="LogInForm", then you could use:

form = driver.find_element_by_Id("LoginForm")
submit = form.find_element_by_id("Submit")

More complex cases might require getting all elements by some id, name or class and iterating them.

@gian-te
Copy link
Author

gian-te commented Sep 22, 2016

So if I have a webview that has an element like this:
image

The coded ui driver can locate it by passing "Id" to strategy and "hplogo" in value, right?

@NickAb
Copy link
Contributor

NickAb commented Sep 22, 2016

It should, but I would recommend you to check this one using page source.

I had some problems with a few elements on google page and, unfortunately, I do not remember details. I think I was able to locate elements using id, but not name as it was not forwarded by CodedUI to the driver.

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