EventFiringMouse in Selenium webdriver

Mouse Actions in selenium web driver:


Fine, while I was verifying an image (progress bar like, graph chart like) I wanted to make two tests as per my requirement, 
    1) Verify expected image / widget is loaded properly
    2) If it is a progress / graph chart, some part of the image will get shaded region and we need to click on that region to view complete details

I am gonna writing this post to share how actually I achieved and what I experienced in this process.. 

1) Apparently, unlike other functional testing tools, we can't verify the expected image pixel by pixel using selenium web driver.. Instead, we can verify, whether an image is completely loaded or not, in other words, we can make sure, while loading page, expected image has not been broken

      To do this, I have written the following code, 
          import org.apache.http.HttpResponse;
          import org.apache.http.impl.client.DefaultHttpClient;
          import org.apache.http.client.methods.HttpGet;
/**
  * The following method verifying whether an expected image element is loaded properly or not,
  *  by reading image URL as an argument and returns true, if image loaded completely
  *
  *   @param imageURL
  **/
  public static boolean verifyImageStatus(String imageURL){
      try{
              HttpResponse  response = new DefaultHttpClient().execute(new HttpGet(imageURL));
              int statusCode = response.getStatusLine().getStatusCode();
              if(statusCode != 200) {
                           return false;
              }
      } catch(ClientProtocolException cpe) {
      } catch(IOException ioe) {
      }
       return true;
  }


2) To click on an web element, we can do it two ways as per my knowledge, using selenium webdriver 

 > Basic level:     Using simple 'Actions' class 

     Actions action = new Actions(webDriverReference);
     WebElement mainMenu = webDriverReference.findElement(By.linkText("mainMenuLink"));
     action.moveToElement(mainMenu);

     WebElement subMenu =  webDriverReference.fineElement(By.linkText("subMenuLink"));
     action.moveToElement(subMenu);
     action.click().build().perform();

     The entire thing can be also be done in a other way, 

     Actions action = new Actions(webDriverReference);
     WebElement mainMenu = webDriverReference.findElement(By.linkText("mainMenuLink"));
     action.moveToElement(mainMenu).moveToElement(webDriverReference.findElement(By.linkText("subMenuLink"))).click().build().perform();

>Advance level: Using EventFiringWebdriver and EventFiringMouse classes

       From selenium API, we can find that we have already provided with listeners so, what we need to do now is to simply implementing / extending those interface / classes respectively.

       For the time being, I will call a class, and say it is 'TestMouseEventListener' which extends 'AbstractWebDriverEventListener'. Notable thing here with API is, 'AbstractWebDriverEventListener' class is already have all implemented methods of 'WebDriverEventListener' interface, inorder to access these methods we need a reference, and since 'AbstractWebDriverEventListener' is an abstract class, we are creating our own class 'TestMouseEventListener' by extending it

        /**
          * Custom listener class
          */
           public class TestMouseEventListener extends AbstractWebDriverEventListener {
           }

        Now, inorder to click a web element, we need to register the event firing web driver and then use it, simple isn't it.. 

           /**
             *   part of implemented code
              **/
           TestMouseEventListener tmel = new TestMouseEventListener();
           EventFiringWebDriver efwd = new EventFiringWebDriver(webDriverReference);
           efwd.register(tmel);

           EventFiringMouse efm = new EventFiringMouse(webDriverReference, tmel);
           
           Locatable locator = (Locatable)webElement;
           Coordinates coordinates = locator.getCoordinates();
           //Move mouse to web element coordinates
           efm.mouseMove(coordinates);
           sleep(5.0);



  

No comments:

Post a Comment