使用Appium从Mac运行测试时无法在webview中执行触摸操作

时间:2014-08-07 13:57:00

标签: selenium testng appium selendroid

我对以下问题严重不满,请帮我解决一个有效的解决方案。


对于我正在进行的所有触摸操作

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:unknown命令:session / 9e5f0b55fdfb2c98dd019f44a7bf9c8a / touch / perform

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

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

实际上现在 webview 不支持触​​摸操作。但是有一些解决方法可用;我将用一个长按示例来展示它:我使用 Pointoption 因为我将获得元素的坐标并将其用于长按。

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.)")));