WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
Saturday, January 11, 2014
Take a screenshot using Selenium WebDriver with Java
Drag And Drop
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
Changing the User Agent
FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override", "some UA string");
WebDriver driver = new FirefoxDriver(profile);
Cookies
Before we leave these next steps, you may be interested in understanding how to use cookies. First of all, you need to be on the domain that the cookie will be valid for. If you are trying to preset cookies before you start interacting with a site and your homepage is large / takes a while to load an alternative is to find a smaller page on the site, typically the 404 page is small (http://example.com/some404page)
// Go to the correct domain
driver.get("http://www.example.com");
// Now set the cookie. This one's valid for the entire domain
Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);
// And now output all the available cookies for the current URL
Set<Cookie> allCookies = driver.manage().getCookies();
for (Cookie loadedCookie : allCookies) {
System.out.println(String.format("%s -> %s", loadedCookie.getName(),
loadedCookie.getValue()));
}
// You can delete cookies in 3 ways
// By name
driver.manage().deleteCookieNamed("CookieName");
// By Cookie
driver.manage().deleteCookie(loadedCookie);
// Or all of them
driver.manage().deleteAllCookies();
Navigation: History and Location
driver.navigate().to("http://www.example.com");
The “navigate” interface also exposes the ability to move backwards and forwards in your browser’s history:
driver.navigate().forward();
driver.navigate().back();
Popup Dialogs
Alert alert = driver.switchTo().alert();
Moving Between Windows and Frames
driver.switchTo().window("windowName");
<a href="somewhere.html" target="windowName">Click here to open a new window</a>
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
driver.switchTo().frame("frameName");
driver.switchTo().frame("frameName.0.child");
User Input - Filling In Forms
WebElement select = driver.findElement(By.tagName("select"));
List<WebElement> allOptions = select.findElements(By.tagName("option"));
for (WebElement option : allOptions) {
System.out.println(String.format("Value is: %s", option.getAttribute("value")));
option.click();
}
Select select = new Select(driver.findElement(By.tagName("select")));
select.deselectAll();
select.selectByVisibleText("Edam");
Once you’ve finished filling out the form, you probably want to submit it. One way to do this would be to find the “submit” button and click it:
driver.findElement(By.id("submit")).click();
element.submit();
Locating UI Elements (WebElements)
Locating elements in WebDriver can be done on the WebDriver instance itself or on a WebElement. Each of the language bindings expose a “Find Element” and “Find Elements” method. The first returns a WebElement object otherwise it throws an exception. The latter returns a list of WebElements, it can return an empty list if no DOM elements match the query.
The “Find” methods take a locator or query object called “By”. “By” strategies are listed below.
By ID
This is the most efficient and preferred way to locate an element. Common pitfalls that UI developers make is having non-unique id’s on a page or auto-generating the id, both should be avoided. A class on an html element is more appropriate than an auto-generated id.Example of how to find an element that looks like this:
<div id="coolestWidgetEvah">...</div>
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
By Class Name
“Class” in this case refers to the attribute on the DOM element. Often in practical use there are many DOM elements with the same class name, thus finding multiple elements becomes the more practical option over finding the first element.Example of how to find an element that looks like this:
<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>
List<WebElement> cheeses = driver.findElements(By.className("cheese"));
By Tag Name
The DOM Tag Name of the element.Example of how to find an element that looks like this:
<iframe src="..."></iframe>
WebElement frame = driver.findElement(By.tagName("iframe"));
By Name
Find the input element with matching name attribute.Example of how to find an element that looks like this:
<input name="cheese" type="text"/>
WebElement cheese = driver.findElement(By.name("cheese"));
By Link Text
Find the link element with matching visible text.Example of how to find an element that looks like this:
<a href="http://www.google.com/search?q=cheese">cheese</a>>
WebElement cheese = driver.findElement(By.linkText("cheese"));
By Partial Link Text
Find the link element with partial matching visible text.Example of how to find an element that looks like this:
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>
WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
By CSS
Like the name implies it is a locator strategy by css. Native browser support is used by default, so please refer to w3c css selectors <http://www.w3.org/TR/CSS/#selectors> for a list of generally available css selectors. If a browser does not have native support for css queries, then Sizzle is used. IE 6,7 and FF3.0 currently use Sizzle as the css query engine.Beware that not all browsers were created equal, some css that might work in one version may not work in another.
Example of to find the cheese below:
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>
WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged"));
By XPATH
At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible. On those browsers that don’t have native XPath support, we have provided our own implementation. This can lead to some unexpected behaviour unless you are aware of the differences in the various xpath engines.Driver | Tag and Attribute Name | Attribute Values | Native XPath Support |
---|---|---|---|
HtmlUnit Driver | Lower-cased | As they appear in the HTML | Yes |
Internet Explorer Driver | Lower-cased | As they appear in the HTML | No |
Firefox Driver | Case insensitive | As they appear in the HTML | Yes |
<input type="text" name="example" />
<INPUT type="text" name="other" />
List<WebElement> inputs = driver.findElements(By.xpath("//input"));
XPath expression | HtmlUnit Driver | Firefox Driver | Internet Explorer Driver |
---|---|---|---|
//input | 1 (“example”) | 2 | 2 |
//INPUT | 0 | 2 | 0 |
Using JavaScript
You can execute arbitrary javascript to find an element and as long as you return a DOM Element, it will be automatically converted to a WebElement object.Simple example on a page that has jQuery loaded:
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");
List<WebElement> labels = driver.findElements(By.tagName("label"));
List<WebElement> inputs = (List<WebElement>) ((JavascriptExecutor)driver).executeScript(
"var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +
"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels);
Subscribe to:
Posts (Atom)