如何在 Java 中使用 appium 为 android 原生应用程序执行滑动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23339742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to perform swipe using appium in Java for android native app
提问by user3540555
I need to swipe my app(Both from left to right and right to left), wherelse I am using Java in appium for android native app automation.
我需要刷我的应用程序(从左到右和从右到左),我在 appium 中使用 Java 来实现 android 本机应用程序自动化。
I have tries this link, Swipe method not working in android automation testing
我已经尝试过这个链接, Swipe 方法在 android 自动化测试中不起作用
But i can't, is any other link please share or anyone help me out.
但我不能,请分享任何其他链接或任何人帮助我。
回答by ABDUL SATHAR BEIGH
Here is how we do it-
这是我们如何做到的 -
Swipe left-
向左滑动——
appiumDriver.context("NATIVE_APP");
Dimension size = appiumDriver.manage().window().getSize();
int startx = (int) (size.width * 0.8);
int endx = (int) (size.width * 0.20);
int starty = size.height / 2;
appiumDriver.swipe(startx, starty, endx, starty, 1000);
Swipe right-
向右滑动——
appiumDriver.context("NATIVE_APP");
Dimension size = appiumDriver.manage().window().getSize();
int endx = (int) (size.width * 0.8);
int startx = (int) (size.width * 0.20);
int starty = size.height / 2;
appiumDriver.swipe(startx, starty, endx, starty, 1000);
Here appiumDriver is an instance of io.appium.java_client.AppiumDriver
这里的 appiumDriver 是 io.appium.java_client.AppiumDriver 的一个实例
回答by Pinak Gauswami
swipe() method is deprecated.
不推荐使用 swipe() 方法。
so we can use below methods.
所以我们可以使用以下方法。
Swipe left will be perform as:
向左滑动将执行为:
new TouchAction(driver).longPress(250, 1200).moveTo(900, 1200).release().perform();
Swipe right will be perform as:
向右滑动将执行为:
new TouchAction(driver).longPress(1000, 450).moveTo(500, 450).release().perform();
Here, driver is your driver like AndroidDriver
, RemoteWebDriver
, AppiumDriver
.
在这里, driver 是您的驱动程序,例如AndroidDriver
, RemoteWebDriver
, AppiumDriver
。
And 250, 1200 and other is Your app view Co-ordinate that you can see in the UIAutomaterView batch file located under the android-sdk platform-tools.
而 250、1200 和其他是你的应用视图坐标,你可以在位于 android-sdk 平台工具下的 UIAutomaterView 批处理文件中看到。
回答by anuja jain
public void leftRightSwipe(int timeduration) {
// duration should be in milliseconds
size = driver.manage().window().getSize();
System.out.println(size);
startx = (int) (size.width * 0.70);
endx = (int) (size.width * 0.30);
starty = size.height / 2;
System.out.println("Start swipe operation");
driver.swipe(endx, starty, startx, starty, timeduration);
}
public void rightLeftSwipe(int timeduration) {
size = driver.manage().window().getSize();
System.out.println(size);
startx = (int) (size.width * 0.70);
endx = (int) (size.width * 0.30);
starty = size.height / 2;
System.out.println("Start swipe operation");
driver.swipe(startx, starty, endx, starty, timeduration);
}
In case you want to learn in details Refer here - http://www.qaautomated.com/2017/10/swipe-rightleftup-down-using-appium.html
如果您想详细了解请参阅此处 - http://www.qaautomated.com/2017/10/swipe-rightleftup-down-using-appium.html
回答by user3540555
Use this below code for Swipe using appium in Junit for android native app,
使用下面的代码在 Junit 中使用 appium for android native app 进行滑动,
public void swipe()
{
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, Double> swipeObject = new HashMap<String, Double>();
swipeObject.put("startX", 0.95);
swipeObject.put("startY", 0.5);
swipeObject.put("endX", 0.05);
swipeObject.put("endY", 0.5);
swipeObject.put("duration", 1.8);
js.executeScript("mobile: swipe", swipeObject);
}
//Call this method where you need to swipe,
//在需要滑动的地方调用这个方法,
swipe(); //it will call the swipe method
I have tried this out and got swiped successfully in android native app.
我已经尝试过了,并在 android 本机应用程序中成功刷卡。
For more this might helpful:
有关更多信息,这可能会有所帮助:
https://github.com/appium/appium/blob/master/docs/en/gestures.md
https://github.com/appium/appium/blob/master/docs/en/gestures.md
回答by Suman
public void swipeUpElement(AppiumDriver<WebElement> driver, WebElement element, int duration){
int bottomY = element.getLocation().getY()-200;
driver.swipe(element.getLocation().getX(), element.getLocation().getY(), element.getLocation().getX(), bottomY, duration);
}
回答by arunkumar sambu
public void swipingHorizontally() throws MalformedURLException, InterruptedException{
DesiredCapabilities();
Dimension size = driver.manage().window().getSize();
System.out.println(size);
int startx = (int) (size.width * 0.70);
int endx = (int) (size.width * 0.30);
int starty = size.height / 2;
driver.swipe(startx, starty, endx, starty, 2000); // it swipes from right to left
driver.swipe(endx, starty, startx, starty, 2000); // it swiptes from left to right
}
Thread.sleep(2000);
}
回答by Rohan
@ABDUL SATHAR BEIGH : Absolutely right . Just to add , for me to work on Android I had to typecast it by (AndroidDriver) driver) if the above doesn't work for you . The following worked with this modification. this is swipe right.
@ABDUL SATHAR BEIGH:绝对正确。只是补充一下,如果我在 Android 上工作,我必须通过 (AndroidDriver) 驱动程序对它进行类型转换,如果上述方法对您不起作用。以下内容适用于此修改。这是向右滑动。
((AndroidDriver) driver).context("NATIVE_APP");
Dimension size = driver.manage().window().getSize();
int endx = (int) (size.width * 0.8);
int startx = (int) (size.width * 0.20);
int starty = size.height / 2;
((AndroidDriver) driver).swipe(startx, starty, endx, starty, 1000);