tags:

views:

1612

answers:

5

I'm trying to automate testing of the code... well, written without testing in mind (no IDs on many elements, and a lot of elements with the same class names). I would appreciate any help (questions are below the code):

<div id="author-taxonomies" class="menu-opened menu-hover-opened-inactive" onmouseover="styleMenuElement(this)" onmouseout="styleMenuElement(this)" onclick="toggleSFGroup(this)">Author</div>
<div id="author-taxonomies-div" class="opened">
    <div id="top-level-menu" class="opened">
        <div id="top-level-menu-item-1" class="as-master">
            <div class="filter-label"> Name</div>
        </div>
        <div id="top-level-menu-item-1" class="as-slave"
            style="top: 525px; left: 34px; z-index: 100; display: none;"> </div>
        <div id="top-level-menu-item-2" class="as-master">
            <div class="filter-label">Title</div>
        </div>
        <div id="top-level-menu-item-2" class="as-slave">
            <div id="top-level-menu-item-2" class="as-slave-title as-slave-title-subgroup"
                >Title</div>
            <div id="top-level-menu-item-2" class="as-slave-body"> </div>
            <div class="as-slave-buffer"> </div>
        </div>
        <div id="top-level-menu-item-3" class="as-master">
            <div class="filter-label">Location</div>
        </div>
        <div id="top-level-menu-item-3" class="as-slave"> </div>
    </div>
</div>

The question is: how to refer particular labels of this menu and the properties with xPath expressions? For example, if I want to:

  1. verify the "Location" label is there
  2. check if "Title" with class "as-slave" is not visible at the moment
+1  A: 

If you're just starting with Selenium, download the selenium add-on for Firefox. As you click on DOM elements, Selenium shows you the xpath to access it.

Julien
Yeah, it does, but it looks unusable:/html/body/div/div[3]/div/table/tbody/tr/td/div[3]/div[2]/div[16]/div/div
+1  A: 

It would be something similar to:

  1. //div[@id="top-level-menu-item-3"]/div[@class="filter-label"]
  2. //div[@id="top-level-menu1"] --- and check in code for display: none ... assuming it is selenium rc you are using

Update: also be sure to install the following firefox addin, it is Really useful when trying different xpath expressions on a site https://addons.mozilla.org/en-US/firefox/addon/1095

eglasius
Its right though in my experience you would need to correct the xpath syntax for it to actually work, xpath=//div[@id="top-level-menu-item-3"]/div[@class="filter-label"]
j pimmel
@j pimmel thx, updated it - that's correct, since @ is for attributes :)
eglasius
+1  A: 

As a side note: try to avoid using xpath locators in Selenium, if possible. If you have a long xpath expression, it can be up to 20 times slower for Selenium to find the element compared to identifying it using its unique ID. Of course, sometimes there is no alternative to using xpath. However, when you do use it, keep '//' expressions to minimum - this is a real performance killer.

Igor Brejc
A: 

I'm having the strange things with this (maybe I'm doing something wrong :( ). So: the example code: a class="Test1" href="Test1.com/">TEST
When I'm trying to: |verifyElementPresent| //a[@class="Test1"] all works normally, but if: |verifyElementPresent | //a[@class="Test1"]/a[@href="Test1.com/"] this thing just does not works :(. Please help me where I'm wrong.

+1  A: 

I am currently working on an open source library for generating xpath expressions through a fluent .Net API. The idea is to be able to generate xpath based selenium locators without having to know xpath.

Here's an example of how the library can be used in your case:

XPathFinder.Find.Tag("div").With.Attribute("id", "top-level-menu-item-3").And.Child("div").With.Attribute("class", "filter-label").ToXPathExpression();

This will produce the following xpath: "//div[@id='top-level-menu-item-3']/div[@class='filter-label']"

Check it out at http://code.google.com/p/xpathitup/

Tor