Popup Windows and Frames
Popup window handling is one of the must handle things in Automation
and Webdriver does is really well. Using the window handle command.
|
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
|
Once action is performed, switch back to parent window.
|
driver.switchTo().window(driver.getWindowHandle());
|
List Box
Select an item in Listbox using Selenium Webdriver is quite easy, there two way’s to do it.
|
WebElement select = driver.findElement(By.id("selection"));
List<WebElement> options = select.findElements(By.tagName("option"));
for (WebElement option : options) {
if("Germany".equals(option.getText()))
option.click();
}
|
Or
|
WebElement roleDropdown = driver.findElement(By.id("");
roleDropdown.click();
|
Then
|
WebElement roleOptionOne = driver.findElement(By.id(FIRST_OPTION));
roleOptionOne.click();
|
Right Click
|
WebElement webelement = driver.findElement(By.xpath("*Xpath Locator*"));
Actions actions = new Actions(driver);
Action action = actions.contextClick(webelement).build();
action.perform();
<h2>Double Click</h2>
Actions action = new Actions(driver);
action.doubleClick(myElemment);
action.perform();
|
Excel Reader
Read or write data from excel using the jxl or POI, I will prefer POI. Create a property reader file then read the data
|
Workbook workbook = WorkbookFactory.create(new FileInputStream(file));
Sheet sheet = workbook.getSheetAt(0);
|
Database Connection
To connect to Database using WebDriver with Java, we use DBC(“Java Database Connectivity) API.
|
DriverManager.getConnection(URL, "username", "password" )
|
Time Outs – Wait
Implicit Waits:
|
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
|
Or
Explicit Waits
|
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
<h2>Alert Box</h2>
driver.findElement(By.id("updateButton")).click();
Alert alert = driver.switchTo().alert();
|
alert.accept(); or alert.dismiss();
Navigation
|
driver.navigate().to(www.google.com);
driver.get("http://www.google.com");
driver.navigate().forward();
driver.navigate().back();
driver.navigate().refresh();
driver.manage().deleteAllCookies();
driver.close();
|
Table Columns
|
WebElement Table = driver.findElement(By.id("")));
List<WebElement> Row = Table.findElements(By.tagName("tr"));
|
Drag And Drop
|
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
|
Mouse Over
|
Actions actions = new Actions(driver);
WebElement mouseover = driver.findElement(By.id(""));
actions.moveToElement(mouseover);
actions.click().perform();
|
IsElement/TextPresent
There are various way to check weather an element is present of not before execution of a piece of code.
Option 1
|
private boolean isElementPresent(WebDriver driver, By by){
try{
driver.findElement(by);
return true;
}catch(NoSuchElementException e){
return false;
}
}
|
Option 2
|
if (driver.findElements(locator).size() > 0) {
return true;
} else {
return false;
}
}
|
Option 3
Sweet and simple one line of code
|
Webelement elementpresent = driver.PageSource.Contains("Sign out")
|
Web Elements Finding
These are generic ways to do certain common task i.e.
Id:
WebElement element = driver.findElement(By.id(""));
Name:
WebElement element = driver.findElement(By.name(""));
Tag Name:
WebElement frame = driver.findElement(By.tagName("iframe"));
Xpath:
WebElement element = driver.findElement(By.xpath.name(""));
CSS:
WebElement element = driver.findElement(By.CSS.name(""));
LinkText:
WebElement element = driver.findElement(By.LinkText.(""));
No comments:
Post a Comment