Skip to main content

Posts

Showing posts from October, 2011

PageObjects and PageFactory - Selenium 2.0

Hi all, Today I am going to speak about an important and well equipped feature of Selenium2.0 PageObjects and PageFactory. Advantages: 1. Max re-usabality 2. Easy to modify the elements locator strings as it will have only one place for each element 3. Well understandable framework 4. Neatly designed and easy to code. Page Objects: Elements on a page or parts of a page called page objects. We can design a class which consists constructors, WebElements including locators, Getters, Setters of some web elements of a page. ie. Services done by part of a page (application). By using this class, we will instantiate using Page Factories  and use the instance to do the services. So for every element, our full test script contains the locators in one place only. Likewise, we have design classes for every operations done by particular page and use the instances to do the operations. Example: You can build your Page Object of the Common Web Elements (just invented this name :)...

Selenium - Custom ExceptionHandling to Control the script execution

Hi all, Every time exception throws while running the selenium test, we can't know whats the original place its thrown. So you can write an exception handler by own so that it will throw the type. Here is the  ExceptionHandler .Java file. package SeleniumUtils; import java.lang.Exception; @SuppressWarnings("serial") public class ExceptionHandler extends Exception {   public String functionName;  public String errorMessage;   public ExceptionHandler(String functionName, String errorMessage) {   this.functionName = functionName;   this.errorMessage = errorMessage;   }  public String toString(){         return "\nFunction Name: " +this.functionName+ "\nError Message: " +this.errorMessage+ "\n" ;  } } So, just write your handler in your script like below to control your script. function abc() {    if (isElementPresent(locator))          sel.click(...

Selenium - LogPicture in ReportNG generated report

Hi all, Here is the method you can add under your package to log the screenshot in report generated by ReportNG. Before that, just remove the lines from your class files under your ReportNG JAR. Under org.uncommons.reportng.templates.html, edit the below files by extracting the JAR. ie.Remove the lines which is doing some replacement for some characters .(line which have  shouldEscaped ) 1. class-results.html.vm 2. output.html.vm Then, add the modified file to JAR. Then, use the below function to log the screenshot. Add this function to TestNG JAR under the Reporter class which already have log method. //if I am using webdriver instance public static void logPicture(Webdriver driver, String imagePath, String errorMessage) {    final File tmpScreenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);   FileUtils.copyFile(tmpScreenshot, new File(imagePath));    String s = "<a href=\"file:///" + imagePath + "\">" +...

Pass keys in Selenium Tests

Hi all, Here is the simple way to pass any KeyBoard keys to selenium. You can use below methods for your required purpose. sel.keyDownNative(java.awt.event.KeyEvent.VK_SHIFT + "");  //To keep pressing a key sel.keyPressNative(java.awt.event.KeyEvent.VK_F1 + "");  //To just type a key sel.keyUpNative(java.awt.event.KeyEvent.VK_SHIFT + ""); //To Release a key being in pressed state For WebDriver instance, just pass using driver.sendKeys()

Sikuli - Handling Dialogs across Platforms

Hi all, I am writing this post as I came to know this feature is some bit useful for Selenium Users who are running scripts across Windows, Linux and Mac platforms. It is some difficult to handle dialogs while automating browser applications using selenium. Though its easy to automate those dialogs using native supported EXEs like AutoIt for Windows, dogTail for linux, ATOmac( http://pypi.python.org/pypi/atomac ) and AppleScript for Mac, we need a common tool which can do the same operations in all platforms to ease the work. So we are going to Sikuli As its supporting all platforms, its not using any API to automate the dialogs and controls rather its using images of controls to do the operations. It provides IDE to do the scripting on all OSs. Also no need for typing and own scriting. IDE itself provides the flow like 1. Find / Wait / Exists for some image 2. Do KeyBoard or Mouse Operations on that image Finally you can export or save the script by .sikuli extension. T...