Selenium Automating Web Applications for Testing
Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.
Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. It is also the core technology in countless other browser automation tools, APIs and frameworks.
A. Selenium IDE
The Selenium-IDE (Integrated Development Environment) is the tool you use to develop your Selenium test cases. It's an easy-to-use Firefox plug-in and is generally the most efficient way to develop test cases. It also contains a context menu that allows you to first select a UI element from the browser's currently displayed page and then select from a list of Selenium commands with parameters pre-defined according to the context of the selected UI element. This is not only a time-saver, but also an excellent way of learning Selenium script syntax.
See Selenium IDE
B. Selenium-WebDriver API Commands and Operations (by Java)
Fetching a Page
driver.get("http://www.google.com");
Locating UI Elements (WebElements)
// find element by ID // e.g. <p id="coolestWidgetEvah">...</p> WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
// find element by Class Name // e.g. // <p class="cheese"><span>Cheddar</span></p> // <p class="cheese"><span>Gouda</span></p> List
cheeses = driver.findElements(By.className("cheese")); // find element by Tag Name // e.g. <iframe src="..."></iframe> WebElement frame = driver.findElement(By.tagName("iframe"));
// find element by Name // e.g. <input name="cheese" type="text"/> WebElement cheese = driver.findElement(By.name("cheese"));
// find element by Link Text // e.g. <a href="http://www.google.com/search?q=cheese" rel="nofollow">cheese</a> WebElement cheese = driver.findElement(By.linkText("cheese"));
// find element by Partial Link Text // e.g. <a href="http://www.google.com/search?q=cheese" rel="nofollow">search for cheese</a> WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
// find element by CSS // e.g. <p id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></p> WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged")); // find element by XPATH // e.g. <input type="text" name="example"> List
inputs = driver.findElements(By.xpath("//input")); // find element by JavaScript // simple example on a page that has jQuery loaded WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]"); // Finding all the input elements to the every label on a page List<WebElement> labels = driver.findElements(By.tagName("label")); List<WebElement> inputs = (List ) ((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); // check element exist or not, using findElements boolean isPresent = driver.findElements(By.yourLocator).size() > 0 User Input - Filling In Forms
WebElement select = driver.findElement(By.tagName("select")); List
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"); driver.findElement(By.id("submit")).click(); element.submit(); Moving Between Windows and Frames
// switch to window driver.switchTo().window("windowName"); // how do you know the window's name for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); } // switch to frame driver.switchTo().frame("frameName");
Example// store the current window handle String winHandleBefore = driver.getWindowHandle(); // perform the click operation that opens new window // switch to new window opened for (String winHandle : driver.getWindowHandles()) { driver.switchTo().window(winHandle); } // perform the actions on new window // close the new window, if that window no more required driver.close(); // switch back to original browser (first window) driver.switchTo().window(winHandleBefore); // continue with original browser (first window)
Window Util// use to wait for window popup until available public static void waitForNumberOfWindowsEqual(final WebDriver driver, final int numberOfWindows) { new WebDriverWait(driver, 10).until(new ExpectedCondition
() { @Override public Boolean apply(WebDriver arg0) { return driver.getWindowHandles().size() == numberOfWindows; } }); } Popup Dialogs
Alert alert = driver.switchTo().alert();
Navigation: History and Location
driver.navigate().to("http://www.example.com"); driver.navigate().forward(); driver.navigate().back();
Cookies
// set the cookie, this one's valid for the entire domain Cookie cookie = new Cookie("key", "value"); driver.manage().addCookie(cookie); // output all the available cookies for the current URL Set
allCookies = driver.manage().getCookies(); for (Cookie loadedCookie : allCookies) { System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue())); } // delete cookies in 3 ways // by name driver.manage().deleteCookieNamed("CookieName"); // by Cookie driver.manage().deleteCookie(loadedCookie); // all of them driver.manage().deleteAllCookies(); Changing the User Agent
FirefoxProfile profile = new FirefoxProfile(); profile.addAdditionalPreference("general.useragent.override", "some UA string"); WebDriver driver = new FirefoxDriver(profile);
Drag And Drop
WebElement element = driver.findElement(By.name("source")); WebElement target = driver.findElement(By.name("target")); (new Actions(driver)).dragAndDrop(element, target).perform();
Explicit and Implicit Waits
Explicit Waits
WebDriver driver = new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement"))); // Expected Conditions WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Implicit Waits
WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
Comments
Post a Comment