Tuesday, 7 February 2017

Selenium Actions on Web Elements

There are number of actions that can be performed on web element using Selenium Web Driver.

Most of the actions to performed on elements are common. So instead of writing repetitive steps to perform those actions we can create a class having all methods and call them wherever we needed them. We just need to pass two parameters in the method.

ElementLocator: To identify element on which action to be performed
Data: To pass values to element or to pass expected result

  • navigate: Use navigate method to open URL.
While calling navigate method pass URL in data parameter  
If response code of URL is not 200 then it will throw an exception.

public static void navigate(String ElementLocator, String data){
try{
driver.get(data);
URL url = new URL(data);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
int statusCode = http.getResponseCode();
if(statusCode==200)
{
Log.info("Navigating to URL");
DriverScript.Message = "Navigating to URL " + data;
Thread.sleep(3000);
DriverScript.bResult=true;
}
else
{
throw new Exception();
}
}catch(Exception e){
Log.info("Not able to navigate --- " + e.getMessage());
DriverScript.bResult = false;
DriverScript.Message = "Not able to navigate --- " + e.getMessage();
}
}

  • navigateBack: Use navigateBack method to go to previous page
public static void navigateBack(String ElementLocator, String data){
try{
Log.info("Navigating back");
Thread.sleep(2000);
driver.navigate().back();
DriverScript.Message = "Navigating back ";
Thread.sleep(2000);
DriverScript.bResult=true;
}catch(Exception e){
Log.info("Not able to navigate back--- " + e.getMessage());
DriverScript.bResult = false;
DriverScript.Message = "Not able to navigate back--- " + e.getMessage();
}
}

  • Refresh: Use refresh method to reload current page. Keep both parameters empty as they are not required
public static void refresh(String ElementLocator, String data){
try{
Log.info("Refreshing current page");
Thread.sleep(2000);
driver.navigate().refresh();
DriverScript.Message = "Refreshing current page";
Thread.sleep(2000);
DriverScript.bResult=true;
}catch(Exception e){
Log.info("Not able to refresh page--- " + e.getMessage());
DriverScript.bResult = false;
DriverScript.Message = "Not able to refresh page--- " + e.getMessage();
}
}

  • clickByClass: Clicking element by using Class name as Elementlocator

public static void clickByClass(String ElementLocator, String data){
try{
Log.info("Clicking on Web element "+ ElementLocator);
driver.findElement(By.className(ElementLocator)).click();
DriverScript.Message = "Clicking on Web element "+ ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to click --- " + e.getMessage());
DriverScript.Message = "Not able to click --- " + e.getMessage();
DriverScript.bResult = false;
         }
}

  • clickbyXpath: Clicking element by using Xpath as Elementlocator
public static void clickbyXpath(String ElementLocator, String data){
try{
Log.info("Clicking on Web element "+ ElementLocator);
driver.findElement(By.xpath(ElementLocator)).click();
DriverScript.Message = "Clicking on Web element "+ ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to click --- " + e.getMessage());
DriverScript.Message = "Not able to click --- " + e.getMessage();
DriverScript.bResult = false;
         }
}
  • ClickByJSXpath: If any element is not visible on the page and we want to forcefully click on it then we can use ClickByJSXpath

public static void ClickByJSXpath(String ElementLocator, String data){
try{
Log.info("Click on Web element"+ElementLocator);
Thread.sleep(2000);
WebElement element = driver.findElement(By.xpath(ElementLocator));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
DriverScript.Message = "Click on Web element"+ElementLocator;
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to click --- " + e.getMessage());
DriverScript.Message = "Not able to click --- " + e.getMessage();
DriverScript.bResult = false;
         }
}
  • cssSelector: Clicking element by using cssSelector as Elementlocator
public static void clickByCSSSelector(String ElementLocator, String data){
try{
Log.info("Clicking on Web element "+ ElementLocator);
driver.findElement(By.cssSelector(ElementLocator)).click();
DriverScript.Message = "Clicking on Web element "+ ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to click --- " + e.getMessage());
DriverScript.Message = "Not able to click --- " + e.getMessage();
DriverScript.bResult = false;
         }
}

  • clickByPartiallink: Clicking element by using partialLinkText as Elementlocator
public static void clickByPartiallink(String ElementLocator, String data){
try{
Log.info("Clicking on Webelement "+ ElementLocator);
driver.findElement(By.partialLinkText(ElementLocator)).click();
DriverScript.Message = "Clicking on Web element "+ ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to click --- " + e.getMessage());
DriverScript.Message = "Not able to click --- " + e.getMessage();
DriverScript.bResult = false;
         }
}
  • clickByText: Clicking element by Label of element. Here we will pass Label in data parameter.In the ElementLocator parameter we will pass tag name in which element is located for e.g. //a,//button,//input,etc.In the data parameter we will pass the Label of the element
public static void clickByText(String ElementLocator, String data){
try{
Log.info("Clicking on Webelement "+ ElementLocator);
driver.findElement(By.xpath(ElementLocator+"[contains(.,'"+data+"')]")).click();
DriverScript.Message = "Clicking on Web element "+ ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to click --- " + e.getMessage());
DriverScript.Message = "Not able to click --- " + e.getMessage();
DriverScript.bResult = false;
         }
}

  • clickByTextLast: If there are multiple elements with same label and we want to click on last one then we can use clickByTextLast method
public static void clickByTextLast(String ElementLocator, String data){
try{
Log.info("Clicking on Webelement "+ ElementLocator);
driver.findElement(By.xpath(("("+ElementLocator+"[contains(.,'"+data+"')])")+"[last()]")).click();
DriverScript.Message = "Clicking on Web element "+ ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to click --- " + e.getMessage());
DriverScript.Message = "Not able to click --- " + e.getMessage();
DriverScript.bResult = false;
         }
}

  • clickByTextJS: If any element is not visible on the page and we want to forcefully click on it then we can use clickByTextJS
public static void clickByTextJS(String ElementLocator, String data){
try{
Log.info("Clicking on Webelement "+ ElementLocator);
WebElement element = driver.findElement(By.xpath(("("+ElementLocator+"[contains(.,'"+data+"')])")));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
DriverScript.Message = "Clicking on Web element "+ ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to click --- " + e.getMessage());
DriverScript.Message = "Not able to click --- " + e.getMessage();
DriverScript.bResult = false;
         }
}
  • clickByTextJSLast : clickByTextJSLast will click last element not visible on page
public static void clickByTextJSLast(String ElementLocator, String data){
try{
Log.info("Clicking on Webelement "+ ElementLocator);
WebElement element = driver.findElement(By.xpath(("("+ElementLocator+"[contains(.,'"+data+"')])")+"[last()]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
DriverScript.Message = "Clicking on Web element "+ ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to click --- " + e.getMessage());
DriverScript.Message = "Not able to click --- " + e.getMessage();
DriverScript.bResult = false;
         }
}

  • clickMoveMouse: Here we will move mouse to an element and then perform click action on it
public static void clickMoveMouse(String ElementLocator, String data){
try{
Log.info("Moving over element"+ElementLocator);
Thread.sleep(2000);
WebElement searchBtn = driver.findElement(By.xpath(ElementLocator));
Actions action = new Actions(driver);
action.moveToElement(searchBtn).click().perform();
DriverScript.Message = "Moving over element"+ElementLocator;
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to move mouse --- " + e.getMessage());
DriverScript.Message = "Not able to move mouse --- " + e.getMessage();
DriverScript.bResult = false;
         }
}

  • submit: On form we can use submit method instead of performing click event on submit button.It can be used when button type is submit.

public static void submit(String ElementLocator, String data){
try{
Log.info("Clicking on Web element "+ ElementLocator);
driver.findElement(By.xpath(ElementLocator)).submit();
DriverScript.Message = "Clicking on Web element "+ ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to click --- " + e.getMessage());
DriverScript.Message = "Not able to click --- " + e.getMessage();
DriverScript.bResult = false;
         }
}

  • pressEnter: We can use pressEnter method press enter button on keyboard.
public static void pressEnter(String ElementLocator, String data){
try{
Log.info("Pressing enter key on keyboard " + ElementLocator);
try {
Robot robot = new Robot();
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.delay(500);
robot.keyRelease(KeyEvent.VK_ENTER);
} catch (AWTException e) {
       e.printStackTrace();
}
Thread.sleep(3000);
DriverScript.bResult = true;
DriverScript.Message = "Pressing enter key on keyboard " + ElementLocator;
}catch(Exception e){
Log.error("Not able to press enter key--- " + e.getMessage());
DriverScript.Message = "Not able to press enter key--- " + e.getMessage();
DriverScript.bResult = false;
}
}

  • AlertAccept: To accept alert use AlertAccept method
public static void AlertAccept(String ElementLocator, String data){
try{
Log.info("Accepting Alert"+ElementLocator);
WebDriverWait wait = new WebDriverWait(driver, 5);
       wait.until(ExpectedConditions.alertIsPresent());
    Alert alert = driver.switchTo().alert();
       alert.accept();
       DriverScript.Message = "Alert Accepted --- ";
DriverScript.bResult = true;
       
}catch(Exception e){
Log.error("Not able to accept alert --- " + e.getMessage());
DriverScript.Message = "Not able to accept alert --- " + e.getMessage();
DriverScript.bResult = false;
         }
}

  • AlertDismiss: To dismiss alert use AlertDismiss method
public static void AlertDismiss(String ElementLocator, String data){
try{
Log.info("Dismissing alert"+ElementLocator);
Alert alert = driver.switchTo().alert();
alert.dismiss();
DriverScript.Message = "Alert Dismissed --- ";
DriverScript.bResult = true;
}catch(Exception e){
Log.error("Not able to dismiss alert --- " + e.getMessage());
DriverScript.Message = "Not able to dismiss alert --- " + e.getMessage();
DriverScript.bResult = false;
         }
}
  • input: To enter value in text field by xpath use input method
public static void input(String ElementLocator, String data){
try{
Log.info("Entering the text in " + ElementLocator);
driver.findElement(By.xpath(ElementLocator)).sendKeys(data);
DriverScript.Message = "Entering the text " +data +" in the " + ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to Enter UserName --- " + e.getMessage());
DriverScript.Message = "Not able to Enter UserName --- " + e.getMessage();
DriverScript.bResult = false;
}
}

  • inputByCSS: To enter value in text field by cssSelector use input method
public static void inputByCSS(String ElementLocator, String data){
try{
Log.info("Entering the text in " + ElementLocator);
driver.findElement(By.cssSelector(ElementLocator)).sendKeys(data);
DriverScript.Message = "Entering the text in  " +data +" in the " + ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to Enter UserName --- " + e.getMessage());
DriverScript.Message = "Not able to Enter UserName --- " + e.getMessage();
DriverScript.bResult = false;
}
}
  • clear: Clear Text box value using Xpath

public static void clear(String ElementLocator, String data){
try{
Log.info("Clearing text in " + ElementLocator);
driver.findElement(By.xpath(ElementLocator)).clear();
DriverScript.Message = "Clearing text in " + ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to clear text --- " + e.getMessage());
DriverScript.Message = "Not able to clear text --- " + e.getMessage();
DriverScript.bResult = false;
}
}

  • clearByCSS: Clear Text box value using cssSelector

public static void clearByCSS(String ElementLocator, String data){
try{
Log.info("Clearing text in " + ElementLocator);
driver.findElement(By.cssSelector(ElementLocator)).clear();
DriverScript.Message = "Clearing text in " + ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to clear text --- " + e.getMessage());
DriverScript.Message = "Not able to clear text --- " + e.getMessage();
DriverScript.bResult = false;
}
}
  • SelectDDByValue: Selecting Drop Down by using Value. Pass Drop Down value in data.

public static void SelectDDByValue(String ElementLocator, String data){
try{
Thread.sleep(2000);
Log.info("Selecting drop down from the " + ElementLocator);
Select selectByValue = new Select(driver.findElement(By.xpath(ElementLocator)));
selectByValue.selectByValue(data);
DriverScript.Message = "Selecting drop down value "+data+ " from the " + ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to select DD value --- " + e.getMessage());
DriverScript.Message = "Not able to select DD value --- " + e.getMessage();
DriverScript.bResult = false;
}
}

  • SelectDDByValueCSS: Selecting Drop Down by using cssSelector. Pass Drop Down value in data.

public static void SelectDDByValueCSS(String ElementLocator, String data){
try{
Thread.sleep(2000);
Log.info("Selecting drop down from the " + ElementLocator);
Select selectByValue = new Select(driver.findElement(By.cssSelector(ElementLocator)));
selectByValue.selectByValue(data);
DriverScript.Message = "Selecting drop down from the "+data+ " from the " + ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to select DD value --- " + e.getMessage());
DriverScript.Message = "Not able to select DD value --- " + e.getMessage();
DriverScript.bResult = false;
}
}
Selecting Drop Down by using Value.
Pass Visible Text in data paramter.

public static void SelectDDByVisibleText(String ElementLocator, String data){
try{
Thread.sleep(2000);
Log.info("Selecting drop down from the " + ElementLocator);
Select selectByValue = new Select(driver.findElement(By.xpath(ElementLocator)));
selectByValue.selectByVisibleText(data);
DriverScript.Message = "Selecting drop down value "+data+ " from the " + ElementLocator;
Thread.sleep(3000);
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to select DD value --- " + e.getMessage());
DriverScript.Message = "Not able to select DD value --- " + e.getMessage();
DriverScript.bResult = false;
}
}

  • wait: Use wait method to wait for fixed interval of time before next statement is executed.
Pass wait interval as parameter in data field
public static void wait(String ElementLocator, String data) throws Exception{
try{
Log.info("Wait for 5 seconds");
Thread.sleep(data);
DriverScript.Message = "Wait for 5 seconds";
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to Wait --- " + e.getMessage());
DriverScript.Message = "Not able to Wait --- " + e.getMessage();
DriverScript.bResult = false;
         }
}
  • closeBrowser: Use closeBrowser method to stop execution. It will quit driver and no further action can be performed.

public static void closeBrowser(String ElementLocator, String data){
try{
Log.info("Closing the browser");
driver.quit();
DriverScript.Message = "Closing the browser";
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to Close the Browser --- " + e.getMessage());
DriverScript.Message = "Not able to Close the Browser --- " + e.getMessage();
DriverScript.bResult = false;
         }
}
  • MoveMouse: Use Move Mouse method to focus on element.

public static void MoveMouse(String ElementLocator, String data){
try{
Log.info("Moving over element"+ElementLocator);
WebElement el= driver.findElement(By.xpath(ElementLocator));
Actions action = new Actions(driver);
action.moveToElement(el).perform();
DriverScript.Message = "Moving over element"+ElementLocator;
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to move mouse --- " + e.getMessage());
DriverScript.Message = "Not able to move mouse --- " + e.getMessage();
DriverScript.bResult = false;
         }
}

  • VerifyTextPresentByTagName: To verify text is present in Tag. For e.g to verify any text is present on web page, pass body tag as ElementLocator parameter.In data parameter pass expected value for verification.

public static void VerifyTextPresentByTagName(String ElementLocator, String data){
try{
Log.info("Verifying text is present " + ElementLocator);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.tagName(ElementLocator)));   
String bodyText = el.getText();
String bodyText_lower = bodyText.toLowerCase();
String data_lower = data.toLowerCase();
if(bodyText_lower.contains(data_lower))
{
DriverScript.bResult = true;
}
else
{
throw new Exception();
}
DriverScript.Message = "Verifying text "+data+ " is present for " + ElementLocator;
Thread.sleep(3000);
}catch(Exception e){
Log.error("Not able to Verify Text by TagName--- " + e.getMessage());
DriverScript.Message = "Not able to Verify Text by TagName--- " + e.getMessage();
DriverScript.bResult = false;
}
}

  • VerifyTextPresentByXpath: To verify text present on element by Xpath
public static void VerifyTextPresentByXpath(String ElementLocator, String data){
try{
Log.info("Verifying text is present " + ElementLocator);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(ElementLocator)));   
String bodyText =  el.getText();
String bodyText_lower = bodyText.toLowerCase();
String data_lower = data.toLowerCase();
if(bodyText_lower.contains(data_lower))
{
DriverScript.bResult = true;
DriverScript.Message = "Verifying text "+data+ " is present for " + ElementLocator;
}
else
{
throw new Exception();
}
Thread.sleep(3000);
}catch(Exception e){
Log.error("Not able to Verify Text --- " + e.getMessage());
DriverScript.Message = "Not able to Verify Text --- " + e.getMessage();
DriverScript.bResult = false;
}
}

  • VerifyTextPresentByClassName: To verify text present on element by Class Name
public static void VerifyTextPresentByClassName(String ElementLocator, String data){
try{
Log.info("Verifying text is present for " + ElementLocator);
String bodyText = driver.findElement(By.className(ElementLocator)).getText();
String bodyText_lower = bodyText.toLowerCase();
String data_lower = data.toLowerCase();
if(bodyText_lower.contains(data_lower))
{
DriverScript.bResult = true;
DriverScript.Message = "Verifying text "+data+ " is present for " + ElementLocator;
}
else
{
throw new Exception();
}
Thread.sleep(3000);
}catch(Exception e){
Log.error("Not able to Verify Text by ClassName--- " + e.getMessage());
DriverScript.Message = "Not able to Verify Text by ClassName--- " + e.getMessage();
DriverScript.bResult = false;
}
}
  • VerifyTextPresentByCSS: To verify text present on element by cssSelector

public static void VerifyTextPresentByCSS(String ElementLocator, String data){
try{
Log.info("Verifying text is present for " + ElementLocator);
String bodyText = driver.findElement(By.cssSelector(ElementLocator)).getText();
String bodyText_lower = bodyText.toLowerCase();
String data_lower = data.toLowerCase();
if(bodyText_lower.contains(data_lower))
{
DriverScript.bResult = true;
DriverScript.Message = "Verifying text "+data+ " is present for " + ElementLocator;
}
else
{
throw new Exception();
}
Thread.sleep(3000);
}catch(Exception e){
Log.error("Not able to Verify Text by ClassName--- " + e.getMessage());
DriverScript.Message = "Not able to Verify Text by ClassName--- " + e.getMessage();
DriverScript.bResult = false;
}
}
  • VerifyByURL: To verify opened URL by comparing it with expected URL. Pass expected URL in data parameter
public static void VerifyByURL(String ElementLocator, String data){
try{
Log.info("Verifying URL for " + ElementLocator);
String URL = driver.getCurrentUrl();
String URL_lower = URL.toLowerCase();
String data_lower = data.toLowerCase();
if(URL_lower.contains(data_lower))
{
DriverScript.bResult = true;
DriverScript.Message = "Verifying URL "+data+ " is present for " + ElementLocator;
}
else
{
throw new Exception();
}
Thread.sleep(3000);
}catch(Exception e){
Log.error("Not able to Verify by URL--- " + e.getMessage());
DriverScript.Message = "Not able to Verify by URL--- " + e.getMessage();
DriverScript.bResult = false;
}
}
  • VerifyByImage: To verify image is present on page use VerifyByImage method. Here pass expected image name as data parameter.
public static void VerifyByImage(String ElementLocator, String data){
try{
Log.info("Verifying Image is displayed for " + ElementLocator);
WebElement element = driver.findElement(By.xpath(ElementLocator));
String src = ((JavascriptExecutor)driver).executeScript("return arguments[0].attributes['src'].value;", element).toString();
String src_lower = src.toLowerCase();
String data_lower = data.toLowerCase();
if(src_lower.contains(data_lower))
{
DriverScript.bResult = true;
DriverScript.Message = "Verifying Image "+data+ " is present for " + ElementLocator;
}
else
{
throw new Exception();
}
Thread.sleep(3000);
}catch(Exception e){
Log.error("Not able to Verify Image by Xpath--- " + e.getMessage());
DriverScript.Message = "Not able to Verify Image by Xpath--- " + e.getMessage();
DriverScript.bResult = false;
}
}
  • VerifyByImageCSS: Verify image is present by cssSelector

public static void VerifyByImageCSS(String ElementLocator, String data){
try{
Log.info("Verifying Image is displayed for " + ElementLocator);
WebElement element = driver.findElement(By.cssSelector(ElementLocator));
String src = ((JavascriptExecutor)driver).executeScript("return arguments[0].attributes['src'].value;", element).toString();
String src_lower = src.toLowerCase();
String data_lower = data.toLowerCase();
if(src_lower.contains(data_lower))
{
DriverScript.bResult = true;
DriverScript.Message = "Verifying Image "+data+ " is present for " + ElementLocator;
}
else
{
throw new Exception();
}
Thread.sleep(3000);
}catch(Exception e){
Log.error("Not able to Verify Image by CSS--- " + e.getMessage());
DriverScript.Message = "Not able to Verify Image by CSS--- " + e.getMessage();
DriverScript.bResult = false;
}
}
  • VerifyFileDownloaded: To verify any media file is downloaded in specified folder use VerifyFileDownloaded method. Here first of all we will check whether folder is empty or not. Expected file name will be passed as data parameter.

public static void VerifyFileDownloaded(String ElementLocator, String data){
try{
File dir = new File("downloadpath");
System.out.println("downloadpath");
System.out.println(data);
   File[] files = dir.listFiles();
   if (files == null || files.length == 0) {
    DriverScript.bResult = false;
    DriverScript.Message = "Not able to Verify file download--- ";
   }
   for (int i = 1; i < files.length; i++) {
    if(files[i].getName().contains(data)) {
    DriverScript.bResult = true;
DriverScript.Message = "Uploaded Image " + data;
    }
   }
   Thread.sleep(3000);
}catch(Exception e){
Log.error("Not able to Verify file download---  " + e.getMessage());
DriverScript.Message = "Not able to Verify file download--- " + e.getMessage();
DriverScript.bResult = false;
}
}

  • UploadImage: Uploading image using Robot class. Pass file path as data parameter
public static void UploadImage(String ElementLocator, String data){
try{
Log.info("Uploading Image for " + ElementLocator);
StringSelection stringSelection_filepath = new StringSelection(data);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection_filepath, null);
Thread.sleep(500);
System.out.println("File path"+data);
try {
Robot robot = new Robot();
 // Press CTRL+V
 robot.setAutoDelay(100);
 robot.keyPress(KeyEvent.VK_CONTROL);
 robot.keyPress(KeyEvent.VK_V);
 //Release CTRL+V
 robot.keyRelease(KeyEvent.VK_V);
 robot.keyRelease(KeyEvent.VK_CONTROL);
 robot.setAutoDelay(100);
 robot.keyPress(KeyEvent.VK_ENTER);
 robot.keyRelease(KeyEvent.VK_ENTER);

 } catch (AWTException e) {
       e.printStackTrace();
}
Thread.sleep(3000);
DriverScript.bResult = true;
DriverScript.Message = "Uploaded Image " + data;
}catch(Exception e){
Log.error("Not able to upload image--- " + e.getMessage());
DriverScript.Message = "Not able to upload image--- " + e.getMessage();
DriverScript.bResult = false;
}
}
  • ScrollUp: Scroll up in a web page

public static void ScrollUp(String ElementLocator, String data){
try{
Log.info("Scrolling Up " + ElementLocator);
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,-250)", "");
jse.executeScript("window.scrollBy(0,-250)", "");
/*
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_PAGE_UP);
robot.keyPress(KeyEvent.VK_PAGE_UP);
robot.keyPress(KeyEvent.VK_PAGE_UP);
*/
Thread.sleep(3000);
DriverScript.bResult = true;
DriverScript.Message = "Scroll Up " + ElementLocator;
}catch(Exception e){
Log.error("Not able to Scroll up--- " + e.getMessage());
DriverScript.Message = "Not able to Scroll up--- " + e.getMessage();
DriverScript.bResult = false;
}
}
  • ScrollDown: Scroll down in a web page
public static void ScrollDown(String ElementLocator, String data){
try{
Log.info("Scrolling Down " + ElementLocator);
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");
jse.executeScript("window.scrollBy(0,250)", "");
/*
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_PAGE_DOWN);
robot.keyPress(KeyEvent.VK_PAGE_DOWN);
robot.keyPress(KeyEvent.VK_PAGE_DOWN);
*/
Thread.sleep(3000);
DriverScript.bResult = true;
DriverScript.Message = "Scroll Down " + ElementLocator;
}catch(Exception e){
Log.error("Not able to Scroll Down--- " + e.getMessage());
DriverScript.Message = "Not able to Scroll Down--- " + e.getMessage();
DriverScript.bResult = false;
}
}
  • DragAndDrop:To move element from one pace to other use DragAndDrop method. Here pass source element in ElementLocator parameter and destination in data parameter.

public static void DragAndDrop(String ElementLocator, String data){
try{
WebElement elementToMove = driver.findElement(By.xpath(ElementLocator));
   WebElement moveToElement = driver.findElement(By.xpath(data));
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(elementToMove).moveToElement(moveToElement).release(moveToElement).build();
dragAndDrop.perform();
Thread.sleep(3000);
DriverScript.bResult = true;
DriverScript.Message = "Drag and Drop " + ElementLocator;
}catch(Exception e){
Log.error("Not able to Drag and Drop--- " + e.getMessage());
DriverScript.Message = "Not able to Drag and Drop--- " + e.getMessage();
DriverScript.bResult = false;
}
}
  • Slide: To perform sliding action use Slide method. Here element will be dragged by number of pixels as specified in data parameter.
public static void Slide(String ElementLocator, String data){
try{
WebElement dragElementFrom = driver.findElement(By.xpath(ElementLocator));
new Actions(driver).dragAndDropBy(dragElementFrom, data, 0).build().perform();
Thread.sleep(2000);
DriverScript.bResult = true;
DriverScript.Message = "Slide " + ElementLocator;
}catch(Exception e){
Log.error("Not able to Slide--- " + e.getMessage());
DriverScript.Message = "Not able to Slide--- " + e.getMessage();
DriverScript.bResult = false;
}
}

  • SetStyle: Set Stlye of Element as specified in data parameter. For e.g. set data = width: 40%

public static void SetStyle(String ElementLocator, String data){
try{
Log.info("Set Style of Web element"+ElementLocator);
Thread.sleep(2000);
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath(ElementLocator));
js.executeScript("arguments[0].setAttribute('style', data)",element);
DriverScript.Message = "Set Stlye of Web element"+ElementLocator;
DriverScript.bResult=true;
}catch(Exception e){
Log.error("Not able to set stlye --- " + e.getMessage());
DriverScript.Message = "Not able to set style --- " + e.getMessage();
DriverScript.bResult = false;
         }
}

1 comment: