|
- 1.sleep eg: Thread.sleep(60000);强制等待60s
- 2.implicitlyWait
- driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
- //隐式等待,全局等待30s不管是否已经加载
- 3.WebDriverWait 显示等待,这个需要增加一定等待时间,显示等待时间可以通过WebDriverWait 和util来决定
- WebDriverWait wait=new WebDriverWait(driver,60);
- WebElement e=wait.until(new ExpectedCondition<WebElement>(){
- @Override
- public WebElement apply(WebDriver d){
- return d.findElement(By.id("q"));
- }
- })
- 只用WebDriverWait是selenium所特有,在java-client中也找不到相应,如果想使用这种方法怎么办?
- 改造轮子,首先添加AndroidDriverWait.java, 其实是将WebDriverWait的类型改成AndroidDriverWait
- 具体代码如下:
- 第一步:创建AndroidDriverWait.java, 其实是将WebDriverWait的类型改成AndroidDriverWait
- import java.util.concurrent.TimeUnit;
- import io.appium.java_client.android.AndroidDriver;
- import org.openqa.selenium.NotFoundException;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebDriverException;
- import org.openqa.selenium.TimeoutException;
- import org.openqa.selenium.remote.RemoteWebDriver;
- import org.openqa.selenium.support.ui.SystemClock;
- import org.openqa.selenium.support.ui.Clock;
- import org.openqa.selenium.support.ui.FluentWait;
- import org.openqa.selenium.support.ui.Sleeper;
- public class AndroidDriverWait extends FluentWait<AndroidDriver>{
- public final static long DEFAULT_SLEEP_TIMEOUT=500;
- private final WebDriver driver;
- public AndroidDriverWait(AndroidDriver driver,long timeOutInSeconds){
- this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, DEFAULT_SLEEP_TIMEOUT);
- }
- public AndroidDriverWait(AndroidDriver driver,long timeOutInSeconds,long sleepInMillis){
- this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, sleepInMillis);
- }
- public AndroidDriverWait(AndroidDriver driver,Clock clock,Sleeper sleeper,long timeOutInSeconds,long sleepTimeOut){
- super(driver,clock,sleeper);
- withTimeout(timeOutInSeconds,TimeUnit.SECONDS);
- pollingEvery(sleepTimeOut,TimeUnit.MILLISECONDS);
- ignoring(NotFoundException.class);
- this.driver=driver;
- }
- @Override
- protected RuntimeException timeoutException(String message,Throwable lastException){
- TimeoutException ex=new TimeoutException(message,lastException);
- ex.addInfo(WebDriverException.DRIVER_INFO,driver.getClass().getName());
- if(driver instanceof RemoteWebDriver){
- RemoteWebDriver remote=(RemoteWebDriver) driver;
- if(remote.getSessionId()!=null){
- ex.addInfo(WebDriverException.SESSION_ID,remote.getSessionId().toString());
- }
- if(remote.getCapabilities()!=null){
- ex.addInfo("Capabilities",remote.getCapabilities().toString());
- }
- }
- throw ex;
- }
-
- }
- 第二步:接着需要修改接口:ExpectedCondition,将其WebDriver的类型替换为AndroidDriver
- import com.google.common.base.Function;
- import io.appium.java_client.android.AndroidDriver;
- public interface ExpectedCondition<T> extends Function<AndroidDriver,T>
- {}
- 第三步:经过第一二步后,就可以在appium 中直接使用;
- WebElement showClose=new AndroidDriveWait(driver,60).until(new ExpectedCondition<WebElement>(){
- public WebElement apply(AndroidDriver d){
- return d.findElemnt(By.id("com.zhihu.android:id/showcase_close"));
- }
- });
复制代码
|
|