Most common functions used in WebDriver to make a web-application testing.
- IsElementPresent/Text Present
- Finding elements by using function that take argument of By class
private boolean isElementPresent(WebDriver driver, By by)
try{
driver.findElement(by);
return true;
}
catch(Exception e)
{
return false;
}
} - Using the size to decide whether element is there or not
if(driver.findElements(Locator).size()>0
{
return true
}else
{
return false
}
} - Finding the text using the PageSource
driver.PageSource.Contains("TEXT that you want to see on the page");
- Finding elements by using function that take argument of By class
- Finding WebElement by using various locators
- Using ID
WebElement welement = driver.findElement(By.id("Id from webpage"));
- Using Name
WebElement welement = driver.findElement(By.name("Name of WebElement"));
- Using Tag Name
WebElement welement = driver.findElement(By.tagName("tag name"));
- Using Xpath
WebElement welement = driver.findElement(By.xpath("xpath of webElement"));
- Using CSS
WebElement welement = driver.findElement(By.CSS("CSS locator path"));
- Using LinkText
WebElement welement = driver.findElement(By.LinkText("LinkText"));
- Using ID
- Fetching pop-up message
this is the function that would help you in fetching the message
public static String getPopupMessage(final WebDriver driver) {
String message = null;
try {
Alert alert = driver.switchTo().alert();
message = alert.getText();
alert.accept();
} catch (Exception e) {
message = null;
}
System.out.println("message"+message);
return message;
} - Canceling pop-up
public static String cancelPopupMessageBox(final WebDriver driver) {
String message = null;
try {
Alert alert = driver.switchTo().alert();
message = alert.getText();
alert.dismiss();
} catch (Exception e) {
message = null;
}
return message;
} - Inserting string in Text Field
public static void insertText(WebDriver driver, By locator, String value) {
WebElement field = driver.findElement(locator);
field.clear();
field.sendKeys(value);
} - Reading ToolTip text
public static String tooltipText(WebDriver driver, By locator){
String tooltip = driver.findElement(locator).getAttribute("title");
return tooltip;
} - Selecting Radio Button
public static void selectRadioButton(WebDriver driver, By locator, String value){ List select = driver.findElements(locator);
for (WebElement element : select)
{
if (element.getAttribute("value").equalsIgnoreCase(value)){
element.click();
}
} - Selecting CheckBox
public static void selectCheckboxes(WebDriver driver, By locator,String value)
{
List abc = driver.findElements(locator);
List list = new ArrayListArrays.asList(value.split(",")));
for (String check : list){
for (WebElement chk : abc){
if(chk.getAttribute("value").equalsIgnoreCase(check)){
chk.click();
}}}} - Selecting Dropdown
public static void selectDropdown(WebDriver driver, By locator, String value){
new Select (driver.findElement(locator)).selectByVisibleText(value); } - Selecting searched dropdown
public static void selectSearchDropdown(WebDriver driver, By locator, String value){
driver.findElement(locator).click();
driver.findElement(locator).sendKeys(value);
driver.findElement(locator).sendKeys(Keys.TAB);
} - Uploading file
public static void uploadFile(WebDriver driver, By locator, String path){
driver.findElement(locator).sendKeys(path);
} - Downloading fileHere we will click on a link and will download the file with a predefined name at some specified location.
public static void downloadFile(String href, String fileName) throws Exception{
URL url = null;
URLConnection con = null;
int i;
url = new URL(href);
con = url.openConnection();
// Here we are specifying the location where we really want to save the file.
File file = new File(".//OutputData//" + fileName);
BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(file));
while ((i = bis.read()) != -1) {
bos.write(i);
}
bos.flush();
bis.close(); - Wait()
- Implicit Wait :
driver.manage.timeouts().implicitlyWait(10,TimeUnit.SECONDS);
- Explicit Wait:
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditons.elementToBeClickable(By.id/xpath/name("locator")); - Using Sleep method of java
Thread.sleep(time in milisecond)
- Implicit Wait :
- Navigation method of WebDriver Interface
- to() method (its a alternative of get() method)
driver.navigate().to(Url);
This will open the URL that you have inserted as argument - back() – use to navigate one step back from current position in recent history syntax ==
driver.navigate().back();
- forward() – use to navigate one step forward in browser history
driver.navigate().forward();
- refresh() – This will refresh you current open url
driver.navigate().refresh();
- to() method (its a alternative of get() method)
- Deleting all Cookies before doing any kind of action
driver.manage().deleteAllCookies();
This will delete all cookies - Pressing any Keyboard key using Action builder class of WebDriverWebDriver has rewarded us with one class Action to handle all keyboard and Mouse action. While creating a action builder its constructor takes WebDriver as argument. Here I am taking example of pressing Control key
Actions builder = new Actions(driver);
When we press multiple keys or action together then we need to bind all in a single command by using build() method and perform() method intend us to perform the action.
builder.keyDown(Keys.CONTROL).click(someElement).click(someOtherElement).keyUp(Keys.CONTROL).build().perform();
In the same way you can handle other key actions. - Drag and Drop action in Webdriver
In this we need to specify both WebElement means Source and target and for draganddrop Action class has a method with two argument so let see how it normally look likeWebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
No comments:
Post a Comment