Selenium With Java and Python For Mobile Apps & Web Apps......!

Tuesday, 17 February 2015

Drop Down or Select List in Selenium Web Driver.

Select Class Example....How to handle Select List or Drop Down

package basic;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SelectClassExample {
public static void main(String[] args) throws InterruptedException {

WebDriver driver=new FirefoxDriver();//launch browser
driver.get("http://piyush-pc/login.do");//navigate to actiTime login page
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//input[@type='submit']")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 driver.findElement(By.linkText("Projects & Customers")).click();

 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 WebElement selectlst=driver.findElement(By.name("selectedCustomer"));
 Select sel=new Select(selectlst);
    sel.selectByIndex(1);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.findElement(By.xpath("//input[contains(@value,'Show')]")).click();
    Thread.sleep(3000);
    System.out.println("done");
}
}

Or
package app1;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class MultiSelectList {
public static void main(String[] args) throws InterruptedException {

WebDriver driver=new FirefoxDriver();
System.out.println("done");
driver.get("http://piyush-pc/login.do");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//input[@type='submit']")).click();
Thread.sleep(2000);
driver.findElement(By.linkText("Reports")).click();
Thread.sleep(2000);
WebElement wb=driver.findElement(By.name("users"));
Select sel=new Select(wb);
System.out.println(sel.isMultiple());
sel.selectByIndex(0);
//sel.selectByIndex(1);

}


}

Dynamic Web List:
package app1;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class DynamicWebList {
public static void main(String[] args) {

WebDriver driver=new FirefoxDriver();
System.out.println("done");
driver.get("http://piyush-pc/login.do");
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//input[@type='submit']")).click();
WebElement wb=driver.findElement(By.name("customerProject.shownCustomer"));
Select sel=new Select(wb);

List<WebElement> lst = sel.getOptions();

//display size of list
System.out.println(lst.size());
String expval="C";
boolean flag=false;
//display dynamic weblist item name
for (int i = 0; i < lst.size(); i++) {
System.out.println(lst.get(i).getText());
String val=lst.get(i).getText();
if(expval.equals(val))
{
sel.selectByVisibleText(expval);
flag=true;
break;
}
}
if(flag)
{
System.out.println("pass");
}
else {
System.out.println("fails");
}
}


}


Key Board Operation Example :
package basic;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class KeyboardOperationExample {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();//launch browser
driver.get("http://piyush-pc/login.do");//navigate to actiTime login page
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
String un= driver.findElement(By.name("username")).getAttribute("value");
   System.out.println(un);
//driver.findElement(By.name("pwd")).sendKeys("manager");
Thread.sleep(3000);
Actions act=new Actions(driver);
WebElement wb=driver.findElement(By.xpath("//input[@type='submit']"));
act.moveToElement(wb).perform();


act.sendKeys(Keys.SHIFT,Keys.DELETE).perform();
act.sendKeys(Keys.ENTER).perform();

}


}

Get All Web Elements Example...

package basic;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GetAllWebElementExamole {
public static void main(String[] args) {

WebDriver driver=new FirefoxDriver();//launch browser
driver.get("http://piyush-pc/login.do");//navigate to actiTime login page
driver.findElement(By.name("username")).sendKeys("admin");
String un= driver.findElement(By.name("username")).getAttribute("value");
   System.out.println(un);
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.xpath("//input[@type='submit']")).submit() ;
//to get all link element present in ui
List<WebElement> lst =driver.findElements(By.xpath("//a"));
//to get all web elements
//List<WebElement> lst1 =driver.findElements(By.xpath("//*"));
// System.out.println(lst1.size());
System.out.println(lst.size());

//show all
for (int i = 0; i < lst.size(); i++) {
System.out.println(lst.get(i).getText());


}
for (int i = 0; i < lst1.size(); i++) {
System.out.println(lst1.get(i).getText());

}

}

}


Auto Suggestion.....
package pac1;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AutoSugesstEditBox {

public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in/?gws_rd=cr");
driver.findElement(By.id("gbqfq")).sendKeys("cognizant wiki" ,Keys.ENTER);


}


}



Translate

Popular Posts

Total Pageviews