Friday, October 16, 2020

How to handle Drag and Drop in Selenium and Make Re-usable method add in your framework

 Q)How to handle  Drag and Drop in Selenium

package com.seleniumpractice;


import java.util.List;

import java.util.concurrent.TimeUnit;


import org.openqa.selenium.By;

import org.openqa.selenium.NoSuchElementException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.FluentWait;

import org.openqa.selenium.support.ui.Select;

import org.openqa.selenium.support.ui.Wait;

import org.openqa.selenium.support.ui.WebDriverWait;


public class HelperTest {

public static WebDriver driver;

public static String driverpath="D:\\SeleniumJar_08_28_2020\\chromedriver_win32 (4)\\chromedriver.exe\\";

public static String url="file:///D:/Ingenious_TechHub_Teaching/Ingenious_Selenium/Htmldoc/dob.html";

/**

     * This Method is used to launch App

     * 

     */

public static void launchApp(){

System.setProperty("webdriver.chrome.driver", driverpath);

driver=new ChromeDriver();

driver.get(url);

maxmizeBrowser();

}

/**

     * This Method is used to max browser

     * 

     */

public static void maxmizeBrowser(){

driver.manage().window().maximize();

}

/**

     * This Method is used to close borwser

     * 

     */

public static void closeBrowser(){

driver.close();

}

/**

     * This Method is used to compare String

     * @param actual is String Type

     * @param Expected is String Type

     */

public static void compareString(String actual,String expected){

if(actual.equals(expected)){

System.out.println("************ Test Cased is passed ***********");

}

else{

System.out.println("************ Test Cased is Failed ***********");

}

}

public static WebElement explicitlyWait(WebDriver driver,int a,String str){

WebDriverWait wait=new WebDriverWait(driver,a);

return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(str)));

}

public static WebElement fluentWait(WebDriver driver,int a,int b,String strxpath){

Wait<WebDriver> wait=new FluentWait<>(driver)

.withTimeout(a, TimeUnit.SECONDS)

.pollingEvery(b, TimeUnit.SECONDS)

.ignoring(NoSuchElementException.class);

return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(strxpath)));

}

public static WebElement getfirstOptions(WebElement ele){

Select sel=new Select(ele);

WebElement getFirstOption=sel.getFirstSelectedOption();

String strgetFirstOption= getFirstOption.getText();

System.out.println("strgetFirstOption..."+ strgetFirstOption);

return getFirstOption;

}

public static WebElement listofDropdown(WebElement ele){

Select sel=new Select(ele);

List<WebElement> listofDropdown=sel.getOptions();

for(int i=0;i<listofDropdown.size();i++){

String listofDrop=listofDropdown.get(i).getText();

System.out.println("listofDrop..." + listofDrop);

}

return ele;

}

public static WebElement selctByIndexEle(WebElement ele1,int index){

Select selEle=new Select(ele1);

selEle.selectByIndex(index);

return ele1;

}

public static WebElement selectByValueEle(WebElement ele2,String value){

Select selEle=new Select(ele2);

selEle.selectByValue(value);

return ele2;

}

public static WebElement selectByTextEle(WebElement ele3,String text){

Select selEle=new Select(ele3);

selEle.selectByVisibleText(text);

return ele3;

}

public static List<WebElement> selectBosttrapDropDown(List<WebElement> list,String xpath,String selValue){

list=driver.findElements(By.xpath(xpath));

for(int i=0;i<list.size();i++){

String str=list.get(i).getText();

if(str.equalsIgnoreCase(str)){

list.get(i).click();

break;

}

}

return list;

}

public static WebElement movetoElement(WebDriver driver,WebElement ele){

Actions act=new Actions(driver);

act.moveToElement(ele).perform();

return ele;

}

public static void ClickWithActions(WebDriver driver,WebElement ele){

Actions act=new Actions(driver);

act.moveToElement(ele).click().perform();

}


public static void dragAndDrop(WebDriver driver,WebElement drag,WebElement drop){

Actions act=new Actions(driver);

act.dragAndDrop(drag, drop).build().perform();

}


public static void movetoOffSet(WebDriver driver,WebElement ele,int xoffSet,int yoffSet){

Actions act=new Actions(driver);

act.moveToElement(ele, xoffSet,yoffSet).perform();

}


}

====================================================================
package com.seleniumpractice;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class DragAndDropTest  extends HelperTest{

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\driver1016\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://jqueryui.com/resources/demos/droppable/default.html");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
WebElement dragpath=driver.findElement(By.id("draggable"));
WebElement droppath=driver.findElement(By.id("droppable"));
//Actions act=new Actions(driver);
//act.dragAndDrop(dragpath, droppath).build().perform();
HelperTest.dragAndDrop(driver,dragpath, droppath);

}

}
======================================================================
package com.seleniumpractice;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class MoveToEleoffset extends HelperTest {

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver","D:\\driver1016\\chromedriver.exe\\");
    WebDriver driver=new ChromeDriver();
    driver.get("https://demoqa.com/slider/");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    WebElement ele=driver.findElement(By.id("sliderContainer"));
    HelperTest.movetoOffSet(driver, ele,50,10);
   // Actions act=new Actions(driver);
    //act.moveToElement(ele, 50,0).perform();
    ele.click();

    
    
    
    
    
    
    

}

}

No comments:

Post a Comment

Q) How To Find Duplicate Characters In A String In Java?

Step1:- Creating a HashMap containing char as key and it's occurrences as value. Step2:- Converting given string to char array. Step3:- ...