Trying to automate android app using appium, when entered the below code for swipe giving ----
TouchAction io.appium.java_client.TouchAction.press(WebElement el)
#Deprecated
TouchAction ac = new TouchAction(driver);
ac.press(436,652).moveTo(-311,-14).release().perform();
What can be used to swipe?
The use of coordinates or wait times has been deprecated. You're now supposed to use ActionOptions. In the case of points, they're called PointOptions.
TouchAction works for me when giving it elements to start and stop the swiping at:
WebElement start = driver.findElement(By.id("xxxxxx"));
WebElement stop = driver.findElement(By.xpath("xxxxxx"));
TouchAction action = new TouchAction(driver);
action.longPress(start).moveTo(stop).release().perform();
The waitAction in the following code is crucial to properly implementing a swipe (that took me a couple hours of investigation to learn).
public static void actionSwipeLeft(AndroidDriver driver)
{
Dimension dims = driver.manage().window().getSize();
int x = (int) (dims.getWidth() * .5);
int y = (int) (dims.getHeight() * .8);
int endX = 0;
System.out.println("Swiping left.");
new TouchAction(driver)
.press(new PointOption().withCoordinates(x, y))
.waitAction(new WaitOptions().withDuration(Duration.ofMillis(500)))
.moveTo(new PointOption().withCoordinates(endX, y))
.release()
.perform();
}
I think one of commentors, Mike, might be right about wait times being deprecated. (The sources are all discombobulated.) This is the code that I use. Hope it helps.
new TouchAction(driver).longPress(PointOption.point(x, y)).moveTo(PointOption.point(x, y)).release().perform();
Related
How can I click on Text link inside in textview in appium
for ex. i have a string and Don't Have an Account? Register
Only Register has a link other text are disable, when i click on Register it navigate to Register screen.
But I can't click on Register.
Refer a image
WebElement element = driver.findElement(By.id("element id here"));
Point point = element.getLocation();
//you can change follwing point based on your link location
int x = point.x +1;
int y = point.y + element.getSize().getHeight() - 1;
new TouchAction(driver).tap(x, y).perform();
I find this solution here in appium discussion
I usually try to avoid XPATH in my tests, hence to search for Elements by Text I am doing something like this, which will return a By element to be used in the webdriver interaction.
public static By byText(final String text)
{
switch (Configuration.getInstance().getPlatform())
{
case ANDROID:
// This requires a UiAutomator2 test driver:
return MobileBy.AndroidUIAutomator("new UiSelector().text(\"" + text + "\")");
case IOS:
default:
throw new RuntimeException("not implemented");
}
}
Unfortunately I don't know the equivalent to iOS just now, but it should also be possible somehow ;)
Use this simple code
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
In a sidebar menu I want to move to a menu item. I'm using this code:
#FindBy(xpath = ".//*[#id='sidebar-wrapper']/ul//a[contains(text(), 'Contact')]")
WebElement contactLink;
public void clickHamburgerMenuAndContactLink() {
Actions action = new Actions(driver);
action.click(hamburgerMenu).moveToElement(contactLink, 0, 0).click().perform();
}
This works most of the time although it also fails from time to time (behavior seems to be unstable).
First I tried to move without mentioning the x and y Offset using only the webelement contactlink as an argument in the moveToElement method. This don't work at all, I don't understand why. I tried also with build() before the perform() but this makes no difference.
The unstable behaviour is probably due to the animation effect which makes the contact link unsteady. I would wait for the close button since it is displayed once the menu reaches its final position:
public void clickHamburgerMenuAndContactLink() {
WebDriverWait wait = new WebDriverWait(driver, 10000);
hamburgerMenu.click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("menu-close")));
contactLink.click();
}
I need to copy the contents of a page except header and footer only using mouse select+drag+mouseup+ctrl+C kind of copy.
I am using Selenium Actions class.
FirefoxDriver driver = new FirefoxDriver();
Actions builder = new Actions(driver);
driver.get("http://connectatgrace.org/im-new/what-to-expect");
builder.click(driver.findElement(By.className("block-holder")))
.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT)
.click(driver.findElement(By.id("footer")));
System.out.println("CLICKED");
Is it possible?
Experts please help.
If all you want is the text, you can get it by using .getText() on the right element.
WebDriver driver = new FirefoxDriver();
driver.get("http://connectatgrace.org/im-new/what-to-expect");
String text = driver.findElement(By.id("main")).getText().trim();
EDIT
I understand now what you are looking for. I was able to get it to work with the code below.
Basically the code moves the mouse to the bottom-right corner of the element that contains all the desired text, <div id="content full-width">. Click and hold the mouse button down, move to the top-left corner of the element, and then CTRL+C. I've also validated that the correct text gets into the clipboard.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://connectatgrace.org/im-new/what-to-expect");
WebElement content = driver.findElement(By.id("content full-width"));
Actions builder = new Actions(driver);
builder.moveToElement(content, 0, 0).clickAndHold().moveToElement(content,
content.getSize().getWidth(), content1.getSize().getHeight()).release();
builder.build().perform();
content.sendKeys(Keys.chord(Keys.CONTROL, "c"));
System.out.println("CLICKED");
NOTE: If you use moveToElement(), it moves to the center of the element. That's why there's code to get the width/height and divide by 2.
EDIT 2
After OP noticed that I had a typo, I went back and did some investigations on the .moveToElement() methods. From the docs,
moveToElement(WebElement toElement)
Moves the mouse to the middle of the element.
moveToElement(WebElement toElement, int xOffset, int yOffset)
Moves the mouse to an offset from the top-left corner of the element.
So corrected the code to use .moveToElement(e, 0, 0) to start the selection at the top left of the element and then moved the selection to the bottom right using .moveToElement(content, content.getSize().getWidth(), content1.getSize().getHeight()). This should work correctly now.
This works!!!
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://connectatgrace.org/im-new/what-to-expect");
WebElement content = driver.findElement(By.id("content full-width"));
Actions builder = new Actions(driver);
builder.moveToElement(content, content.getSize().getWidth() / 2, content.getSize().getWidth() / 2).clickAndHold().moveToElement(content, -content.getSize().getHeight() / 2, -content.getSize().getHeight() / 2);
builder.build().perform();
Basically you forgot to place height i.e. getHeight() instead of getWidth().
Now its working fine.
I've quickly programmed a java program that randomly moves the cursor to prank my mate. Everything works perfectly except that when Task Manager is the current active window, the cursor doesn't move. It works fine when other applications are active. My questions are: What is causing this and if possible, how do i fix it. Here's my code(i know it's messy.):
Robot rob = new Robot();
while (true) {
PointerInfo pointer = MouseInfo.getPointerInfo();
Point point = pointer.getLocation();
int x = (int) point.getX();
int y = (int) point.getY();
int xd = getRandomDirection();
int yd = getRandomDirection();
rob.mouseMove(x+xd, y+yd);
}
private static int getRandomDirection() {
Random ran = new Random();
float ran1 = ran.nextFloat();
if(ran1 > 0.5){
return 1;
}else{
return -1;
}
}
I found the answer. Simply it is a rule in the newer versions of windows that a program that is not "run as admin" can't control over an admin-run program, e.g task manager. The simple solution was enabling the little checkbox on the program in windows with "rightclick" > "properties" > "Compatibility" > "Run as admin" or simply manually running as admin every time with a simple rightclick.
I have a problem using Selenium Webdriver (version 2.32.0) and Firefox (21.0), trying to change the values on a slider.
I wrote a Java code like this:
private void selectGiftCardPrice() throws TestingException {
try {
WebElement slider = getDriver().findElement(
By.cssSelector("div.sliderHandle"));
Actions move = new Actions(getDriver());
move.dragAndDropBy(slider, 90, 0);
move.build().perform();
sleep(4000);
} catch (Exception e) {
log.info(e);
throw new TestingException("e");
}
I tried out every code I found on the Web, every change, and it still does not work. It does not show any problem, just finds the element, and does nothing. Any idea what it is, or what can I do?
EDIT from comment:
I finally made it working with jQuery slider demo
driver.get("http://jqueryui.com/resources/demos/slider/multiple-vertical.html");
WebElement slider = driver.findElement(By.xpath("//div[1]/a[contains(#class,'ui-slider-handle')]"));
But it is still not working for me with jQuery UI Slider demo page using Xpath //div[#id='slider']/a. What is the problem?
This code works absolutely fine for me.
program handles slider of website : Homeshope18.com
Check it out:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.homeshop18.com/fashion-jewellery/category:15143/filter_Theme:%28%22Traditional+Wear%22+%22Cuff+%26+Kada%22+%22Daily+Wear%22+%22Maang+Tikka%22+%22Openable+Round%22+%22Round%22+%22Openable+Oval%22%29/sort:Popularity/inStock:true/?it_category=HP&it_action=JW-HPSP01&it_label=HP-HPSP01-131021235900-PD-JW-ZC-VK-SC_DiwaliFestWeddingJewellery&it_value=0");
WebElement slider = driver.findElement(By.xpath("//*[#id='slider-range']/a[1]"));
Thread.sleep(3000);
Actions moveSlider = new Actions(driver);
Action action = moveSlider.dragAndDropBy(slider, 30, 0).build();
action.perform();
Using Actions class, firs use clickAndHold("WebElemnt");
Then to move horizontally we need to move in the Y direction of the screen so we can use movebyoffset, i.e X-axis: 0 & Y axis: 40px
To move vertically we need to move in the X direction of the screen so we can use movebyoffset, i.e X-axis: 40px & Y axis: 0
The sample code would be :
Actions slider=new Actions(driver);
slider.clickAndHold("WebElemnt");
slider.movebyoffset(0,40).build.perform();