Selenium 使用Appium尝试触摸操作时出错
下面是我的代码:Selenium 使用Appium尝试触摸操作时出错,selenium,appium,appium-android,Selenium,Appium,Appium Android,下面是我的代码: package AppiumPackage; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.touch.TouchActions; import org.openqa.selenium.remote.Augmenter; impo
package AppiumPackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileBrowserType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
public class AppiumClass {
AppiumDriver driver;
WebDriverWait wait;
String AppURL = "https://www.ndtv.com/";
@BeforeTest
public void setup() throws MalformedURLException {
// Create an object for Desired Capabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("chromedriverExecutable", "C:/Users/Madankumar/Desktop/Chrome Driver/chromedriver.exe");
// Name of mobile web browser to automate. ‘Safari’ for iOS and ‘Chrome’
// or ‘Browser’ for Android
//capabilities.setCapability("browsername", "Chrome");
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,MobileBrowserType.CHROME);
// The kind of mobile device or emulator to use - iPad Simulator, iPhone
// Retina 4-inch, Android Emulator, Galaxy S4 etc
capabilities.setCapability("deviceName", "919d8316");
// Which mobile OS platform to use - iOS, Android, or FirefoxOS
capabilities.setCapability("platformName", "Android");
// Java package of the Android app you want to run- Ex:
// com.example.android.myApp
capabilities.setCapability("appPackage", "com.android.chrome");
// Activity name for the Android activity you want to launch from your
// package
capabilities.setCapability("appActivity", "com.google.android.apps.chrome.Main");
capabilities.setCapability("autoAcceptAlerts", false);
// Initialize the driver object with the URL to Appium Server and
// passing the capabilities
driver = new AppiumDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
wait = new WebDriverWait(driver, 5);
}
@Test
public void testSearchAppium() throws Throwable {
driver.get(AppURL);
Thread.sleep(2000);
driver.findElement(By.xpath("//a[@class='notnow']")).click();
Thread.sleep(2000);
TouchActions actions=new TouchActions(driver);
actions.longPress(driver.findElement(By.xpath("//a[@class='scroll__nav-link active-nav']"))).perform();
Thread.sleep(3000);
actions.flick(20, 0);
}
/*
@AfterTest
public void tearDown() {
driver.quit();
}
*/
}
从Eclipse IDE运行类时出现以下错误:
2018年5月12日12:12:25 PM org.openqa.selenium.remote.ProtocolHandshake createSession
信息:检测到的方言:OSS在此处输入代码
失败:testSearchAppium
java.lang.ClassCastException:io.appium.java_client.AppiumDriver无法强制转换为org.openqa.selenium.interactions.HasTouchScreen
位于org.openqa.selenium.interactions.touch.TouchActions.(TouchActions.java:38)
位于AppiumPackage.AppiumClass.testSearchAppium(AppiumClass.java:77)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
位于org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
位于org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
位于org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
位于org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
位于org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
位于org.testng.TestRunner.privateRun(TestRunner.java:648)
位于org.testng.TestRunner.run(TestRunner.java:505)
位于org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
在org.testng.SuiteRunner.runsequential上(SuiteRunner.java:450)
位于org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
运行(SuiteRunner.java:364)
位于org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
位于org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
在org.testng.testng.runSuitesSequentially上(testng.java:1208)
位于org.testng.testng.runSuitesLocally(testng.java:1137)
位于org.testng.testng.runSuites(testng.java:1049)
位于org.testng.testng.run(testng.java:1017)
位于org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
位于org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
位于org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
TouchAction
类与Appium JAVA客户端相关,与selenium无关
移除导入org.openqa.selenium.interactions.touch.TouchActions包并导入Appium JAVA Clientjar
请按照以下方式执行代码:
public void testSearchAppium() throws Throwable
{
driver.get(AppURL);
Thread.sleep(2000);
MobileElement notNow=driver.findElement(By.xpath("//a[@class='notnow']"));
TouchAction actions=new TouchAction((PerformsTouchActions) driver);
actions.tap(notNow).perform();
}
此问题的工作解决方案位于以下链接中 我们又在这里写解决方案了 使用
io.appium.java\u client.TouchAction
class
第一步
这里的驱动程序是AppiumDriver的一个实例
步骤2
使用TouchAction的新实现,您无法直接传递WebElement
使用的依赖项
木卫一
java客户端
7.0.0
希望有帮助。添加了此导入io.appium.java\u client.PerformTouchActions;导入io.appium.java_client.TouchAction;将代码修改为public void testSearchAppium()抛出可丢弃的{driver.get(AppURL);Thread.sleep(2000);WebElement notNow=driver.findElement(By.xpath(//a[@class='notNow']);//notNow.click();TouchAction actions=new TouchAction((performstuchactions)driver);actions.tap(notNow);错误为“失败:testSearchAppium java.lang.ClassCastException:org.openqa.selenium.remote.RemoteWebDriver无法强制转换为io.appium.java_client.PerformsTouchActions”得到相同的错误,但我使用的是Eventfiringwebdriver,因此我的代码如下所示新的TouchAction((PerformsTouchActions)(AppiumDriver)驱动程序),但仍然无法将eventfiringwebdriver转换为appiumdriver…对此有何想法?
TouchAction action = new TouchAction(driver);
WebElement ele = driver.findElement(By.id("locator"));
action.tap(new TapOptions().withElement(new ElementOption().withElement(ele))).perform();
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.0.0</version>
</dependency>