gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:23:21 24 4
gpt4 key购买 nike

我在亚马逊购物应用程序上进行了测试。滑动查找元素后出现问题。因为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com