Sunday, 15 February 2015

HOW TO LOCATE ELEMENT

     Since the selenium work on the DOM model its important have the knowledge of the element locator in Selenium. Selenium provides a variety of selector.

        These are the selector provided by the Selenium to locating object. Effect uses of these are required when we write the code for test. Consider the scenario we want check the hyperlinks in a page of a website. For that we can use the locator to identify the link and click on it. As I mentioned earlier a HTML tag contain any of the above listed attribute. By giving the name with in the double quote we can select the element.

  • driver.findElement(By.xpath(".//*[@id='c).click();
    In the above code, the driver finds the element using the XPath and performs a click on the link.
  • driver.findElement(By.linkText("Java")).click(); 
This code search for the link which contains the text Java and perform a click on the particular link.
  • driver.findElement(By.name("User")).click();
This code search for the name attribute where the name as User and perform a click on the particular link.
  •  driver.findElement(By.Id("User")).click();
     This code search for the Id attribute where the name as User and perform a click on the particular link.
Similarly the rest of the selectors are easily understood. These selectors are the basic building blocks of the test suite 


BEST PRACTICE
        Knowing the basic of JAVA will be good for the programming, such as importing packages accessing methods and parametrize, while writing the code one of the key thing to consider is the code re usability. Following a framework will be good for the code re usability.

No comments:

Post a Comment