原帖地址: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 GetScreenshot
public 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 webDriver
Screenshot 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:
platform | IE6 | IE7 | IE8 | IE9 | FF3.6 | FF10+ | Chrome stable | Chrome beta | Chrome dev | Opera | Android | iOS |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Windows XP | Y | Y | Y | n/a | Y | Y | Y | Y | Y | ? | Y [1] | n/a |
Windows 7 | n/a | n/a | Y | Y | Y | Y | Y | Y | Y | ? | Y [1] | n/a |
Linux (Ubuntu) | n/a | n/a | n/a | n/a | Y [2] | Y [2] | Y | Y | Y | ? | Y [1] | n/a |
Mac OSX | n/a | n/a | n/a | n/a | N | N | Y | Y | Y | ? | Y [1] | N |
Mobile Device | n/a | n/a | n/a | n/a | n/a | ? | n/a | n/a | n/a | ? | Y | N |
[1] | (1,2, 3, 4) Using the emulator |
[2] | (1,2) With explicitly enabling 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.