【问题标题】:Unable to perform in touch action in webview while running test from Mac with Appium使用 Appium 从 Mac 运行测试时无法在 webview 中执行触摸操作
【发布时间】:2014-09-30 19:25:44
【问题描述】:

我被以下问题严重困扰,请帮助我找到一个可行的解决方案。


对于我得到的所有触摸动作

org.openqa.selenium.WebDriverException:未实现的命令:session/537d48a9dcdfb38a12ff318a302c9a08/touch/scroll 命令持续时间或超时:8 毫秒 构建信息:版本:'2.42.2',修订:'6a6995d31c7c56c340d6f45a76976d43506cd6cc',时间:'2014-06-03 10:52:47' 系统信息:主机:'Praveen-Prabhus-MacBook-Pro.local',ip:'192.168.0.42',os.name:'Mac OS X',os.arch:'x86_64',os.version:'10.9。 4',java.version:'1.7.0_65' 会话 ID:bbe122fa-f325-4142-a555-9d2f4ea60e02 驱动信息:core.AppiumSwipeableDriver


public class AppiumSwipeableDriver extends AppiumDriver implements HasTouchScreen{ 
 public RemoteTouchScreen touch; 
 public AppiumSwipeableDriver(URL URL, Capabilities Cap) { 
 super(URL, Cap); 
 touch = new RemoteTouchScreen(getExecuteMethod()); 
} 

 @Override 
 public TouchScreen getTouch() { 
 return touch; 
 }
}

if(browser.equalsIgnoreCase("android")){
 DesiredCapabilities capabilities = new DesiredCapabilities();
 capabilities.setCapability(CapabilityType.BROWSER_NAME,"");
 capabilities.setCapability("deviceName","Android");
 capabilities.setCapability("device","Android");
 capabilities.setCapability("takesScreenshot","true");
 capabilities.setCapability("platformName","Android");
 capabilities.setCapability("platformVersion","4.4.2");
 capabilities.setCapability(CapabilityType.PLATFORM,"Mac");
 capabilities.setCapability("appPackage","uk.co.ee.myee");
 capabilities.setCapability("appActivity","uk.co.ee.myee.Launcher");
 capabilities.setCapability("udid","26d7be7b");
 driver = new AppiumSwipeableDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
 touch = new TouchActions(driver);
 Set<String> contextNames = driver.getContextHandles();

 for (String contextName : contextNames) {
     if (contextName.contains("WEBVIEW_uk.co.ee.myee")){
         driver.context(contextName);
     }
   }


    public boolean TopUpRegisteredCard(){
     try{
     waitForVisible(By.xpath(OR.getProperty("wblTopUpWidget")),10);
     driver.findElement(By.xpath(OR.getProperty("wblTopUpWidget"))).click();   
     waitForVisible(By.xpath(OR.getProperty("btnTopUpRegisteredCard")),10);
     driver.findElement(By.xpath(OR.getProperty("btnTopUpRegisteredCard"))).click();
     waitForVisible(By.xpath(OR.getProperty("txtTopUpPaymentAmt")),10);
     driver.findElement(By.xpath(OR.getProperty("txtTopUpPaymentAmt"))).sendKeys("10");
     driver.findElement(By.xpath(OR.getProperty("txtTopUpCVVNum"))).sendKeys("123");
      touch.flick(driver.findElement(By.xpath(OR.getProperty("txtTopUpCVVNum"))),0,-250,1000).perform();
     waitForVisible(By.xpath(OR.getProperty("btnTopUpMakePayment")),10);
     driver.findElement(By.xpath(OR.getProperty("btnTopUpMakePayment"))).click();
     return true;

     }catch(Exception e){
     ReportTest.error(e.getMessage());
     return false;
     }

我也尝试过 AppiumDriver - TouchAction,这给了我

org.openqa.selenium.UnsupportedCommandException:未知命令:session/9e5f0b55fdfb2c98dd019f44a7bf9c8a/touch/perform

我已经在 Windows 机器上成功运行了上面显示的相同脚本,但现在我已将我的项目移动到 MAC 并且在它没有按预期运行之后。

请帮我解决这个问题

【问题讨论】:

  • 我的直觉告诉我你的 Appium 驱动程序已经过时了
  • 我在 POM io.appiumjava-clientLATESTtest

标签: selenium testng appium selendroid


【解决方案1】:

实际上现在 webview 不支持 Touch Actions。但是有一些解决方法可用;我将通过一个 longpress 示例来展示它:我正在使用 Pointoption,因为我将获取元素的坐标并将其用于 longpress。

public void longpress(PointOption po) {
   //first you need to switch to native view
    driver.switchToNativeView();
    TouchAction action = new TouchAction((PerformsTouchActions) driver);
    action.longPress(po).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)));
    action.release();
    action.perform();
    driver.switchToDefaultWebView();
}

为了得到我在methood下设计的元素的坐标

public PointOption getElementLocation(WebElement element) {
    int elementLocationX;
    int elementLocationY;

    //get element location in webview
    elementLocationX = element.getLocation().getX();
    elementLocationY = element.getLocation().getY();

    //get the center location of the element
    int elementWidthCenter = element.getSize().getWidth() / 2;
    int elementHeightCenter = element.getSize().getHeight() / 2;
    int elementWidthCenterLocation = elementWidthCenter + elementLocationX;
    int elementHeightCenterLocation = elementHeightCenter + elementLocationY;

    //calculate the ratio between actual screen dimensions and webview dimensions
    float ratioWidth = device.getDeviceScreenWidth() / ((MobileDevice) device)
            .getWebViewWidth().intValue();
    float ratioHeight = device.getDeviceScreenHeight() / ((MobileDevice) device)
            .getWebViewHeight().intValue();

    //calculate the actual element location on the screen , if needed you can increase this value,for example i used 115 for one of my mobile devices.
    int offset = 0;  


    float elementCenterActualX = elementWidthCenterLocation * ratioWidth;
    float elementCenterActualY = (elementHeightCenterLocation * ratioHeight) + offset;
    float[] elementLocation = {elementCenterActualX, elementCenterActualY};

    int elementCoordinateX, elementCoordinateY;
    elementCoordinateX = (int) Math.round(elementCenterActualX);
    elementCoordinateY = (int) Math.round(elementCenterActualY);
    PointOption po = PointOption.point(elementCoordinateX, elementCoordinateY);
    return po;
}

现在你有一个 longpress(PointOption po) 和 getElementLocation(Webelement element) 方法,它们为你提供了在 longpress 方法中使用的 po 值。现在一切准备就绪,您可以按如下方式使用它们..

longpress(getElementLocation(driver.findElement(By.id("the selector can be any of them(xpath,css,classname,id etc.)")));
 

【讨论】:

    猜你喜欢
    • 2017-11-07
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多