WebDriver: Advanced Usage
来源:互联网 发布:流星网络电视tv版apk 编辑:程序博客网 时间:2024/05/01 20:59
原帖地址:http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
参考地址:http://www.docin.com/p-748352113.html
Explicit and Implicit Waits
Waiting is having the automated task execution elapse a certain amount of time before continuing with the next step.
Explicit Waits
An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code.The worst case of this is Thread.sleep(), which sets the condition to an exact time period to wait.There are some convenience methods provided that help you write code that will wait only as long as required.WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.
IWebDriver driver = new FirefoxDriver();driver.Url = "http://somedomain/url_that_delays_loading";WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));IWebElement myDynamicElement = wait.Until<IWebElement>((d) => { return d.FindElement(By.Id("someDynamicElement")); });
This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds.WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return isfor ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.
This example is also functionally equivalent to the first Implicit Waits example.
Expected Conditions
There are some common conditions that are frequently come across when automating web browsers. Listed below areImplementations of each. Java happens to have convienence methods so you don’t have to code an ExpectedConditionclass yourself or create your own utility package for them.
- Element is Clickable - it is Displayed and Enabled.
The ExpectedConditions package (Java) (Python) (.NET) contains a set of predefined conditions to use with WebDriverWait.
Implicit Waits
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
WebDriver driver = new FirefoxDriver();driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));driver.Url = "http://somedomain/url_that_delays_loading";IWebElement myDynamicElement = driver.FindElement(By.Id("someDynamicElement"));
RemoteWebDriver
Taking a Screenshot
// Add this class to your code and use this instead of RemoteWebDriver// You will then be able to cast it to ITakesScreenshot and call GetScreenshotpublic class ScreenShotRemoteWebDriver : RemoteWebDriver, ITakesScreenshot{ public ScreenShotRemoteWebDriver(Uri RemoteAdress, ICapabilities capabilities) : base(RemoteAdress, capabilities) { } /// <summary> /// Gets a <see cref="Screenshot"/> object representing the image of the page on the screen. /// </summary> /// <returns>A <see cref="Screenshot"/> object containing the image.</returns> public Screenshot GetScreenshot() { // Get the screenshot as base64. Response screenshotResponse = this.Execute(DriverCommand.Screenshot, null); string base64 = screenshotResponse.Value.ToString(); // ... and convert it. return new Screenshot(base64); }}// And then the usage would be:ScreenShotRemoteWebDriver webDriver = new ScreenShotRemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), DesiredCapabilities.Firefox());// ... do stuff with webDriverScreenshot ss = ((ITakesScreenshot)webDriver).GetScreenshot();string screenshot = ss.AsBase64EncodedString;byte[] screenshotAsByteArray = ss.AsByteArray;ss.SaveAsFile(activeDir + TestSuiteName + "//" + FileNanme + imageFormat, ImageFormat.Jpeg);
Using a FirefoxProfile
Using ChromeOptions
AdvancedUserInteractions
The Actions class(es) allow you to build a Chain of Actions and perform them.There are too many possible combinations to count. Below are a few of the commoninteractions that you may want to use. For a full list of actions please refer tothe API docsJavaC#RubyPython
The Advanced User Interactions require native events to be enabled. Here’s a tableof the current support Matrix for native events:
Browser Startup Manipulation
Todo
Topics to be included:
- restoring cookies
- changing firefox profile
- running browsers with plugins
Using a Proxy
Internet Explorer
The easiest and recommended way is to manually set the proxy on the machine that will be running the test.If that is not possible or you want your test to run with a different configuration or proxy,then you can use the following technique that uses a Capababilities object. This temporarily changes the system’sproxy settings and changes them back to the original state when done.
Chrome¶
Is basically the same as internet explorer. It uses the same configuration on the machine as IE does (on windows).On Mac it uses the System Preference -> Network settings. On Linux it uses (on Ubuntu) System > Preferences >Network Proxy Preferences (Alternatively in “/etc/environment” set http_proxy).As of this writing it is unknown how to set the proxy programmatically.
Firefox
Firefox maintains its proxy configuration in a profile. You can preset theproxy in a profile and use that Firefox Profile or you can set it on profilethat is created on the fly as is shown in the following example.
- WebDriver: Advanced Usage
- Selenium Webdriver 学习总结-Advanced Usage-Cookie、Profile(七)
- Selenium Webdriver 学习总结-Advanced Usage-Cookie、Profile(七)
- Selenium Webdriver 学习总结-Advanced Usage-Cookie、Profile(七)
- Advanced usage in vim
- Advanced SQLite Usage in Python
- Spring Security Advanced Usage Sample
- usage
- webDriver
- webdriver
- WebDriver
- Webdriver
- WinDbg Usage
- cscope usage
- doxygen usage
- Maven Usage
- Vss usage
- mq_notify usage
- 一句话搞定webmap(一)——轻地图组件
- ajax的post
- OO多态性的一个体现
- Server.CreateObject("Excel.Application")的权限问题
- [POJ1681]画家问题
- WebDriver: Advanced Usage
- IOS 截屏||截图
- mongodb的group详细成功案例
- pig基础知识总结
- 为何OpenWrt能在这十年里脱颖而出?(转自雷锋网)
- C++的函数重载
- Java学习之Iterator(迭代器)的一般用法
- html Frame
- 服了,写了两篇博客也没人评论,本人打算写小说或者画漫画了,我的csdn是单机吗