User defined functions (UDF) are the functions which are created by users as per their own requirement
If any functionality needs to be repeatedly used such functions can be written in 1 utility file and called multiple times from any other testcases. They are also called generic functions.
Benefits of User Defined Functions
· - Can be used in a number of places without rewriting the code.
· - Code can be made less complex and easier to write.
· - Parameters can be passed to the function.
· - Simpler to invoke.
· - Cleaner and more understandable code
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String currentDir = System.getProperty("user.dir");
FileUtils.copyFile(scrFile, new File(currentDir + "/Screenshots/" + System.currentTimeMillis() + ".png"));
}
Switching to Frame
public void switchToFrame()
{
driver.switchTo().frame("mainpanel");
}
Explicit Wait to Click on any Element
public static void clickOn(WebDriver driver, WebElement element, int timeout)
{
new WebDriverWait(driver, timeout).
until(ExpectedConditions.elementToBeClickable(element));
element.click();
}
Explicit Wait for Sending Data to any Element.
public static void sendKeys(WebDriver driver, WebElement element, int timeout, String value)
{
new WebDriverWait(driver, timeout).
until(ExpectedConditions.visibilityOf(element));
element.sendKeys(value);
}
Highlight the Element using Java Script.
public static void highLightElement(WebDriver driver, WebElement element)
{
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript("arguments[0].setAttribute('style','background: palegreen; border: 8px solid red:')", element);
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
js.executeScript("arguments[0].setAttribute('style','border: solid 2px white')", element);
}
Handle Frames with ID
public void switchToFrame(int frame)
{
try
{
driver.switchTo().frame(frame);
System.out.println("Navigated to Frame with Id " + frame);
}
catch (NoSuchFrameException e)
{
System.out.println("Unable to Locate Frame with Id " + frame + e.getStackTrace());
}
catch (Exception e)
{
System.out.println("Unable to Navigate to Frame with Id " + frame + e.getStackTrace());
}
}
public void switchWindow(WebDriver driver, String firstWindow, String secondWindow)
{
Set<String> windowHandles = driver.getWindowHandles();
for(String windows : windowHandles)
{
if(!windows.equals(firstWindow) && !windows.equals(secondWindow))
{
driver.switchTo().window(windows);
}
}
}
public static void scrollSpecificElement(WebDriver driver,WebElement element)
{
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
}
public static void displayElement(WebElement element)
{
boolean elementDisplayed = element.isDisplayed();
if(elementDisplayed)
{
System.out.println("Element is Displayed");
}
else
{
System.out.println("Element is not Displayed");
}
}
public static void enableElement(WebElement element)
{
boolean elementEnabled = element.isEnabled();
if(elementEnabled)
{
System.out.println("Element is Enabled in Page");
}
else
{
System.out.println("Element is not Enabled in Page");
}
}
Select Value from Dropdown by using SelectByVisibleTest Method
public static void selectValueFromDropDownByText(WebElement element, String value)
{
Select select = new Select(element);
select.selectByVisibleText(value);
}
Select Value from Dropdown by using SelectByIndex Method
public static void selectValueFromDropDownByIndex(WebElement element, int value)
{
Select select = new Select(element);
select.selectByIndex(value);
}
public static void selectValueFromDropDownByValue(WebElement element, String value)
{
Select select = new Select(element);
select.selectByValue(value);
}
Print all the Values from Dropdown and Select a Value from Dropdown
public static void selectDropDownValue(String xpathValue, String value)
{
List<WebElement> monthList = driver.findElements(By.xpath(xpathValue));
System.out.println(monthList.size());
for(int i=0; i<monthList.size(); i++)
{
System.out.println(monthList.get(i).getText());
if(monthList.get(i).getText().equals(value))
{
monthList.get(i).click();
break;
}
}
}
Function to Accept an Alert Pop-Up
public static void acceptAlertPopup() throws InterruptedException
{
try
{
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
Thread.sleep(2000);
alert.accept();
System.out.println("Alert Accepted Successfully");
}
catch(Exception e)
{
System.out.println("Something Went Wrong -- Please Check" +e.getMessage());
}
}
Function to Dismiss an Alert Pop-Up
public static void dismissAlertPopup() throws InterruptedException
{
try
{
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
Thread.sleep(2000);
alert.dismiss();
System.out.println("Alert Dismissed Successfully");
}
catch(Exception e)
{
System.out.println("Something Went Wrong -- Please Check" +e.getMessage());
}
}
Click on Element using Actions Class
public void clickOnElementUsingActions(WebElement element)
{
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
}
Method to Click on Element using JavaScript
public void clickOnElementUsingJs(WebElement element)
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
}
Method to Match Value with List of Elements and Click on it
public void clickOnMatchingValue(List<WebElement> listOfElements, String valueToBeMatched)
{
for(WebElement element : listOfElements)
{
if(element.getText().equalsIgnoreCase(valueToBeMatched))
{
element.click();
return;
}
}
}
Select Calendar Date Or Data Picker Using Java Script Executor
public static void selectDateByJS(WebDriver driver, WebElement element, String dateValue)
{
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("arguments[0].setAttribute('value','"+dateValue+"');", element);
}
Function to Mouse Hover and Click Or Select an Element using Actions Class
public static void moveToElement(WebDriver driver, WebElement element)
{
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
}
Function to Drag and Drop using Actions Class
public static void dragAndDrop(WebDriver driver, WebElement elementOne, WebElement elementTwo)
{
Actions actions = new Actions(driver);
actions.dragAndDrop(elementOne, elementTwo).release().build().perform();
}
Function to Right Click using Actions Class
public static void rightClick(WebDriver driver, WebElement element)
{
Actions actions = new Actions(driver);
actions.contextClick(element).build().perform();
}
Function to Double Click using Actions Class
public static void doubleClick(WebDriver driver, WebElement element)
{
Actions actions = new Actions(driver);
actions.doubleClick(element).build().perform();
}
Click on Element Using Java Script
public static void clickElementByJS(WebElement element, WebDriver driver)
{
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("arguments[0].click();", element);
}
Refresh Browser Using Java Script
public static void refreshBrowserByJS(WebDriver driver)
{
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("history.go(0)");
}
Get Title Using Java Script.
public static String getTitleByJS(WebDriver driver)
{
JavascriptExecutor js = ((JavascriptExecutor) driver);
String title = js.executeScript("return document.title;").toString();
return title;
}
Scroll Down the Page Using Java Script
public static void scrollPageDown(WebDriver driver)
{
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
}
Extent Report - 1
public static String getSystemDate()
{
DateFormat dateFormat = new SimpleDateFormat("_ddMMyyyy_HHmmss");
Date date = new Date();
return dateFormat.format(date);
}
Extent Report - 2
public static String getScreenshot(WebDriver driver, String screenshotName) throws IOException
{
//We have generated Date now.
String dateName = new SimpleDateFormat("_ddMMyyyy_HHmmss").format(new Date());
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);
//After execution, you could see a folder "FailedTestsScreenshots"
//Under Source folder
String destination = System.getProperty("user.dir") + "/FailedTestsScreenshots/" + screenshotName + dateName + ".png";
File finalDestination = new File(destination);
FileUtils.copyFile(source, finalDestination);
return destination;
7 Comments
I was just examining through the web looking for certain information and ran over your blog.It shows how well you understand this subject. Bookmarked this page, will return for extra.
ReplyDelete360DigiTMG
I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. smart touch switches
ReplyDeleteI definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. Sentiment Analysis
ReplyDeleteNice work... Much obliged for sharing this stunning and educative blog entry!
ReplyDeletehrdf training course
I will truly value the essayist's decision for picking this magnificent article fitting to my matter.Here is profound depiction about the article matter which helped me more.
ReplyDeletedata science course in noida
Wow, amazing post! Really engaging, thank you.
ReplyDeleteBest Artificial Intelligence Course in pune
Thank you for sharing excellent information. Your site is so cool. I’m impressed by the details that you’ve on this website. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for more articles. You, my friend, ROCK! I found simply the info I already searched all over the place and just could not come across. What a perfect website
ReplyDeleteData Science Training in Hyderabad
Data Science Course in Hyderabad