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

Proposed by Kenneth Koontz on 2011-09-19
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/ (+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) 2011-09-19 Needs Fixing on 2011-09-19
Review via email:

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.


To post a comment you must log in.
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.


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)



review: Needs Fixing
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)



Leo Arias (elopio) wrote :

What about adding the possibility to use regular expressions?

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 on 2011-09-19

#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
1=== modified file 'src/sst/'
2--- src/sst/ 2011-09-17 15:32:04 +0000
3+++ src/sst/ 2011-09-19 03:41:25 +0000
4@@ -400,16 +400,37 @@
6 def link_click(id_or_elem, check=False, wait=True):
7 """
8- Click the specified link. As some links do redirects the location you end
9- up at is not checked by default. If you pass in `check=True` then this
10- action asserts that the resulting url is the link url.
11+ Click the specified link. If the specified element is not a link, look for
12+ children tags that are links and click on a unique link, if present. If there
13+ is not a unique link within the specified element, raise an AssertionError.
14+ Some links do redirects the location you end up at is not checked by default.
15+ If you pass in `check=True` then this action asserts that the resulting url is
16+ the link url.
18 By default this action will wait until a page with a body element is
19 available after the click. You can switch off this behaviour by passing
20 `wait=False`."""
21+ element_locator = '//*[contains(@id, %r)]' % id_or_elem
23+ if not get_element_by_xpath(element_locator).get_attribute('href'):
24+ link_locator = element_locator + '//a'
25+ link = get_elements_by_xpath(link_locator)
26+ if len(get_elements_by_xpath(link_locator)) == 1:
27+ link = get_element_by_xpath(link_locator)
28+ link_url = link.get_attribute('href')
29+ elif len(get_elements_by_xpath(link_locator)) > 1:
30+ msg = "Multiple links are present within %s, did not click" % id_or_elem
31+ _raise(msg)
32+ return
33+ else:
34+ msg = "No links are present within %s, did not click" % id_or_elem
35+ _raise(msg)
36+ return
37+ else:
38+ link = is_link(id_or_elem)
39+ link_url = link.get_attribute('href')
41 _print('Clicking link %r' % id_or_elem)
42- link = is_link(id_or_elem)
43- link_url = link.get_attribute('href')
46 if wait:


People subscribed via source and target branches