使用Appium尝试触摸操作时出错 [英] Error while trying out touch actions using Appium

查看:125
本文介绍了使用Appium尝试触摸操作时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码:

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运行类时出现以下错误:

On running the class from Eclipse IDE getting below error:

2018年5月12日下午12:12:25 org.openqa.selenium.remote.ProtocolHandshake createSession INFO:检测到的方言:OSS enter code here 失败: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.runSequentially(SuiteRunner.java:450) 在org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) 在org.testng.SuiteRunner.run(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)

May 12, 2018 12:12:25 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSSenter code here FAILED: testSearchAppium java.lang.ClassCastException: io.appium.java_client.AppiumDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen at org.openqa.selenium.interactions.touch.TouchActions.(TouchActions.java:38) at AppiumPackage.AppiumClass.testSearchAppium(AppiumClass.java:77) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) at org.testng.internal.Invoker.invokeMethod(Invoker.java:580) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.privateRun(TestRunner.java:648) at org.testng.TestRunner.run(TestRunner.java:505) at org.testng.SuiteRunner.runTest(SuiteRunner.java:455) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415) at org.testng.SuiteRunner.run(SuiteRunner.java:364) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208) at org.testng.TestNG.runSuitesLocally(TestNG.java:1137) at org.testng.TestNG.runSuites(TestNG.java:1049) at org.testng.TestNG.run(TestNG.java:1017) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

推荐答案

TouchAction类与 Appium JAVA Client jar相关,与硒无关.

TouchAction class related to Appium JAVA Client jar,not related to selenium.

删除import org.openqa.selenium.interactions.touch.TouchActions软件包并导入 Appium JAVA Client jar.

Remove import org.openqa.selenium.interactions.touch.TouchActions package and import Appium JAVA Client jar.

您的代码是这样的吗?

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(); 
}

这篇关于使用Appium尝试触摸操作时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆