首页 > 解决方案 > 在 Appium iOS 自动化中使用时,PageFactory 似乎很慢

问题描述

我们正在使用 Appium 自动化一个 react-native iOS 应用程序。我们正在使用 PageFactory 设计模式。对于单击元素,这是正在使用的代码:

  1. 等待元素可见。
  2. 单击元素

public  Boolean waitUntilVisible(WebElement element)
{
	try {
		wait.until(ExpectedConditions.visibilityOf(element));
		return true;
	}catch (Exception e)
	{}
	return false;
}

public boolean click(WebElement element)
{
	//Click on the element and return true if successful and false if unsuccessful.
	try 
	{
        waitUntilVisible(element);
		element.click();
	} catch (Exception e) {}
	return false;
}

整体执行似乎花费了太多时间。根据我的理解,waitUntilVisible 会等到元素的 isDisplayed() 变为真。
当我们使用 PageFactory 时,我假设元素识别发生了两次。
1. 在检查可见性之前首先识别元素。
2、点击前会再次识别相同的元素。

由于我们在很多领域都使用xpath,因此元素识别通常需要更长的时间。对于一个简单的点击,相同的元素被识别两次,这进一步增加了时间。

我想知道存储已识别元素的任何解决方案,以便它不会花时间再次识别它。

所以我修改了我的代码,如下所示:

public  WebElement waitUntilVisible(WebElement element)
{
	try {
		return wait.until(ExpectedConditions.visibilityOf(element));
	}catch (Exception e)
	{}
	return null;
}

public boolean click(WebElement element)
{
	//Click on the element and return true if successful and false if unsuccessful.
	try 
	{
        WebElement remoteElement = waitUntilVisible(element);
		remoteElement.click();
	} catch (Exception e) {}
	return false;
}

这种方法似乎不节省时间。

我有没有其他方法可以减少执行时间。

注意:我们使用 WebElement 而不是 IOSElement,以便在桌面自动化中使用的相同代码也可以在 IOS 自动化中使用。

标签: appium-iospage-factory

解决方案


利用

mobileElement = driver.findElement(MobileBy.locator("locator"));
appiumDriverActions.moveToElement(mobileElement).click().perform();
appiumWait.until(ExpectedConditions.condition(Element/locator));

代替

mobileElement = driver.findElement(MobileBy.locator("locator"));
mobileElement.click();

在某些情况下,使用来自 iOS13 的新默认 VC,最好是 moveToElement 然后使用操作单击而不是element.click();

有关更多详细信息,https://medium.com/@ayman.ibrahim.mansour/appiums-best-way-to-deal-with-the-new-view-controller-presentation-in-ios-13-6d801fcc3cb


推荐阅读