我的Appium Java代码:
package Generic;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.MobileDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
public class AppiumSelenium {
//AndroidDriver Ad=new AndroidDriver();
public static void main(String arg[]) throws MalformedURLException, InterruptedException {
DesiredCapabilities dc= new DesiredCapabilities();
dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1.1");
dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
dc.setCapability(MobileCapabilityType.APP, "/home/connexis/appium/AppiumMobile/src/main/java/Apps/ApiDemos.apk");
URL url =new URL("http://127.0.0.1:4723/wd/hub");
AndroidDriver<WebElement> driver= new AndroidDriver<WebElement>(url,dc);
driver.findElementsById("android:id/text1").get(1).click();
WebElement info=driver.findElementsById("android:id/text1").get(1);
int x1=info.getLocation().getX();
int y1=info.getLocation().getY();
//we need end coornidates a.longPress(x).moveTo(startX, endY).release().perform();too hence , lets use some formulat to geterate
int x2=x1 /2;
int y2=y1 /2;
TouchAction a = new TouchAction((MobileDriver) driver);
-------------------------------------------------------
a.longPress(info).moveTo(x1, y2).perform().release();
/*
List<WebElement> val=driver.findElementsById("android:id/text1");
//it should retrive all the objects means 30 or 40 whatever present in that app
for(int i=0;i<val.size();i++){
System.out.println(val.get(i).getText());
}
*/
Thread.sleep(9000);
driver.quit();
}
}
环境设置:
Selenium server standalone 2.53
> java-client 5.0.4
> java-client 6.1.0
错误日志
2018年10月4日下午3:07:53 io.appium.java_client.remote.AppiumCommandExecutor $ 1 lambda $ 0信息: 检测到的方言:线程“ main”中的W3C异常 java.lang.NoSuchMethodError: io.appium.java_client.TouchAction.longPress(Lorg / openqa / selenium / WebElement;)Lio / appium / java_client / TouchAction; 在Generic.AppiumSelenium.main(AppiumSelenium.java:52)
答案 0 :(得分:2)
如果您查看发行版https://github.com/appium/java-client/releases/tag/v6.0.0-BETA1,则TouchActions
的用法会发生变化,它们会接受io.appium.java_client.touch.ActionOptions
并添加了子类别
请参考TouchActions
类here的Appium单元测试
答案 1 :(得分:0)
在您的代码中,您无法在longpress()中传递webelement。 LongPress()将点选项作为其参数。修改您的代码,如下所示:
new TouchAction(driver).longPress(PointOption.point(x1, y1)).moveTo(PointOption.point(x2, y2)).perform().release();
如果要垂直滚动,可以按照以下步骤进行操作:
public static void swipe(int startX, int endX, int startY, int endY) {
try {
new TouchAction(Init.driver).press(PointOption.point(startX, startY)).waitAction(WaitOptions.waitOptions(ofSeconds(1)))
.moveTo(PointOption.point(endX, endY)).release().perform();
} catch (Exception e) {
System.out.println("unable to swipe");
}
}