代码之家  ›  专栏  ›  技术社区  ›  Ahamed Abdul Rahman

PageFactory在用于Appium iOS自动化时似乎速度较慢

  •  0
  • Ahamed Abdul Rahman  · 技术社区  · 7 年前

    我们正在使用Appium自动化react本机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自动化中使用。

    0 回复  |  直到 7 年前
        1
  •  0
  •   miyaman    6 年前

    使用

    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时,最好先移动到元素,然后使用操作单击,而不是单击 element.click();

    欲知更多详情, https://medium.com/@ayman.ibrahim.mansour/appiums-best-way-to-deal-with-the-new-view-controller-presentation-in-ios-13-6d801fcc3cb