Navigating
The first thing you'll want to do with WebDriver is navigate to a page. The normal way to do this is by calling "get": driver.get("http://www.google.com"); WebDriver will wait until the page has fully loaded (that is, the "onload" event has fired) before returning control to your test or script. Interacting With the Page Just being able to go to places isn't terribly useful. What we'd really like to do is to interact with the pages, or, more specifically, the HTML elements within a page. First of all, we need to find one. WebDriver offers a number of ways of finding elements. For example, given an element defined as: <input name="passwd" id="passwd-id" /> we could use any of: WebElement element; element = driver.findElement(By.id("passwd-id")); element = driver.findElement(By.name("passwd")); element = driver.findElement(By.xpath("//input[@id='passwd-id']")); Filling In FormsWe've already seen how to enter text into a textarea or text field, but what about the other elements? You can "toggle" the state of checkboxes, and you can use "setSelected" to set something like an OPTION tag selected. Dealing with SELECT tags isn't too bad: WebElement select = driver.findElement(By.xpath("//select")); List<WebElement> allOptions = select.findElements(By.tagName("option")); for (WebElement option : allOptions) { System.out.println(String.format("Value is: %s", option.getValue())); option.setSelected(); } This will find the first "SELECT" element on the page, and cycle through each of it's OPTIONs in turn, printing out their values, and selecting each in turn. As you can see, this isn't the most efficient way of dealing with SELECT elements. WebDriver's support classes come with one called "Select", which provides useful methods for interacting with these. 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(); // Assume the button has the ID "submit" :) Alternatively, WebDriver has the convenience method "submit" on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn't in a form, then the "NoSuchElementException" will be thrown: element.submit() Getting Visual Information And Drag And DropSometimes you want to extract some visual information out of an element, perhaps to see if it's visible or where it is on screen. You can find out this information by casting the element to a RenderedWebElement: WebElement plain = driver.findElement(By.name("q")); RenderedWebElement element = (RenderedWebElement) plain; Not all drivers render their content to the screen (such as the HtmlUnitDriver), so it's not safe to assume that the cast will work, but if it does you can gather additional information such as the size and location of the element. In addition, you can use drag and drop, either moving an element by a certain amount, or on to another element: RenderedWebElement element = (RenderedWebElement) driver.findElement(By.name("source")); RenderedWebElement target = (RenderedWebElement) driver.findElement(By.name("target")); element.dragAndDropOn(target); Moving Between Windows and FramesIt's rare for a modern web application not to have any frames or to be constrained to a single window. WebDriver supports moving between named windows using the "switchTo" method: driver.switchTo().window("windowName"); All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window's name? Take a look at the javascript or link that opened it: <a href="somewhere.html" target="windowName">Click here to open a new window</a> Alternatively, you can pass a "window handle" to the "switchTo().window()" method. Knowing this, it's possible to iterate over every open window like so: for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); } You can also swing from frame to frame (or into iframes): driver.switchTo().frame("frameName"); It's possible to access subframes by chaining switchTo() calls, and you can specify the frame by its index too. That is: driver.switchTo().frame("frameName") .switchTo().frame(0) .switchTo().frame("child"); would go to the frame named "child" of the first subframe of the frame called "frameName". All frames are evaluated as if from currently switched to frame. To get back to the top level, call: driver.switchTo().defaultContent();
5 Comments
Kumar
8/25/2011 04:30:32 pm
still having some problem with frame. I am trying to log out from gmail. but this never move to the frame. help me please. .
Reply
Masud
8/25/2011 06:12:29 pm
Dear kumar.. Google js is really wakkk... if you are just doing for learning then try any other web with xpath .. that will work for you. surely ! just keep the frame name correct.
Reply
Kumar
8/25/2011 06:43:26 pm
thank you. you are right !!
Reply
jack
8/27/2011 08:52:52 pm
some frame ids are dynamic. on this case how will i handle that?
Reply
Masud
8/28/2011 12:26:41 pm
@Jack: on that case first try with xpath.
Reply
Leave a Reply. |
AuthorI am Masud Parvez. Working as IT Senior Project Manager for RMIT University. Previously I built and run a distributed Test Center. My success was to turn that in to one of the most successful business units of the company. Categories
All
|