How to Drag and Drop between two different iframes in karate framework - karate

I have a scenario where l have to drag a image from one iframe and drop to other iframe.
I am thinking of following solution
Some switch frame logic then drag
script("var myDragEvent = new Event('dragstart'); myDragEvent.dataTransfer = new DataTransfer()")
waitFor('{}Draggable 1').script("_.dispatchEvent(myDragEvent)")
script("var myDropEvent = new Event('drop');
Some Logic to switch to switch frame to root
Some Logic to switch frame to drop frame
myDropEvent.dataTransfer =
myDragEvent.dataTransfer")
script("// some xpath", "_.dispatchEvent(myDropEvent)")
screenshot()
However this is not working for me

Related

How do I use Selenium Drag and Drop one a webpage to an iframe

I am working on a test, where I am trying to utilize drag and drop to drag the 'Text' from grapejs editor into the Frame, where we enter in our content.
I first tried to move the element from one element to the other, but I noticed that the second element was within an iframe, so I tried to use the x, y coordinates. But of course, the XY coordinates are (0,0) within the frame. So the element was out of range. I also tried the 0,0, but also said it was out of bounds.
var target = Driver.Instance.FindElement(By.XPath("//*[#id='gjs']/div[1]/div[2]/div[5]/div[2]/div/div[1]/div[1]/div[2]/div[5]"));
var builder = new Actions(Driver.Instance);
var action = builder.ClickAndHold(target);
builder.Build();
action.Perform();
var iframe = Driver.Instance.FindElement(By.TagName("iframe"));
Driver.Instance.SwitchTo().Frame(iframe);
builder = new Actions(Driver.Instance);
var destination = Driver.Instance.FindElement(By.XPath("/html/body"));
action = builder.MoveToElement(destination);
builder.Release(destination);
builder.Build();
action.Perform();
Has anyone successfully used Selenium to Drag and Drop across to an iframe lately? All the examples are old...maybe there is a new way to do it. All the examples say use the offset, but putting what I think is the offset for that iframe in the page is not working, nor is switching to the iframe, and setting the co-ordinates within the frame.

How to scroll in mobile browser using Appium and selenium?

How to scroll in mobile browser using Appium and selenium? Scroll is working in app but not in browser. Using scrollToExact method. web app was developed using ionic framework
There several ways how to get it done.
If you are using instance of AppiumDriver, you need to switch to native view before you can use TouchAction
driver.context("NATIVE_APP");
// Get your screen size to set properly start point (startX, startY)
// and end point (endX, endY) for scrolling
Dimension screenSize = driver.manage().window().getSize();
new TouchAction(driver)
.press(<startX>, <startY>)
.waitAction(500)
.press(<endX>, <endY>)
.release()
.perform();
If you are using instance of RemoteWebDriver, then you can do it like this:
driver.get("https://www.google.de");
ExecuteMethod method = new RemoteExecuteMethod(driver);
RemoteTouchScreen screen = new RemoteTouchScreen(method);
screen.up(10, 20);
Thanks to dmle I discovered you need to switch to native view before you can use TouchAction. Anyway that code didn't work as press method doesn't accept two int values.
Here I share the code that worked in my case, and also how to use a web context element as a reference by saving its values before changing to native context. I do also restore the previous context:
//Get web context element references to touch on its area
MobileElement tmpElement = driver.findElement(by);
int x=tmpElement.getLocation().x;
int y=tmpElement.getLocation().y;
Dimension elemSize = tmpElement.getSize();
int height=elemSize.height;
//Save precious context
String previousContext=driver.getContext();
//Set native context
driver.context("NATIVE_APP");
//Perform scroll
new TouchAction(driver)
.press(ElementOption.point(x+5, y+height-5))
.waitAction(WaitOptions.waitOptions(ofSeconds(1)))
.moveTo(ElementOption.point(x+5, y+5))
.release()
.perform();
//Restore context
driver.context(previousContext);

Switching to iframes in Selenium-Java

I am not able to switch between the iframes of a window. I want to select an iframe inside the top window of a webpage.
The link of the page is:
http://way2automation.com/way2auto_jquery/dropdown.php#example-1-tab-1
I can find two iframes but cannot switch to the iframes. Each iframe has their own dropdowns from which I need to select the elements.
I've tried using driver.switchto() but it does not recognizes the iframes.
My code is:
public void SimpleDropDown() throws InterruptedException {
dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// dr.findElement(By.xpath("//a[text()='Select Country']")).click();
// dr.switchTo().frame(dr.findElement(By.xpath("//div[#id='example-1-tab-1']//iframe")));
Select dropdown = new Select(dr.findElement(By.xpath("html/body/select")));
dropdown.selectByVisibleText("India");
System.out.println(dropdown.getFirstSelectedOption().getText());
}
public void comboBox() {
dr.switchTo().frame(2);
dr.findElement(By.xpath("//a[text()='Enter Country']")).click();
Select dropdown = new Select(dr.findElement(By.xpath("//select[#id='combobox']")));
dropdown.selectByVisibleText("Portugal");
Try to execute this code.
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe); //Move inside to the frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
driver.findElement(By.xpath("//a[text()='Enter Country']")).click();
Select dropdown = new Select(driver.findElement(By.xpath("//select[#id='combobox']")));
dropdown.selectByVisibleText("Portugal");
driver.switchTo().defaultContent(); //Move outside to the frame.
Switching to frames with starting index as 0 works but instead of switching to default frames, should use switchTo().parentFrame() as it goes back to the top window (only if you're not working with nested frames).
Your site is showing 2 iFrames in the same page . So before selecting the dropdown you need to switch onto frame as your dropdown is under iframe-
driver.switchTo().frame(0);
Select select = new Select(driver.findElement(By.tagName("select")));
select.selectByValue("Angola");
Once you have made the selection, need to come out the frame using -
driver.switchTo().defaultContent();
and then navigate to the other tab and then again your element is under iframe so need to do the same actions again
driver.findElement(By.xpath("//li/a[text()='Enter Country']")).click();
driver.switchTo().frame(1);
driver.findElement(By.xpath("//span[#class='custom-combobox']/input")).sendKeys("India");
driver.switchTo().defaultContent();

SVG Selenium click do not work

I want to simulate a simple mouse click and drag within an svg element.
I managed to get the coordinates of my starting and ending point, both absolute (window coordinates) and relative to the encapsulating svg element.
Here is the code I am using to simulate the mouse:
Actions builder = new Actions(driver);
builder.moveToElement(area, xStart, yStart);
builder.clickAndHold();
builder.moveToElement(area, xStop, yStop);
builder.release();
Action setFilter = builder.build();
setFilter.perform();
Where area is a WebElement representing my svg and the coordinates are relative to that element. Note that:
area.getLocation(); // returns null
This made me wonder whether the webdriver is able to find that element at all. So I tried with absolute coordinates:
Actions builder = new Actions(driver);
builder.moveByOffset(chart.getLocation().x + xStart, chart.getLocation().y + yStart);
builder.clickAndHold();
builder.moveByOffset(xStop - xStart, yStop - yStart);
builder.release();
Action setFilter = builder.build();
setFilter.perform();
where chart is the div surrounding the svg element (note that the offset between the div position and the svg position is only 10 pixels and is not significant). That didn't work either and I also tried by relative position to the div but still no luck.
What am I doing wrong here?
I got a hack for it to work but it requires the webpage to be opened on the foreground. If you're doing anything else at the same time it may break the test and I cannot say if it would work if ran remotely.
Here's what it looks like:
Robot robert = new Robot();
robert.mouseMove(xStart, yStart);
// full click once to get focus on the window
robert.mousePress(MouseEvent.BUTTON1_MASK);
robert.mouseRelease(MouseEvent.BUTTON1_MASK);
// then set the filter
robert.mousePress(MouseEvent.BUTTON1_MASK);
robert.mouseMove(xStop, yStop);
robert.mouseRelease(MouseEvent.BUTTON1_MASK);

Mozilla PdfJs Operations

given a PDF that is rendered in the browser using pdfjs, are there functions to do the following basic view operations:
rotate
flip
zoom
If not, what are the best strategies I can use to do the operations above?
You can set rotation when you getting viewport form PdfPage object:
var viewport = pdfPage.getViewport(scale, rotation);
If you want to immediately set all the parameters, you can clone viewport, created with scale = 1:
var defaultViewport = pdfPage.getViewport(1);
var neededViewport = defaultViewport.clone({scale: needScale, rotation: needRotation, dontFlip: true});