How to perform drag drop in selenium grid - java

I was installed selenium grid successfully but not able to perform drag drop operation in selenium grid. if i run the code in testng it is working as expected, bu for grid it fail i learning selenium grid so unable to fix this issue,
jar: selenium-server-standalone-2.45.0.jar
Selenium JAR: All jar from "selenium-2.45.0" (latest)
CODE:
public class DragdropElements {WebDriver driver;
String nodeURL;
#BeforeTest
public void draganddrop() throws MalformedURLException
{
// driver = new FirefoxDriver();
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");
nodeURL = "http://localhost:4444/wd/hub";
DesiredCapabilities cap = new DesiredCapabilities();
cap.setBrowserName("firefox");
/// cap.setVersion("31.0");
//cap.setPlatform(Platform.VISTA);
driver = new RemoteWebDriver(new URL(nodeURL),cap);
driver.get("http://only-testing-blog.blogspot.in/2014/09/drag-and-drop.html");
}
#Test
public void DragdropElements1() throws InterruptedException
{
WebElement DragFrom = driver.findElement(By.xpath("//*[#id='dragdiv']"));
WebElement DragTo = driver.findElement(By.xpath("//*[#id='dropdiv']"));
Actions builder = new Actions(driver);
Action dragAndDrop3 = builder.dragAndDrop(DragFrom, DragTo).build();
dragAndDrop3.perform();
String Texttocompare = driver.findElement(By.xpath("//*[#id='Blog1']/div[1]/div/div/div/div[1]/h3")).getText();
System.out.println(""+Texttocompare);
Assert.assertEquals(Texttocompare, "Drag and Drop");
}
}
Exception:
java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebDriver cannot be cast to org.openqa.selenium.interactions.HasInputDevices
at org.openqa.selenium.interactions.Actions.(Actions.java:41)
at qa.DragdropElements.DragdropElements1(DragdropElements.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

Try this and it might work
Actions actionsBuilder = new Actions(driver);
builder.keyDown(Keys.CONTROL)
.click(elementName)
.keyUp(Keys.CONTROL);
Action action = actionsBuilder.build();
action.perform();
Not sure why drag and drop isn't working for you. If you're running multi threads in your nodes try running with just tone thread. Also it's worth mentioning your node OS information.

Related

selenium java NGWebdriver

I have tried everything to login into one site using Selenium webdriver java, but there is one "window", which I don't know how to call it , that I couldn't find one way to click in order to access it. Here, I open the firefox browser and lend on the parfumo.net webpage. The site loads "one window" with cookies settings...
public static void invokeBroser() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
NgWebDriver ngWebDriver = new NgWebDriver(js);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://www.parfumo.net");
driver.manage().window().maximize();
Thread.sleep(3000);
// Initialize and wait till element(link) became clickable - timeout in 60 seconds
WebDriverWait w = (WebDriverWait) new WebDriverWait(driver, Duration.ofSeconds(600));
w.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Accept*')]")));
}
There is a Iframe you have to first switch to it like below
driver.switchTo().frame("iframeId");
OR
driver.switchTo().frame("iframeName");
Once you switch to the frame than perform the click like below.
public static void invokeBroser() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
NgWebDriver ngWebDriver = new NgWebDriver(js);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://www.parfumo.net");
driver.manage().window().maximize();
Thread.sleep(3000);
// Initialize and wait till element(link) became clickable - timeout in 60 seconds
WebDriverWait w = (WebDriverWait) new WebDriverWait(driver, Duration.ofSeconds(600));
driver.switchTo().frame("sp_message_iframe_737779");
w.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Accept*')]")));
}
You can switch back to the main document like below:
driver.switchTo().defaultContent();

Unable to open a link in new tab using Actions Class

I am trying to open a link in a new tab using selenium java automation code.
Initially i tried with actions class but it wasn't working. Later i tried using the Keys to automate the same thru keyboard actions and it worked. But i wanted to know why am i unable achieve the same with Actions class. Just wanted to if i am doing anything wrong here or is Actions class not suitable for this.
Below is the code snippet i have written.
public class InterviewQuestion {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
//System.out.println(System.getProperty("user.dir")+"\\"+"chromedriver.exe");
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\"+"chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in/");
driver.manage().window().maximize();
WebElement GoogleSearchTextBox = driver.findElement(By.xpath("//input[#title='Search']"));
GoogleSearchTextBox.sendKeys("test automation");
GoogleSearchTextBox.sendKeys(Keys.ENTER);
**boolean useActionsClass = false,useKeys = true;**
// finding the required element to be clicked
WebElement RequiredSearchResult = driver.findElement(By.xpath("//div[#class='EIaa9b']/div[1]/div[2]/a"));
if(**useActionsClass**)
{
Actions actions = new Actions(driver);
//actions.moveToElement(RequiredSearchResult).build().perform();
Thread.sleep(5000);
actions.moveToElement(RequiredSearchResult).contextClick(RequiredSearchResult).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
}
if(useKeys)
{
Thread.sleep(5000);
String s = Keys.chord(Keys.CONTROL,Keys.ENTER);
RequiredSearchResult.sendKeys(s);
Set<String> windows = driver.getWindowHandles();
Iterator itr = windows.iterator();
Object FirstWindowHandle = itr.next();
Object SecondWindowHandle = itr.next();
driver.switchTo().window((String) SecondWindowHandle);
Thread.sleep(5000);
//driver.switchTo().window((String) FirstWindowHandle);
}
//driver.quit();
}
}
First, get the link with the help of href attribute from your RequiredSearchResultLink element. once you have the link, use selenium 4 driver.switchTo().newWindow(WindowType.TAB); method to open a new tab. Once the tab is open, just pass your get method, the url which you got from your link field.
If you are not using selenium 4, I will strongly suggest using it, as it will remove lots of unnecessary code from the action class.
Selenium 4 dependency:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0</version>
</dependency>
Code with Selenium 4:
driver.get("https://www.google.co.in/");
driver.manage().window().maximize();
WebElement GoogleSearchTextBox = driver.findElement(By.xpath("//input[#title='Search']"));
GoogleSearchTextBox.sendKeys("test automation");
GoogleSearchTextBox.sendKeys(Keys.ENTER);
WebElement RequiredSearchResultLink = driver.findElement(By.xpath("//div[#class='EIaa9b']/div[1]/div[2]/a"));
String href = RequiredSearchResultLink.getAttribute("href");
driver.switchTo().newWindow(WindowType.TAB);
driver.get(href);
Thread.sleep(10);
driver.quit();
}
3 things you need to
Copy the link you need to open in new tab
Open new tab
Paste your link
driver.switchTo().newWindow(WindowType.TAB);
driver.get("your new lin");

Slider error always Selenium Webdriver

Use of everything done. No output only shows an error. Have tried all these ways but it only shows an error. Totally lost it. Nothing is moving forward from yesterday. Kindly look into it and please let me know.
public class task {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "g://geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.cheapoair.com/");
driver.manage().deleteAllCookies();
driver.findElement(By.xpath(".//*[#id='ember746']")).sendKeys("DFW");
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*[#id='departCalendar_0']")).click();
driver.findElement(By.xpath(".//*
[#id='calendarCompId']/section/div/div[1]/ol/div[26]/li")).click();
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*
[#id='ember730']/section/form/input")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//WebElement target = driver.findElement(By.xpath(".//*
[#id='DivDepart']/div/div/div[1]/div[2]"));
//WebElement source = driver.findElement(By.xpath(".//*
[#id='DivDepart']/div/div/div[1]/div[1]"));
//a.dragAndDrop(source, target).build().perform();
WebElement slider = driver.findElement(By.xpath(".//*
[#id='DivDepart']/div/div/div[1]/div[2]"));
//WebElement slider = driver.findElement(By.id("DivDepart"));
Actions a = new Actions(driver);
//a.dragAndDropBy(slider, 30, 0).build().perform();
//a.clickAndHold(slider).moveByOffset(30,
0).release(slider).build().perform();
//System.out.println("moved");
//JavascriptExecutor js = (JavascriptExecutor)driver;
org.openqa.selenium.interactions.Action dragAndDrop =
a.clickAndHold(slider).moveByOffset(40,0).release().build();
dragAndDrop.perform();
//js.executeScript("window.scrollBy(200,0)");
}
}
Note :-
In your code I can see clearly that, you have not selected depart date and arrival date and you are directly clicking on "Search Now" button. Validation messages are appearing if you directly click on search button without selecting depart and arrival date. So webdriver is not able to find the slider that your are interested in. Code you have written for slider is working fine. Do not change it.
public class task {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "D:/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.cheapoair.com/");
driver.manage().deleteAllCookies();
driver.findElement(By.xpath(".//*[#id='ember746']")).sendKeys("DFW");
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
driver.findElement(By.xpath(".//*[#id='departCalendar_0']")).click();
driver.findElement(By.xpath(".//*[#id='calendarCompId']/section/div/div[1]/ol/div[26]/li")).click();
driver.findElement(By.xpath(".//*[#id='ember751']")).sendKeys("JFK");
driver.findElement(By.xpath(".//*[#id='owFlight']")).click();
//------- Corrections
driver.findElement(By.xpath("//*[#id='departCalendar_0']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id='calendarCompId']/section/div/div[1]/ol/div[27]/li")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id='calendarCompId']/section/div/div[1]/ol/div[31]/li")).click();
// Corrections--------
driver.findElement(By.xpath(".//*[#id='ember730']/section/form/input")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement target = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[2]"));
WebElement source = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[1]"));
WebElement slider = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[2]"));
((JavascriptExecutor) driver).executeScript("scrollBy(0,500);");
Actions a = new Actions(driver);
org.openqa.selenium.interactions.Action dragAndDrop =
a.clickAndHold(slider).moveByOffset(40,0).release().build();
dragAndDrop.perform();
} }
Just Execute this code, I have executed this and working fine and as expected.
I just picked your code and executed once, After correction now it is working very fine. Your Slider code is perfect no need of change in it.
Issue was you had not written script for selecting dates[departure and arrival]
You can also try this code to move Slider :
Thread.sleep(5000);
Actions builder1 = new Actions(driver);
WebElement zero = driver.findElement(By.xpath(".//*[#id='DivDepart']/div/div/div[1]/div[2]"));
builder1.dragAndDropBy(zero, 1000, 0).perform();
xpath of slider square :- //*[#id='slider-range']/span[1]
Please let me know if it is working at your end.
After entering From/To & date details when user clicks 'Search' button. Then moves to next screen. From there I believe you would like to move the slider. Then try following code and you may have to alter wait based on application need.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_PAGE_DOWN);
rob.keyRelease(KeyEvent.VK_PAGE_DOWN);

Unecessary logs in headless testing using phantom js and selenium

I am getting so many unnecessary logs in red color while performing headless testing using phantom js.
How to remove all those red color logs
public class Utility
{
private static WebDriver driver=new PhantomJSDriver();
public static WebDriver getDriver()
{
return driver;
}
}
Here is what you need to do to suppress the INFO logs:
File src = new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", src.getAbsolutePath());
DesiredCapabilities dcap = new DesiredCapabilities();
String[] phantomArgs = new String[] {
"--webdriver-loglevel=NONE"
};
dcap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomArgs);
PhantomJSDriver driver = new PhantomJSDriver(dcap);
driver.get("https://www.facebook.com/");
Let me know if it works for you.

cant select the tab drag n sortabke in t he link http://demoqa.com/draggable/ (none of the tabs there)

I tried using all known element identifier to select the tabs in the drag n drop options in the link but constantly giving error object cant be located .
can anyone help me.
public class DragnDrop {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "D:\\Auto\\geckodriver-v0.11.1-win32\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://demoqa.com");
driver.findElement(By.xpath(".//*[#id='menu-item-140']/a")).click();
Actions builder = new Actions(driver);
WebElement know = driver.findElement(By.xpath(".//*[#id='tabs']/ul"));
builder.moveToElement(know, 10, 10 ).click().build().perform();
i tried to even use action to click at particular location none worked
As far as I can see, if it's the "Draggable + Sortable" tab you're trying to click, the below should allow that:
WebElement draggableTab = driver.findElement(By.id("ui-id-5"));
draggableTab.click();
I've tried the above (using C#) and it worked fine.
public class DragDrop {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver",
System.getProperty("user.dir") + "\\src\\Browser_Driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://demoqa.com");
driver.findElement(By.xpath(".//*[#id='menu-item-140']/a")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(), 'Draggable + Sortable')]")));
driver.findElement(By.xpath("//a[contains(text(), 'Draggable + Sortable')]")).click();
// code works perfectly for demoqa site
Actions builder = new Actions(driver);
List<WebElement> list = driver.findElements(By.cssSelector("#sortablebox li"));
WebElement source = driver.findElement(By.id("draggablebox"));
WebElement dest1 = list.get(1);
WebElement dest2 = list.get(4);
builder.click(source).clickAndHold().moveToElement(dest1).moveByOffset(0, 10).release().build().perform();
Thread.sleep(2000);
builder.click(source).clickAndHold().moveToElement(dest2).moveByOffset(0, 10).release().build().perform();

Categories