显式和隐式等待
等待是在执行下一步骤之前让自动任务等定量的时间。
显示等待
一个显式等待就是在继续执行代码之前编码等待定义一个特定条件发生。最糟糕的例子是Thread.sleep(),这设置了一个准确的等待时间。WebDriver提供了一些方便的方法帮助您些代码来等待要求的时间。WebDriverWait和ExpectedCondition的结合就是一种实现的方法。 WebDriver driver = new FirefoxDriver();
driver.get(\"http://somedomain/url_that_delays_loading\");
WebElementmyDynamicElement = (new WebDriverWait(driver, 10)) .until(new ExpectedCondition 在抛出TimeoutException之前这会等待最多10秒钟,或者它找到了元素,在0-10秒之间返回。WebDriverWait默认每500毫秒调用ExpectedCondition直到它成功返回。ExpectedCondition类型的成功返回是布尔值true或非null的返回值。 ExpectedConditions 有些自动化web浏览器时常用的条件。下面列出的是每个实现。Java恰巧有方便的方法,因此您不需要编写一个ExpectedCondition类自己或为它们创建自己的实用程序。 元素可点击–元素显示并且可用。 WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(\"someid\"))); ExpectedConditions类中包含了一组与定义条件,可用于WebDriverWait 隐式等待 隐式等待告诉WebDriver一段特定的时间轮训一下DOM,来查找一个元素或者元素组,如果它们没有马上找到的话。默认设置是0。一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用。 WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get(\"http://somedomain/url_that_delays_loading\"); WebElementmyDynamicElement = driver.findElement(By.id(\"myDynamicElement\")); RemoteWebDriver 截屏 importjava.io.File; import java.net.URL; importorg.openqa.selenium.OutputType; importorg.openqa.selenium.TakesScreenshot; importorg.openqa.selenium.WebDriver; importorg.openqa.selenium.remote.Augmenter; importorg.openqa.selenium.remote.DesiredCapabilities; importorg.openqa.selenium.remote.RemoteWebDriver; public class Testing { public void myTest() throws Exception { WebDriver driver = new RemoteWebDriver( new URL(\"http://localhost:4444/wd/hub\"), DesiredCapabilities.firefox()); driver.get(\"http://www.google.com\"); // RemoteWebDriver does not implement the TakesScreenshot class // if the driver does have the Capabilities to take a screenshot // then Augmenter will add the TakesScreenshot methods to the instance WebDriveraugmentedDriver = new Augmenter().augment(driver); File screenshot = ((TakesScreenshot)augmentedDriver). getScreenshotAs(OutputType.FILE); } } 使用FirefoxProfile FirefoxProfilefp = new FirefoxProfile(); // set something on the profile... DesiredCapabilities dc = DesiredCapabilities.firefox(); dc.setCapability(FirefoxDriver.PROFILE, fp); WebDriver driver = new RemoteWebDriver(dc); 使用ChromeOptions ChromeOptions options = new ChromeOptions(); // set some options DesiredCapabilities dc = DesiredCapabilities.chrome(); dc.setCapability(ChromeOptions.CAPABILITY, options); WebDriver driver = new RemoteWebDriver(dc); 高级用户交互 Actions类允许你创建一个Actions的链然后执行它们。组合的可能多得数不清。以下是一些您可能想要使用的常用的交互。对于完整的Action,请参看API文档JavaC#RubyPython 高级用户交互要求启用native events。这是目前支持的native events的矩阵: ChromplatforIEIEIEIEFF3.FF10e ChromChromOperAndroiiOm 6 7 8 9 6 + stable beta e dev a d S e Windows n/Y Y Y Y XP a Windows n/n/Y Y Y 7 a a Y Y Y Y Y Y Y Y ? ? Y [1] Y [1] n/a n/a n/a Linux n/n/n/n/(UbuntuY [2] Y [2] Y a a a a ) Mac OSX n/n/n/n/N a a a a N Y n/a Y Y ? Y [1] Y n/a Y n/a ? ? Y [1] N Y N Mobile n/n/n/n/n/a ? Device a a a a [1] (1, 2, 3, 4) Using the emulator [2] (1, 2) With explicitly enabling native events 浏览器启动操作 使用代理 Internet Explorer 最简单和推荐的方法是在运行测试的机器中手工设置代理。如果不可能,或者你想让你的测试运行在不同的配置或者代理中,那么你需要使用以下技巧来使用一个Capabilities对象。这会暂时改变系统的代理设置,在测试结束后再改回原来的设置。 String PROXY = \"localhost:8080\"; org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy(); proxy.setHttpProxy(PROXY) .setFtpProxy(PROXY) .setSslProxy(PROXY); DesiredCapabilities cap = new DesiredCapabailities(); cap.setPreference(CapabilityType.PROXY, proxy); WebDriver driver = new InternetExplorerDriver(cap); Chrome 基本上与IE相同。它与IE一样,使用相同的配置(Windows)。在Mac中,使用System Preference->Network settings。在Linux使用System>Preferences>Network Proxy Preferences(或者/tec/environment设置http_proxy) Firefox Firefox用一个proxy来维护代理配置。你可以在一个profile中预先设置一个proxy,然后使用这个Profile。或者你也可以随机生成一个Profile,然后设置代理,如下例子: StringPROXY=\"localhost:8080\"; org.openqa.selenium.Proxyproxy=neworg.openqa.selenium.Proxy(); proxy.setHttpProxy(PROXY) .setFtpProxy(PROXY) .setSslProxy(PROXY); DesiredCapabilitiescap=newDesiredCapabailities(); cap.setPreference(CapabilityType.PROXY,proxy); WebDriverdriver=newFirefoxDriver(cap); 因篇幅问题不能全部显示,请点此查看更多更全内容