51Testing软件测试论坛

标题: Appium 等待的三种方法 [打印本页]

作者: 一月蔷薇_456    时间: 2018-2-26 16:45
标题: Appium 等待的三种方法
  1. 1.sleep  eg: Thread.sleep(60000);强制等待60s
  2. 2.implicitlyWait
  3.   driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
  4.   //隐式等待,全局等待30s不管是否已经加载
  5. 3.WebDriverWait 显示等待,这个需要增加一定等待时间,显示等待时间可以通过WebDriverWait 和util来决定
  6. WebDriverWait wait=new WebDriverWait(driver,60);
  7.   WebElement e=wait.until(new ExpectedCondition<WebElement>(){
  8.   @Override
  9.   public WebElement apply(WebDriver d){
  10.   return d.findElement(By.id("q"));
  11.   }
  12.   })
  13.   只用WebDriverWait是selenium所特有,在java-client中也找不到相应,如果想使用这种方法怎么办?


  14. 改造轮子,首先添加AndroidDriverWait.java, 其实是将WebDriverWait的类型改成AndroidDriverWait


  15. 具体代码如下:
  16. 第一步:创建AndroidDriverWait.java, 其实是将WebDriverWait的类型改成AndroidDriverWait
  17. import java.util.concurrent.TimeUnit;


  18. import io.appium.java_client.android.AndroidDriver;


  19. import org.openqa.selenium.NotFoundException;
  20. import org.openqa.selenium.WebDriver;
  21. import org.openqa.selenium.WebDriverException;
  22. import org.openqa.selenium.TimeoutException;
  23. import org.openqa.selenium.remote.RemoteWebDriver;
  24. import org.openqa.selenium.support.ui.SystemClock;
  25. import org.openqa.selenium.support.ui.Clock;
  26. import org.openqa.selenium.support.ui.FluentWait;
  27. import org.openqa.selenium.support.ui.Sleeper;






  28. public class AndroidDriverWait extends FluentWait<AndroidDriver>{
  29. public final static long DEFAULT_SLEEP_TIMEOUT=500;
  30. private final WebDriver driver;
  31. public AndroidDriverWait(AndroidDriver driver,long timeOutInSeconds){
  32. this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, DEFAULT_SLEEP_TIMEOUT);
  33. }
  34. public AndroidDriverWait(AndroidDriver driver,long timeOutInSeconds,long sleepInMillis){
  35. this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, sleepInMillis);
  36. }

  37. public AndroidDriverWait(AndroidDriver driver,Clock clock,Sleeper sleeper,long timeOutInSeconds,long sleepTimeOut){
  38. super(driver,clock,sleeper);
  39. withTimeout(timeOutInSeconds,TimeUnit.SECONDS);
  40. pollingEvery(sleepTimeOut,TimeUnit.MILLISECONDS);
  41. ignoring(NotFoundException.class);
  42. this.driver=driver;
  43. }
  44.   @Override
  45.   protected RuntimeException timeoutException(String message,Throwable lastException){
  46. TimeoutException ex=new TimeoutException(message,lastException);
  47. ex.addInfo(WebDriverException.DRIVER_INFO,driver.getClass().getName());
  48. if(driver instanceof RemoteWebDriver){
  49. RemoteWebDriver remote=(RemoteWebDriver) driver;
  50. if(remote.getSessionId()!=null){
  51. ex.addInfo(WebDriverException.SESSION_ID,remote.getSessionId().toString());
  52. }
  53. if(remote.getCapabilities()!=null){
  54. ex.addInfo("Capabilities",remote.getCapabilities().toString());
  55. }
  56. }
  57. throw ex;
  58.   }
  59.    
  60. }


  61. 第二步:接着需要修改接口:ExpectedCondition,将其WebDriver的类型替换为AndroidDriver


  62.    import com.google.common.base.Function;
  63.    import io.appium.java_client.android.AndroidDriver;
  64.    public interface ExpectedCondition<T> extends Function<AndroidDriver,T>
  65.    {}
  66. 第三步:经过第一二步后,就可以在appium 中直接使用;
  67.        WebElement showClose=new AndroidDriveWait(driver,60).until(new ExpectedCondition<WebElement>(){
  68.        public WebElement apply(AndroidDriver d){
  69.        return d.findElemnt(By.id("com.zhihu.android:id/showcase_close"));


  70.        }
  71.        });
复制代码



作者: 梦想家    时间: 2018-2-26 17:28





欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2