Triết lý sống

Phàm làm việc gì trước phải suy xét đến hậu quả của nó
Hành động của người quân tử là giữ tĩnh lặng để tu thân, cần kiệm để dung dưỡng đức độ. Không đạm bạc thì không thể có trí tuệ sáng suốt, không yên tĩnh thì không có chí vươn xa. Học thì phải cần yên tĩnh, muốn có tài năng phải học; không học thì không biết rộng, không có chí thì việc học không thành.

Mong muốn lan man thì không thể nảy sinh cái tinh túy, vội tìm cái hiểm hóc thì không nắm được cái thực tình. Thời gian tuổi tác qua nhanh, ý chí cùng ngày tháng trôi đi trở thành khô héo, phần lớn không tiếp cận được với thời đại, rồi buồn tủi nơi lều nát, sao còn khôi phục lại kịp cái chí hướng được nữa !

Sunday, October 30, 2011

Selenium XPath Tutorial

Thu 18 Sep 2008

In the last tutorial we learnt how to create your first basic script by recording and replaying in the IDE. We are now going to learn how to work with XPathes in your scripts

XPath is a really good way to navigate your site when there are no IDs on elements that you need to work with or is near the element you want to work with.

Note: Web browsers were never designed to handle large amounts of XPath so this means that your scripts may run slowly. You will notice this especially in Internet Explorer.

Start the tutorial by creating a script similar to the one in the last script. Only create a script for the first set of elements on this page. You will be working on the other elements later. I have made a number of programming faux pas which you need take account of in your script.

  1. Enter numbers into each of the text boxes and then click on one of the buttons
    Value 1
    Value 2
    Total
    =

  2. I have created a basic version of what a script should look like here. You will notice that instead of the click targets having a nice ID you have a very odd looking item like the following: //input[@value='divide'].
  3. Let's see about creating your own XPaths from whats on the pages. Selenium IDE has a very useful tool making sure that the XPath that you entered is on the screen. So we take the XPath for the divide button //input[@value='divide'] and put it in the target textbox of Selenium IDE and click on the find button.

    Element Find Button

    When you click on the find button it will highlight the element as shown in the image above.

  4. You can specify, in a XPath any of the attributes that is in the element that you want. If you put //img[(@alt='Element Find Button')] into the IDE what element does it highlight? It will show the image that I put above.

  5. Now what if you had an image with no ID and a partially dynamic alt tag what would you put? My quick answer would be //img[contains(@alt,'Partial')]. This searches all the images on the page and looks for an Alt that contains "Partial". If you wanted to try the use it for this page try //img[contains(@alt,'Element')]. Selenium IDE would highlight the image above again. I am sure that you can guess by now how powerful that can become.
  6. I have created the same elements again below and would like you to create another script to test the items at the top and the items at the bottom.

    Value 1
    Value 2
    Total
    =



    I have created an example here. My example will be different to yours and I will explain next why. XPath example 2

  7. I have used different syntaxs in my script. Each of these is just to show you a different way of finding elements by using different kinds of syntax. There is one example where the target is defined by a number of attributes and another is defined by finding one element and then finding its sibling. Another reason for doing this is because the buttons at the top and the bottom are programmed exactly the same. It allows us to have unique identifiers for elements.

    In my opinion, if an item hasn't been programmed properly, i.e., lacks an id attribute, it is best to code the Selenium test to find that item in relation to an item that *has* been programmed properly rather than to rely on meaningless indexes. This means that when reading through your script you are more likely to understand where //input[@id='BottomTotal']/following-sibling::input[contains(@value,'Add')] is compared to //li[6]/input[4]. This becomes more obvoius when you have a large number of tests in your test suite.

  8. We have now come to the end of this tutorial. There are a number of other items on this page that will produce a XPath for your script. Have a play and try different combinations. You can find very good examples of XPath syntax at W3C Schools. I hope that you found this tutorial very informative but if you ever find that your script is using a lot of XPath go back to the developers and remind them that it is good programming practise to put IDs on elements.

14 comments:

  1. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    Hadoop Training in Chennai
    Cloud computing Training
    Cloud computing courses in Chennai
    Cloud Training in Chennai
    Big Data Hadoop Training
    Hadoop training institutes in chennai

    ReplyDelete
  2. Thanks for sharing this valuable information to our vision. You have posted a worthy blog keep sharing.

    Education
    Technology

    ReplyDelete
  3. Thanks for your interesting ideas.the information's in this blog is very much useful
    for me to improve my knowledge.
    android developer course in bangalore
    Android Training courses near me
    Android Training in chennai

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. If You Are Looking For A Reliable Fx Broker, Don't Rush And Read This XM REVIEW Review First. This Is A Serious Warning Against The Broker's Illegal Activities.

    ReplyDelete
  6. Your good knowledge and kindness in playing with all the pieces were
    very useful. I don’t know what I would have done if I had not
    encountered such a step like this.
    dba course in chennai
    java training institute in chennai
    node js course in chennai

    ReplyDelete