Skip to main content

Posts

Maintain Locators - Selenium Automation Projects

Automating your application is easy on the way to reach your goal in your selenium tests. But give me answers to below questions or answer yourself. 1. Is it readable? 2. Is it easily modifiable while your script grows? 3. Is it easily identifiable? 4. Can anybody edit your locator? etc.... For all these questions, your answer might be a big NO.To solve this prob, here is the approach. Maintaining Locators: Always maintain your locators in a single file which is supported by your script driven language like below. Java   ---  you have .properties file Python  --- Config parser or dictionaries C#  --- .config file Ruby  --- YAML  file So maintain your locators in a single file and edit whenever you want like QTP Object Repository. Your file should contain your locators like this. PageName.ElementName=ElementLocatorString Ex: locators.properties file # Here  registration  is my page and firstname is element on registration page....

Selenium - CSS Locators

Here are some tips to use CSS locators in your selenium automation projects. Just go through this XML structure. I going to speak about some locators to get inside elements.  <div   id = 'testdiv' > <span> <a   href = 'http://sds1' > my Link2 </a> </span> <a   href = 'http://sds' > my Link1 </a> <div   class = 'myDiv1' > <a   href = 'sds' > my link3 </a> </div> <form> <input   class = "username" > </input> <input   class = "alias" > </input> <input   type = "hidden"   name = "vid"   value = "442bbdf4-9226-41cd-801e-0d065a16cc52" > <input   type = "hidden"   name = "lf_cid"   value = "LF_c10cf6d6" > </form> </div> Direct Child: css=div > a               //This will give you 'My Link1' element Child or Su...

Custom functions to Override Standard Operations - QTP

Hi all, Here I am going to talk about a nice feature in QTP. Generally we might require the operations such as click, Set, etc which should not stop the script if it fails on that particular step. Also some different way we have simulate the operations for different type of objects. ie. We may need custom functions for click, set etc. Here is the example. ''This is my function which will do repeative typing Function MySetRepeat(test_object, TextToSet)    test_object.Set( TextToSet + TextToSet) End Function ''This line is used to register the above written custom function for type of objects. ''1st Parameter: Class/Type of object ''2nd Parameter: Function name to call for the test objects ''3rd Parameter: Actual function name you wrote ''4th Parameter: Set as default function or not RegisterUserFunc "WebEdit", " SetRepeat ", " MySetRepeat ",True So, now I am using my function in my script...

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 + "\">" +...