Merge lp:~kenneth-koontz/selenium-simple-test/bug-828050 into lp:selenium-simple-test

Proposed by Kenneth Koontz
Status: Work in progress
Proposed branch: lp:~kenneth-koontz/selenium-simple-test/bug-828050
Merge into: lp:selenium-simple-test
Diff against target: 46 lines (+26/-5)
1 file modified
src/sst/actions.py (+26/-5)
To merge this branch: bzr merge lp:~kenneth-koontz/selenium-simple-test/bug-828050
Reviewer Review Type Date Requested Status
Corey Goldberg (community) Needs Fixing
Review via email: mp+75946@code.launchpad.net

Description of the change

link_click now has the ability to locate a link by partial id. Utilizes xpath to check the children element of selected id_or_elem for a unique link.

e.g.
link_click('bugtarget-picker-tasksummary')

To post a comment you must log in.
Revision history for this message
Corey Goldberg (coreygoldberg) wrote :

I looked at the implementation and I'm wondering if this is too much magic for the link_click() action.

the problem I see is..
if we add this to the regular link_click(), we lose the ability to click links with a specific id that is a substring of other id's.

for example, say the page has the following:

<a id="foo" href="/">link</a>
<a id="foobar" href="/">link</a>

it would no longer be possible to do:
link_click('foo') and click the first link,
since link_click('foo') would do a partial match and find both links.

so..

what about leaving link_click() as-is, and adding a new action to handle this instead:

maybe something like:

def partial_link_click(partial_id, check=False, wait=True)

thoughts?

-Corey

review: Needs Fixing
Revision history for this message
Kenneth Koontz (kenneth-koontz) wrote :

Corey, great point! I actually thought about extracting functionality and putting it into another action; however, I was left with the same thing. If we give the user the ability to add partial_id's, then the user can input any substring, like the aforementioned 'foobar' example. Which means that if we want to have partial locators with id's, the user must give some thought into the locator otherwise they will be left with an assertion error.

Take for instance a dynamic website that appends a unique key every time a new post is created. Suppose the string to append the key to is postlink. We would likely see postlink1, postlink2, ... postlink{n}. Furthermore, suppose there was another link element with the id, postlink-summary. In this case, link_click('postlink') won't work; but my question is-should it? Partial matching or not, if the user provides an ambiguous selector, they should be notified and there should be no action associated with it. So in link_click's case, if there is ambiguity in 'element_locator', the following line:

"""
if not get_element_by_xpath(element_locator).get_attribute('href')
"""

should catch this ambiguity in 'get_element_by_xpath'.

"""
if len(elements) != 1:
    msg = 'Could not identify element: %s elements found' % len(elements)
    _raise(msg)
"""

Thanks!

-K

Revision history for this message
Leo Arias (elopio) wrote :

What about adding the possibility to use regular expressions?

Revision history for this message
Kenneth Koontz (kenneth-koontz) wrote :

Leo, I'm not sure how regular expressions would work in this case. Can you provide an example or your thoughts?

Corey, is your stance still the same? Would you like me to just pull out the functionality from this merge and put it into a separate function? Say, partial_link_click()? What are your thoughts?

Unmerged revisions

187. By Kenneth Koontz

#828050: link_click now has ability to locate by partial id

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/sst/actions.py'
--- src/sst/actions.py 2011-09-17 15:32:04 +0000
+++ src/sst/actions.py 2011-09-19 03:41:25 +0000
@@ -400,16 +400,37 @@
400400
401def link_click(id_or_elem, check=False, wait=True):401def link_click(id_or_elem, check=False, wait=True):
402 """402 """
403 Click the specified link. As some links do redirects the location you end403 Click the specified link. If the specified element is not a link, look for
404 up at is not checked by default. If you pass in `check=True` then this404 children tags that are links and click on a unique link, if present. If there
405 action asserts that the resulting url is the link url.405 is not a unique link within the specified element, raise an AssertionError.
406 Some links do redirects the location you end up at is not checked by default.
407 If you pass in `check=True` then this action asserts that the resulting url is
408 the link url.
406409
407 By default this action will wait until a page with a body element is410 By default this action will wait until a page with a body element is
408 available after the click. You can switch off this behaviour by passing411 available after the click. You can switch off this behaviour by passing
409 `wait=False`."""412 `wait=False`."""
413 element_locator = '//*[contains(@id, %r)]' % id_or_elem
414
415 if not get_element_by_xpath(element_locator).get_attribute('href'):
416 link_locator = element_locator + '//a'
417 link = get_elements_by_xpath(link_locator)
418 if len(get_elements_by_xpath(link_locator)) == 1:
419 link = get_element_by_xpath(link_locator)
420 link_url = link.get_attribute('href')
421 elif len(get_elements_by_xpath(link_locator)) > 1:
422 msg = "Multiple links are present within %s, did not click" % id_or_elem
423 _raise(msg)
424 return
425 else:
426 msg = "No links are present within %s, did not click" % id_or_elem
427 _raise(msg)
428 return
429 else:
430 link = is_link(id_or_elem)
431 link_url = link.get_attribute('href')
432
410 _print('Clicking link %r' % id_or_elem)433 _print('Clicking link %r' % id_or_elem)
411 link = is_link(id_or_elem)
412 link_url = link.get_attribute('href')
413 link.click()434 link.click()
414435
415 if wait:436 if wait:

Subscribers

People subscribed via source and target branches