Friday 9 May 2014

Selenium Script for begginers

To select Combo box :

List dd = driver.findElement(By.name("_sacat")).findElements(By.tagName("option"));
   for (WebElement option : dd) {
    System.out.println(option.getText());
    if ("Consumer Electronics".equalsIgnoreCase(option.getText())){
     option.click();
     break;
    }
To select visible text from option

public void selectByVisibleText() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        selectBox.selectByValue("Customer service");
    }

To Select index from option

public void selectByIndex() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        selectBox.selectByIndex(2);
    }

To get all option from selected tag


    public void getOptions() {
        Select selectBox = new Select(driver.findElement(By
                .cssSelector("select#id_contact")));
        List<WebElement> selectOptions = selectBox.getOptions();
        for (WebElement temp : selectOptions) {
            System.out.println("getText" + temp.getText());
        }
    }
''Read data from a web table  specific row / columns using webdriver

    import java.util.List;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;

    public class webdr_table
    {
        public static void main(String[] args) throws Exception
        {
            String str,found="false";
            FirefoxDriver fd=new FirefoxDriver();

            fd.get("http://content.icicidirect.com/newsiteContent/Market/MarketStats.asp?
stats=DailySharePrices");

            Thread.sleep(5000);

            WebElement table=fd.findElement(By.id("gridSource"));
         
            List rows=table.findElements(By.tagName("tr"));

            for(WebElement row : rows)
            {
                List cols=row.findElements(By.tagName("td"));
                for(WebElement col : cols)
                {
                    str=col.getText().trim();
                    if(str.matches("ABB"))
                    {
                        List vals=row.findElements(By.tagName("td"));
                        for(WebElement v:vals)
                        {
                            System.out.println(v.getText());
                        }
                        found="true";
                    }
                }
                if(found.matches("true"))
                {
                    break;
                }
            }     
          }
    }
Locating 'Element  by WebDriver
1 By ID:
 in Java: driver.findElement(By.id("element id"))
2. By CLASS:
 in Java: driver.findElement(By.className("element class"))
 3. By NAME:
 in Java: driver.findElement(By.name("element name"))
 4. By TAGNAME:
 in Java: driver.findElement(By.tagName("element html tag name"))
 5. By CSS Selector:
 in Java: driver.findElement(By.cssSelector("css selector"))
 6. By Link:
 in Java: driver.findElement(By.link("link text"))
 7. By XPath:
 in Java: driver.findElement(By.xpath("xpath expression"))

Find th Link in the page
@Test
public void findLinksTest(){
   //Get all the links displayed
   List links = driver.findElements(By.tagName("a"));
   assertEquals(2, links.size());
   for(WebElement link : links)
   System.out.print(link.getAttribute("href"));
}
 
How to find a link with Selenium WebDriver API by full text:
 
WebElement firstLink = driver.findElement(By.linkText("First Link"));
 assertEquals("#link1", firstLink.getAttribute("href"))
 
 

1 comment: