Thursday, May 8, 2014

Selenium : Object identification + Reporting +No Halt in 1 statement ,EXECUTION LIKE A BOSS !!

Object identification , LIKE A BOSS !!


I always liked the way QTP identifies the object and error reporting technique used in-case the element was not found . I find it to be more convenient for the automation tester , as he only needed to concentrate only on the reports rather than the inner mechanism.

In the back of my mind I wanted something similar to be implemented in Selenium, as you get so used to the luxurious comforts offered by QTP back in the days. Today I had a little time to spare implemented something similar .

Aim :

  1.  To identify elements as usual using Xpath , id ... what ever.
  2. Incase element not found report it to TestNG . 
  3. Take screenshot and hyper link it in the TestNG and xslt Report.
  4. Quit the current Test case go to next Test Case.
  5. Mechanism should be hidden and rather everything should be taken by 1line of code. (People using it should find it much more convenient than QTP reports ).

Basic Requirements:

  1. All the usual Eclipse , Selenium  etc.,
  2. I will be using TestNG framework.

Here is how it can be done :

1.  Usage

2. Implementation



3. Report generated :



Code Here :

//Parent Class  -  Can be your library file 

static class Findele extends SeleneseTestBase
     {
          WebDriver driver_temp;
          String sBrowser="";
       
         Findele(WebDriver driver)
         {
             this.driver_temp=driver;
            // sBrowser=ParallelRunning.sBrowser;
         }
       
         public Object Xpath(String value)
         {
             sBrowser=Thread.currentThread().getName();
             try{
                 System.out.println("Finding Object using Xpath="+value);
                 return driver_temp.findElement(By.xpath(value));
             }catch(Throwable  t)
               {
                 ParallelRunning.APPLICATION_LOGS.debug("Unable to find the object");  // Log error in Log File  http://catchbug.blogspot.in/2014/05/applicationlog-seleniumlog-and.html */
               
                 System.out.println("---Finding Object using Xpath Failed");   //Log error in Report
                   verificationErrors.append(t.getMessage());
                   Reporter.log("(object not found) Xpath="+value+"<br>");  

                   String sError=misc.timestamp();                 //Take screen shot attach it to the error in Report
                   misc.take_screenshot(driver_temp,"C:\\save_here.jpg" );
                   Reporter.log("<a href=\"" +"file:///"+ error_screen_path.replace("\\", "/")+".jpg" + "\">Screenshot</a>");
                  
                   clearVerificationErrors(); // Fail test case move on
                   if (!"".equals(verificationErrors.toString()))
                   fail(verificationErrors.toString());
                   return null;
             }          
         }
       
         public Object ID(String value)  // You add the above enhancements for ID or any other locator as well
         {
             try{
                 System.out.println("Finding Object using ID="+value);
                 return driver_temp.findElement(By.id(value));
             }catch(Throwable  t)
               {
                 System.out.println("---Finding Object using ID Failed");
                   verificationErrors.append(t.getMessage());
                   Reporter.log("object not found--"+"<br>");  
                   clearVerificationErrors();
                   if (!"".equals(verificationErrors.toString()))
                   fail(verificationErrors.toString());
                   return null;
             }          
         }
     }


 static void take_screenshot(WebDriver driver,String sFilename){
         File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
         try {
            FileUtils.copyFile(screenshot, new File(sFilename+".jpg"));
        } catch (Throwable t) {
            // TODO Auto-generated catch block
            System.out.print("failed to create a Screen shot");
        }
     }

Usage

library.Findele FindEle=new library.Findele(driver);       //Declaration 
        ((WebElement) FindEle.ID("dsfdsfdsf")).sendKeys("User"); //usage


No comments:

Post a Comment