jQuery / Javascript with seleniumw webdriver java - java

Is there any examples of Javascript or jQuery been used with Selenium Webdriver Java? I'm having issues with drag and drop using selenium. I'm hoping to use the following code with Javascript/jQuery to perform a drag and drop. I cannot use point and click in my test scripts to perform drag and drop so I'm hoping to use jQuery or java script to perform the drag and drop. I'm having trouble integrating the two. I only want suggestion for drag and drop using javascript or an example of javascript (any) and selenium webdriver code in the same script
public void test(){
driver.findElement(By.id("addService")).click();
driver.findElement(By.id("name")).sendKeys(name);
driver.findElement(By.id("identifier")).sendKeys(id);
driver.findElement(By.id("flowStatus")).clear();
driver.findElement(By.id("flowStatus")).sendKeys(flow);
}
public void dragAndDropElement(WebElement dragFrom, WebElement dragTo) throws Exception {
Actions actions = new Actions(driver);
actions.clickAndHold(dragFrom).release(dragTo).build().perform();;
}
public void test() throws Exception {
WebElement dragFrom = driver.findElement(By.xpath("/html/body/div/div[2]/div[1]/form/fieldset/table[1]/tbody/tr/td[1]/div/div[1]"));
WebElement dragTo = driver.findElement(By.id("drop"));
dragAndDropElement(dragFrom,dragTo);
}

Have tried using Actions class? Here are docs. There are multiple ways you could drag and drop elements using it,
Actions actions = new Actions(driver);
actions.dragAndDrop(source,target).build().perform();
or
actions.clickAndHold(source).release(target).build().perform();
There are other ways as well, check docs and see which one is applicable for you. In general using javascript should be avoided when using WebDriver. WebDriver uses browsers native api's and simulate user interactions which are very close to a real user. Think of this, your user is not going to execute a javascript to do a drag and drop. I would suggest use it only when all other WebDriver doors are closed for you.

Related

Selenium Java Drag and Drop with HTML5

In several threads here, there is a work-around posted for selenium drag and drop with pages that use HTML5 for drag and drop. This work-around involves using javascript to simulate the drag and drop, for example Unable to perform HTML5 drag and drop using javascript for Selenium WebDriver test, and https://gist.github.com/rcorreia/2362544. This solution works well on this page, http://the-internet.herokuapp.com/drag_and_drop.
The general approach is to read the javascript file here (https://gist.github.com/rcorreia/2362544#file-drag_and_drop_helper-js) into a string, referred to as 'jsfile' below.
then in selenium (with java), pass in the css selectors for the source and the destination, where #column-a is the id of the source and #column-b is the target.
((JavascriptExecutor) driver).executeScript(jsfile +"$('#column-a').simulateDragDrop({ dropTarget: '#column-b'});");
It works like a champ on that page.
However, a similar approach does not seem to work on this page, https://crossbrowsertesting.github.io/drag-and-drop.html. Nothing happens when I run
((JavascriptExecutor) driver).executeScript(jsfile +"$('#draggable').simulateDragDrop({ dropTarget: '#droppable'});");
I have pages that seem to behave like this second page (eg no drag and drop). As a first step in understanding this, I'd like to get an idea why this approach does not seem to work in the latter case here.
On re-testing https://crossbrowsertesting.github.io/drag-and-drop.html, it looks like the straight-forward use of the Actions class does the trick for drag and drop. In the particular app that I am testing, which is set up with some additional code to help with accessibility, I was able to get drag and drop happening by setting focus on the first element and hitting the return key, then setting the focus on the target element and hitting return again. I am fairly sure that this is custom event handling, so may not work in other applications. Just in case, I've posted code here which does this in selenium.
public void dndHtml5(String xPathSource, String xPathDestination) {
clickEnterKeyOnElement(xPathSource);
clickEnterKeyOnElement(xPathDestination);
}
public void clickEnterKeyOnElement(String xPath) {
setFocusOnElement(xPath);
WebElement target=element(xPath);
target.sendKeys(Keys.ENTER);
}
public void setFocusOnElement(String xPath) {
WebElement element = element(xPath);
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
}
public WebElement element(String xPath){
return driver.findElementByXPath(xPath);
}

Selenide Drag And Drop to some offset position

Is it possible to perform drag&drop action to some offset position in Selenide? I want to drag element to some part of the page. Unfortunately I cannot find the answer on other pages and documentation lacks it too. I wanted to avoid using selenium tools like "Action" class. It necessarily has to be moved to offsetX and offsetY.
Actions dragAndDrop = new Actions(driver);
dragAndDrop.dragAndDropBy(element, offsetX, offsetY).perform(); -> I want to replace it with some Selenide tool
try to use Selenide.actions() static method.
From Selenide javadoc:
public static org.openqa.selenium.interactions.Actions actions()
With this method you can use Selenium Actions like described in the AdvancedUserInteractions page.
actions()
.sendKeys($(By.name("rememberMe")), "John")
.click($(#rememberMe"))
.click($(byText("Login")))
.build()
.perform();

Selenium Webdriver Java: Unable to perform click operation by Actions in Chrome

I am using Selenium Webdriver(Java) for my Automation. For one of my use-case, I need to click based on co-ordinates. I am using following code to perform this operation:
Actions act = new Actions(driver);
act.moveByOffset(236, 92).click().perform();
Above code is working perfectly in Firefox(Gecko driver). But with Chrome driver, it is not working. Any idea?
Is there any other way to perform this.
I think you can try what Santosh has suggested
act.moveByOffset(236, 92).click().build().perform();
However, this should not make much of a difference as the perform() already contains the build action but this might be a workaround for your problem.
If you can locate the webelement, you can use JavaScript to perform the click this way:
JavaScriptExecutor js = (driver)JavaScriptExecutor;
js.executeScript("arguments[0].click();", element);

mouse hover is not working in safari ,selenium web driver

I used the Actions to mouse hover in FF and chrome , it is working fine. But the same code is not working in safari. I am using mac OS and selenium webdriver and java.
I tried below code.
new Actions(driver).moveToElement(element).build().perform();
JS:
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(mouseOverScript, element);
It is a show stopper issue. any help is appreciated.
Performing mouse hovers requires using WebDriver's Advanced User Interactions API. In the Java language bindings, this is accomplished by the Actions class. Unfortunately, at present, the SafariDriver does not implement the Advanced User Interactions API, so you cannot accomplish this directly. Until it is implemented, you could probably simulate it using JavaScript to fire the events fired my a mouse hover.

How to switch to Telerik Radwindow using webdriver (java)

We are testing an application build using Telerik.
A demo of Telerik is available here: http://demos.telerik.com/aspnet-ajax/window/examples/radwindowobject/defaultcs.aspx
Our application is build in a similar way.
In this demo you can see a window with Bing in it. I want to switch to it using WebDriver (Java) to perform actions on objects within it.
I have tried to switchto iframe but WebDriver comes back saying it is not an iframe.
Also tried to get window handles and switchto window but with no luck, it is not treated as a new window. Any suggestions please?
The following code worked for me. I was able to enter text in the bing search field.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://demos.telerik.com/aspnet-ajax/window/examples/radwindowobject/defaultcs.aspx");
driver.switchTo().frame("RadWindow1");
driver.findElement(By.name("q")).sendKeys("Hello this is my text");
Let me know if this helps you.

Categories