Professional Documents
Culture Documents
1. System.setProperty("webdriver.ie.driver", "D:\\temp\\iedriverserver.exe");
System.setProperty("webdriver.chrome.driver", "D:\\temp\\chromedriver.exe");
driver.navigate.to("http://www.google.com"); // Open but not wait till the whole page loads.
3. driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
driver.manage().window().maximize();
4. driver.getTitle();
driver.getCurrentUrl();
driver.getPageSource();
ID, Name, Class Name, xpath, CSS, linkText, Partial Link Text, tagName.
driver.findElement(By.id("dfsdfad"));
driver.findElement(By.Name("erwerwerw"));
Sreehari, B
Retrieving WebElement Properties:
1. Object Properties
2. Object Methods
dimension.widhth;
dimension.height;
getAttributes
1. element.getAttribute("value");
2. element.getAttribute("align");
getCSSValue
1. element.getCSSValue("width");
2. element.getCSSValue("background-color");
3. element.getCSSValue("font-famliy");
Sreehari, B
Operations/Methods On WebElement
element.click();
element.sendKeys("Selenium");
findElements method will return list of all elements matching from the current page
findElement method will return only one element matching from the current page
/html/body/div/div/div[1]/p[1]/select
//input[@value ='Petrol']
//input[@value ='Diesel']
if(!petrol.isSelected())
petrol.click();
Sreehari, B
Radio Group
for(WebElement i : fuelType)
if(strActual.equalsIgnoreCase(strExpected)
if(!i.isSelected())
i.click();
Check box:
if(!airbags.isSelected())
airbags.click();
Sreehari, B
1. Using List
int l = list.size();
// Code to find the expected list box item in a collection of list items
if(strActual.equals(strExpected)
if(status)
System.out.println("Element found");
else
2. Using Select
int n = dropdown.getOptions().size();
Sreehari, B
//How to select dropdowns
dropdown.selectByVisibleText("Audi");
dropdown.selectByValue("audi");
dropdown.selectByIndex(2);
// Multiselect
int m = dropdown.getAllSelectedOptions().size();
// deselect
dropdown.deselectByVisibleText("Audi");
dropdown.deselectByValue("audi");
dropdown.deselectByIndex(2);
dropdown.deselectAll();
import java.io.File;
import org.openqa.selenium.OutputType;
import org.openqa.selenuim.TakesScreenshot;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.Calendar;
import java.text.SimpleDateFormat;
Sreehari, B
// Get current date and time in a string
-------
Alert
import org.openqa.selenium.Alert;
// To generate alert
driver.findElement(By.xpath("dsjflkjdflk")).click();
Alert A = driver.switchTo().alert();
A.accept();
A.dismiss();
A.sendKeys("This is selenium");
driver.switchTo().window(myWindowHandle);
2. driver.close();
Sreehari, B
NoSuchWindowException: The driver.switchTo().window()
method throws the "NoSuchWindowException" exception when it fails to identify the desired
window.
if(! allWindows.isEmpty)
try{
driver.close();
break;
catch(NoSuchWindowExpection e)
e.printStackTrace();
driver.switchTo().window(parentWindowId);
Sreehari, B
How to verify if an element is displayed in the current web page?
It will return true if the element is present in the page, else it return false.
Frames:
HTML frames allow developers to present documents in multiple views, which may be
independent
windows or subwindows.
A page with frames is created by using <frameset> tag. or the <iframe> tag.
driver.swithTo().frame(driver.findElement(By.classname("demo-name"));
driver.swithTo().defaultContent();
driver.switchTo().frame(0);
driver.switchTo().frame(1);
driver.switchTo().frame(2);
editable.sendKeys("Welcome");
driver.swithTo().defaultContent();
Sreehari, B
List<WebElement> frames = driver.findElements(By.tagName("frame"));
driver.switchTo().frame(framei)
break;
else
driver.switchTo().defaultContent();
WebTable Element:
Sreehari, B
EX: /table/tbody/tr[0]/td[0] = 1
/table/tbody/tr[0]/td[1] = 2
/table/tbody/tr[0]/td[2] = 3
/table/tbody/tr[1]/td[0]
/table/tbody/tr[1]/td[1]
/table/tbody/tr[1]/td[2]
/table/tbody/tr[2]/td[0]
/table/tbody/tr[2]/td[1]
/table/tbody/tr[2]/td[2]
tr1 4 5 6
tr2 7 8 9
Sreehari, B
// To find the size
if(cellValue.equalsIgnoreCase(ExpValue)) {
break; } }
-----------------------------------------------------------------
1. implicit wait : Just wait for some time. Will wait for maximum amount of time.
2. explicit wait : Wait for some time and check for object each 500 milliseconds.
Sreehari, B
Thread.sleep(3000); // Java statement
driver.manage().timeouts().implicitlyWait(10, TimeUNIT.SECONDS);
// driver will wait for an element to appear in DOM for 10 seconds after an initial try
2. Explicit Wait:
wait.until(ExpectedConditions.titleContains("selenium");
The WebDriverWait object will call the ExpectedConditions class object every
// In the above code 10 stands for 10 seconds. Default polling time is 500 msecs.
wait.until(ExpectedConditions.elementToBeClicable(By.xpath("abc"));
Sreehari, B
3. Fluent Wait:
.withTimeout(10, TimeUnit.SECONDS)
.pollingEvery(2, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
--------------
</ul>
from.clear();
from.sendKeys("Che");
Sreehari, B
WebElement autoOption = driver.findElement(By.id('ui-id-1'));
wait.until(ExpectedConditions.visibityOf("autoOption"));
if(expFrom.equalsIgnoreCase(actualFrom))
fromAutoOption.click();
break;
}}
-----
Sreehari, B