java - 在appium中滑动后无法单击元素

标签 java android selenium appium

我在亚马逊购物应用程序上进行了测试。滑动查找元素后出现问题。 因为 ListView 太长了。我需要滚动(使用滑动 API)。但是在我滑动之后,我不能再点击该元素了。它没有返回错误,但应用程序没有响应。

ScrollToElement() 函数是滑动到搜索元素。我尝试使用触摸 Action 点击但没有希望。我尝试点击一个确切的 (372,466) 位置并且它可以工作,但它并不像预期的那样。提前致谢。

public class FirstTest {
   private AppiumDriver driver;
   private   Dimension size;

   @BeforeClass
   public void Setup() throws MalformedURLException {


      String appActivityText = "com.amazon.mShop.home.HomeActivity";


      String appPackageText = "in.amazon.mShop.android.shopping";

      String fileLocation = "/system/app/";

      File classpathRoot = new File(System.getProperty("user.dir"));
      File appDir = new File(classpathRoot, "\\STC");
      File app = new File(appDir, fileLocation);
      System.out.println(app);
      DesiredCapabilities capabilities = new DesiredCapabilities();

      capabilities.setCapability("device", "Android");
      capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");

      capabilities.setCapability(CapabilityType.PLATFORM, "Android");

      capabilities.setCapability(CapabilityType.VERSION, "4.4.2");

      capabilities.setCapability("deviceName", "420373d0de528100");//420373d0de528100 01a61316598f30e6

      capabilities.setCapability("newCommandTimeout", "100");

      // capabilities.setCapability("app", "Chrome"/*app.getAbsolutePath()*/);

      capabilities.setCapability("appPackage", appPackageText);

      capabilities.setCapability("appActivity", appActivityText);

      driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
      driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);

   }

   @Test
   public void Login() throws Exception {
      // Click on Shop by Deparment link
      driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
      System.out.println("Click on Shop by Deparment link");
      driver.findElement(By.id("web_home_shop_by_department_label")).click();
      Thread.sleep(1000);

      WebElement element = driver.findElement(By.name("Office Products Link"));
      ScrollToElement(driver, element);

      Thread.sleep(3000);
//      TouchAction action = new TouchAction(driver);
//      action.tap(372,466).perform();

      driver.findElement(By.name("Office Products Link")).click();

   }
   @AfterClass
   public void closeApp() {
      // driver.closeApp();

   }

public static void ScrollToElement(AppiumDriver driver, WebElement element){
      size = driver.manage().window().getSize();

      // Find swipe start and end point from screen's with and height.
      // Find starty point which is at bottom side of screen.
      int start = (int) (size.height * 0.20);
      int starty = (int) (size.height * 0.80);
      // Find endy point which is at top side of screen.
      int endy = (int) (size.height * 0.20);
      // Find horizontal point where you wants to swipe. It is in middle of
      // screen width.
      int startx = size.width / 2;

      while (true) {
         driver.swipe(startx, starty, startx, endy, 3000);
         start = start + (starty - endy);
         if (element.getLocation().getY() - start < size.height - endy) {
            break;
         }
      }
   }
}

最佳答案

您的代码存在的问题是您错误地使用了driver.findElement。 让我解释。 driver.findElement 检查屏幕上显示的元素,如果找到,它将返回。

您的代码的问题是您在滚动功能之前执行了 driver.findElement,这意味着如果该元素当前不在屏幕上,测试将始终失败。

此代码块应该可以帮助您:

public WebElement findItemWithScrollingUsingBy(By by, int interactions) {
    for (int i = 0; i < interactions; i++) {
        if (driver.findElements(by).size() == 0) {
            scrollDown();
        } else {
            return driver.findElement(by); //you can add .click() here instead of returning the element
        }
    }
    Assert.fail("Element not found");
    return null;
}

函数scrollDown()只是将滑动的代码块放到一个外部函数中,这样就干净多了。

总结一下我的回答:

  1. 在屏幕上搜索元素 - 如果找到,请点击
  2. 如果没有找到 - 进行另一个滚动操作并再次搜索该元素
  3. 重复

关于java - 在appium中滑动后无法单击元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41917557/

相关文章:

selenium - 错误 Protractor 和 Selenium : StaleElementReferenceError: stale element reference: element is not attached to the page document

java - Spring 启动: Can not POST into ManyToMany Relation

java - 为什么我的随机数生成器方法会返回相同的数字?

android - 新的 Android 工具栏 : How to hide the overflow menu icon?

android - 从 WebView 访问 session 变量

java - 使用 WebDriver driver = new FirefoxDriver() 和 FirefoxDriver driver = new FirefoxDriver() 创建驱动程序对象之间的区别?

python - Selenium 发送 key 在 Ubuntu 上无法使用/

java - 通话时自动语音留言

java - 迭代器在使用下一个函数时出现问题

c# - 在具有相同命名空间的多个项目的解决方案中找不到命名空间