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);


  

Code snippet ready-made...

To get web element properties:


I am assuming you have already driver and element initialized in hand, then the following code is used to read the element properties 

/**
 * Return properties list of an element
 */
public static Object getElementProperties(WebElement element){
    Object object = ((JavascriptExecutor)driver).executeScript("var item{};  " + 
                                 " for (index = 0; index < argument[0].attributes.length(); ++index)  { " +
                                 " items[arguments[0].attributes[index].name = arguments[0].  " +                                                              " attributes[index]. value}; return items; ", element)
    return object;
}

Exception & Solutions

Exceptions & Solutions:

           Apparently, as a junior programmer, one can write the code but an experience programmer can easily debug whereas an expert can implement code to avoid run time exceptions., nevertheless we will face exceptions at some point of time.
          Objective of this page is to make familiar with some exceptions I dealt with the possible solutions I got. One more time, this is the possible solutions I found, perhaps your exception may differ in case to occur.
Sharing here may useful to some one or may be it will hold as a record for me to revise later point of time :D

However, see some of the exceptions here also.


Handling SSL Certificates


How to handle SSL Certificates ..??

This Exception may be common to all , but my fingers crossed when I see the next wizard, as shown in second image, 

Image 1:  This Connection is Untrusted
If we click 'Add Exception' button in the first page, then the next wizard comes up as shown below

Image 2: Add Security Exception

After a long search on some forums, I found that a small piece of code are not explained by most of the them or may be I come across most of such blogs. However, I thought of to put what I have discovered and handled this issue and where I was missing or may be you are missing the actual explanation in other blogs

Apparently, handling this ssl certificate can be achieved on firefox browser only, because it allows users to access the profile and where as other browsers doesn't have option for creating custom browser profile, if any one knows, please let me know in comments

Now, before explaining code, I want to explain how to create custom firefox profile then the code might be easily understands at its a very few in lines. 

How to create custom firefox profile?

1) Close all firefox browser windows and run the following command
firefox -ProfileManager -no-remote
2) Click "Create Profile"  and follow the instructions on the wizard
3) You can create with any name, as I created with "QAAutomation" as shown in below image

"firefox -ProfileManager -no-remote" to choose firefox profile

This is how we can create profiles in firefox browser, now we have to use this profile in our selenium webdirver code, as follows

#Java Program:

Method 1:
/**
 * Handle Untrusted HTTPs Connections With Selenium web driver. Also, handle an
 * additional wizards like, 'Add Security Exception' by Confirm Security
 * Exception
 * 
 * @author Fayaz
 * 
 */
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;


public class HandleWindows {

public static void main(String[] args){
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffProfile = profile.getProfile("QAAutomation"); // firefox profile **
ffProfile.setAcceptUntrustedCertificates(true);
ffProfile.setAssumeUntrustedCertificateIssuer(false);
WebDriver driver = new FirefoxDriver(ffProfile);
driver.get(URL);
}
}


** Most of the blogs are pasted code as it is, where some says, "HandleSSLCertificate" but they don't explain what is this "HandleSSLCertificate", whereas others explains simply as "firefox user profile name" (but they missed how to create profile, indeed)


Remember most of us use "default" as a profile name, by default. So please pass "default" as an parameter to getProfile() and try if you want, that is new ProfilesIni().getProfile("default");

Method 2:

/**
 * Handle Untrusted HTTPs Connections With Selenium web driver. Also, handle an
 * additional wizards like, 'Add Security Exception' by Confirm Security
 * Exception
 *
 * @author Fayaz
 *
 */
import java.io.File;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;


public class HandleWindows {

 public static void main(String[] args){
 FirefoxProfile ffProfile = new FirefoxProfile(new File("C:\\Users\\Fayaz\\Desktop\\Selenium\\custom")); // firefox profile**
  ffProfile.setAcceptUntrustedCertificates(true);
  ffProfile.setAssumeUntrustedCertificateIssuer(false);
  WebDriver driver = new FirefoxDriver(ffProfile);
  driver.get(URL);

 }
}

** While creating a firefox profile I created a new folder with name 'custom'. That is though "QAAutomation" is actual firefox profile name, it is created under 'custom' directory. so custom is a my favorite directory on my machine where as QAAutomation is a firefox profile name


Finally, those who wants to execute test from command line interface aka CLI, then the command should be like, 
java -jar selenium-server.jar -firefoxProfileTemplate "C:\Users\Fayaz\Desktop\Selenium",
instead  of the giving the whole default path like, 
java -jar selenium-server.jar -firefoxProfileTemplate "C:\Documents and Settings\Fayaz\Application Data\Mozilla\Firefox\Profiles\io4kgk8d.default"
That's all we are done. For more info see Tips & Tricks page

Chrome :

Though by the time of writing this blog page, I didn't find the similar way, how we are handling dialog firefox browser. However, one way I used to handle this certificate dialog while using chrome is using Threads in java

Before, you read further, let me tell you what my issue was

When I request to an URL by calling our selenium WebDrivers, get(String URL) method and before the response gets back, certificate dialog is appearing and until I select the certificate the URL page isn't loading.
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Fayaz\\Desktop\\chromedriver.exe");
WebDriver driver = new ChromeDriver(); 
driver.get(URL); // this method execution is not finishing until I select a dialog in the //certificate dialog
driver.findElements(By.cssSelector("input#username")); //this code is not at all reaching

 So, inorder to handle this, what I did was, I was calling a Thread in which using Robot class, making enter key to press. That is, start a thread and make it to sleep for a while then do request to URL from driver.get() method, by the time certificate dialog appears our Thread process should finishes its sleep time and then start the thread to run, where in run() method, our Robot class hit the enter button

Here is the working code for me, how it works....        

#Java Program:

package selenium.project;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * Handle Untrusted HTTPs Connections With Selenium web driver. Also, handle an
 * additional wizards like, 'Add Security Exception' by Confirm Security
 * Exception
 *
 * @author Fayaz
 *
 */
public class HandleWindows {

private static final String URL = "<your URL here> ";

public static void main(String[] args) {
WebDriver driver = null;
try {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Fayaz\\Downloads\\chromedriver.exe");
driver = new ChromeDriver();
Thread certSelectionThread = null;
Runnable r = new Runnable() {

@Override
public void run() {
try {
Thread.sleep(1000 * 10);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
certSelectionThread = new Thread(r);
certSelectionThread.start();
driver.get(URL);
if(certSelectionThread != null){
try {
certSelectionThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
if(driver != null && !driver.toString().contains("null")){
driver.close();
driver.quit();
}
}
}
}



Sometime we need to handle certificate issues (SSL) with chrome as well especially for self-signed certificates. else we get exception, which says as follows

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1351)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:156)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:925)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:860)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1043)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:728)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:138)



when we run the following piece of code

public static boolean waitUntilRequestCompletes(String urlRequest) {
try {
URL url = new URL(urlRequest);
HttpURLConnection conn;
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");

if (conn.getResponseCode() != 200) {
throw new RuntimeException(" HTTP error code : "
+ conn.getResponseCode());
}

Scanner scan = new Scanner(url.openStream());
String entireResponse = new String();
while (scan.hasNext()) {
entireResponse += scan.nextLine();
System.out.println("Response : " + entireResponse);
}
scan.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}


To avoid such exception it is important to add signed certificate to JVM - security\cacerts.

How to do that.?

So from the insecure URL download the certificate, follow this page to know how to download certificate

Once the certificate in place, open command prompt in admin mode and run the following command

keytool -import -alias example_name -keystore "C:\Program Files\Java\jdk1.8.0_121\jre\lib\security\cacerts" -file "C:\Users\Fayaz\Desktop\ss_certificate\pd-123456789.cer"

That's it done. happy secure connection :)

Selenium Grid

Selenium Grid:

Well, I have experienced the following scenarios while setting up the grid environment for the first time. 

Here it is, I have two machine, say '10.19.137.247' (test machine) and 192.168.1.113 (test dev machine) so to start the hub on test machine I have run the following command:

On Test Machine:

    java -jar selenium-server-standalone.jar -role hub
The following message or information appeared on command line as a result
Mar 10, 2014 2:57:03 AM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a selenium grid server
2014-03-10 02:57:26.332:INFO:osjs.Server:jetty-7.x.y-SANPSHOT
2014-03-10 02:57:26.395:INFO:osjsh.ContextHandler:started o.s.j.s.ServletContextHandler{/,null}
2014-03-10 02:57:26.410:INFO:osjs.AbstractConnector:Started SocketConnector@0.0.0.0:4444     

On Test Dev machine:

 java -jar selenium-server-standalone -Dwebdriver.chrome.driver=C:\Users\Fayaz\Downloads\chromedriver.exe -role webdriver -hub http://10.19.137.247/grid/register
The following message or information appeared on command line as a result
Mar 10,  2014 12:30:36 PM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a selenium grid node
Mar 10, 2014 12:30:39 PM org.openqa.grid.internal.utils.SelfRegisteringRemote startRemoteServer
WARNING: error getting the parameters from the hub. The node may end up with wrong timeouts. Connection refused: connect
12:30:39.255 INFO - Java: Oracle Corportation 24.45-b08
12:30:39.263 INFO - OS:Windows 7 6.1 x86
12:30:39.295 INFO - v2.39.0, with Core v2.39.0. Built from revision ff23eac
12:30:39.565 INFO - Default driver org.openqa.selenium.iphone.IPhoneDriver registration is skipped: registration capabilities Capabilites [{platform=MAC, browserName=iPhone, version=}] does not match with current platform: VISTA
12:30:39:565 INFO - Default driver org.openqa.selenium.iphone.IPhoneDriver registration is skipped:
registration capabilities Capabilities [{platform=MAC, browserName=iPad, version=}] does not match with current platform: VISTA
12:30:39.648 INFO - RemoteWebDriver instances should connect to : http://127.0.0.1:5555/wd/hub
12:30:39.649 INFO - Version Jetty/5.1.x
12:30:39.650 INFO - Started HttpContext[/selenium-server/driver./selenium-server/driver]
12:30:39.651 INFO - Started HttpContext[/selenium-server,/selenium-server]
12:30:39:652 INFO - Started HttpContext[/,/]
12:30:39:654 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@12132a6
12:30:39.654 INFO - Started HttpContext[/wd,/wd]
12:30:39.657 INFO - Started SocketListener on 0.0.0.0:5555
12:30:39.658 INFO - Started Started org.openqa.jetty.jetty.Server@a4b9da
12:30:39.661 INFO - using the json request : {"class":"org.openqa.grid.common.RegistrationRequest","capabilities":[{"platform":"VISTA","seleniumProtocol":"Selenium","browserName":"*firefox","maxInstances":5},{"platform":"VISTA","seleniumProtocol":"Selenium","browserName":"*googlechrome","maxInstances":5},{"platform":"VISTA","seleniumProtocol":"seleniumProtocol":"WebDriver","browserName":"chrome","maxInstances":1},{"platform":"VISTA","seleniumProtocol":"WebDriver","browserName":"internet explorer","maxInstances":1}],"configuration":{"port":5555,"register":true,"host":"192.168.1.113","proxy":"org.openqa.grid.selenium.proxy.DefaultRemoteProxy","maxSession":5,"role":"node","hubHost":"10.19.137.247","registerCycle":5000,"hub":"http://10.19.137.247/grid/register","hubPort":-1,"url":"http://192.168.1.113:5555","remoteHost":"http://192.168.1.113:5555"}}
12:30:39.664 INFO - Starting auto register thread. Will try to register every 5000 ms.
12:30:39.664 INFO - Registering the node to hub :http://10.19.137.247:-1/grid/register
12:30:41.466 INFO - couldn't register this node : Error sending the registration request.
12:30:48.264 INFO - couldn't register this node : Hub is down or not responding: Connection refused: connect
If we observer on the test machine to verify the status of the selenium web server whether the client / node is connected or not, in other words when no node connects to hub using selenium grid, it will looks like in the screen shot

When no nodes connects to a hub using selenium grid then the server details displayed


This error has been resolved after I corrected the command to connect the hub from my test dev machine, and the changed command is
java -jar selenium-server-standalone.jar -Dwebdriver.chrome.driver=C:\Users\Fayaz\Downloads\chromedriver.exe -role webdriver -hub http://10.19.137.247:4444/grid/register

The difference here is I am using port number ':4444' after test machine URL

After connecting to the server, we can verify the status of the selenium web server whether the client / node is connected or not, it will looks like in the screen shot


When a node connects to hub using selenium grid then the list of details displayed


The path in browser to see list of clients or nodes connected to a hub at server / hub side is,
 localhost:4444/grid/console
For a beginner most common and exception can occur as follows, part of exception pasted
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;
For more info please find the source here

To handle ssl or certification errors on a remote browser, I have used the following code and it worked like a charm

For IE or Chrome:
  remoteWebdriver.navigate().to("javascript:document.getElementById('overridelink').click()");
//I assume you have already had a wed driver reference called 'remoteWebdriver'


Now, to take screen shot of the remote test script execution, you can find the code in this selenium webdriver cheat code or from here

Update :


From the latest release of selenium 3.x, if we use above command to register client to remote hub then we get the following exception

Exception in thread "main" com.beust.jcommander.ParameterException: Unknown option: -Dwebdriver.chrome.driver=C:\Users\Fayaz\Downloads\chromedriver.exe
        at com.beust.jcommander.JCommander.parseValues(JCommander.java:742)
        at com.beust.jcommander.JCommander.parse(JCommander.java:282)
        at com.beust.jcommander.JCommander.parse(JCommander.java:265)
        at com.beust.jcommander.JCommander.<init>(JCommander.java:210)
        at org.openqa.grid.selenium.GridLauncherV3$3.setConfiguration(GridLauncherV3.java:267)
        at org.openqa.grid.selenium.GridLauncherV3.buildLauncher(GridLauncherV3.java:155)
        at org.openqa.grid.selenium.GridLauncherV3.main(GridLauncherV3.java:75)




So the command to register a node / client is changed as follows:

                        java -jar selenium-server-standalone-3.3.1.jar -role node -hub http://<hub_ip>:4444/grid/register -port 4444

Browser Functions

Handling Browser Functionality

Scroll Down:

/**
 * Scroll to the bottom of a page and sleep for 10 seconds
 * 
 */
public static void scrollDown() {
((JavascriptExecutor) SeleniumAction.getDriver())
.executeScript("scroll(0, 250)");
sleep(10); // Customized sleep method
}

Scroll Up:

/**
 * Scroll to the top of a page and sleep for 10 seconds
 * 
 */
public static void scrollUp() {
((JavascriptExecutor) SeleniumAction.getDriver())
.executeScript("scroll(250, 0)");
sleep(10); // Customized sleep method
}


Scroll Upto an Element:

/**
 * Scroll to an web element to make it display on screen 
 *  
 * @param element
 */
public static void scrollToAnElement(WebElement element) {
if (isElementExists(element)) {
((Locatable) element).getCoordinates().inViewPort();
sleep(5); // Customized sleep method
}

}

Maximize a web browser:
/**
* Maximize a browser window
*/
public void maximizeBrowserWindow(){
       driver.manage().window().maximize();
}

Refresh a browser:
/**
* Refresh a browser window
*/
//Method #1:
public void refreshBrowserWindow(){
       driver.navigate().refresh();
}

//Method #2:
public void refreshBrowserWindow(){
       driver.get(driver.getCurrentUrl());
}

// Method #3:
public void refreshBrowserWindow(){
      driver.navigate().to(driver.getCurrentUrl());  
}

//Method #4:
public void refreshBrowserWindow(WebDriver driver){
      Actions refreshAction = new Actions(driver);
      refreshAction.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
}

Handling an alert:
/**
 * Handling an alert dialog 
 * 
 *@param discard
 */
public void handleAlert(boolean discard){
     Alert alertHandler =    driver.switchTo().alert();
     if(discard){
          alertHandler.dismiss();
     } else {
           alertHandler.accept();
     }
}

Take a screenshot:

/**
 * Takes a screen shot and save with the given file name
 *  
 * @param fileName
 * @throws IOException
 */

public static void takeScreenshot(String fileName) throws IOException {
File screenshot = ((TakesScreenshot) SeleniumAction.getDriver())
.getScreenshotAs(OutputType.FILE);
String dirPath = ScriptsExecuted.getRFTLogDirectory()
+ fileName
+ new SimpleDateFormat("MM-dd-yyy_HH:ss")
.format(new GregorianCalendar().getTime() + ".jpg");
FileUtils.moveFile(screenshot, new File(dirPath));
}

// For taking a screen shot on remote web driver, source

/**
  *  RemoteWebDriver does not implement the TakesScreenshot class
  *  if the driver does have the Capabilities to take a screenshot

  *  then Augmenter will add the TakesScreenshot methods to the instance
  */
public void myTest() throws Exception {
        WebDriver driver = new RemoteWebDriver(
                                new URL("http://localhost:4444/wd/hub"), 
                                DesiredCapabilities.firefox());
        
        driver.get("http://www.google.com");
        WebDriver augmentedDriver = new Augmenter().augment(driver);
        File screenshot = ((TakesScreenshot)augmentedDriver).
                            getScreenshotAs(OutputType.FILE);
}


Verification of an element existence:

/**
 * Method to verify whether desired web element is exists or not by handling
 * exceptions
 * 
 * @param element
 * @return
 */
public static boolean isElementExists(WebElement element) {
boolean result = false;
try {
if (element != null) {
result = element.isDisplayed();
}
} catch (NoSuchElementException noElementExecption) {
result = false;
} catch (StaleElementReferenceException staleElementException) {
return false;
}
return result;
}


Wait For page to load

void waitForLoad(WebDriver driver) {
    ExpectedCondition<Boolean> pageLoadCondition = new
        ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
        };
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(pageLoadCondition);

}


Validate Image Loaded properly

/**
  * Import statements, 
  */
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

public void validateInvalidImages() {
try {
invalidImageCount = 0;
List<WebElement> imagesList = driver.findElements(By.tagName("img"));
System.out.println("Total no. of images are " + imagesList.size());
for (WebElement imgElement : imagesList) {
if (imgElement != null) {
verifyimageActive(imgElement);
}
}
System.out.println("Total no. of invalid images are " + invalidImageCount);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}

public void verifyimageActive(WebElement imgElement) {
try {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(imgElement.getAttribute("src"));
HttpResponse response = client.execute(request);
// verifying response code he HttpStatus should be 200 if not,
// increment as invalid images count
if (response.getStatusLine().getStatusCode() != 200)
invalidImageCount++;
} catch (Exception e) {
e.printStackTrace();
}

}

Other randomzz


Windows 'Run' commands:


Accessibility Controlsaccess.cpl
Accessibility Wizardaccwiz
Add Hardware Wizardhdwwiz.cpl
Add/Remove Programsappwiz.cpl
Administrative Toolscontrol admintools
Adobe Acrobat (if installed)acrobat
Adobe Designer (if installed)formdesigner
Adobe Distiller (if installed)acrodist
Adobe ImageReady (if installed)imageready
Adobe Photoshop (if installed)photoshop
Automatic Updateswuaucpl.cpl
Bluetooth Transfer Wizardfsquirt
Calculatorcalc
Certificate Managercertmgr.msc
Character Mapcharmap
Check Disk Utilitychkdsk
Clipboard Viewerclipbrd
Command Promptcmd
Component Servicesdcomcnfg
Computer Managementcompmgmt.msc
Control Panelcontrol
Date and Time Propertiestimedate.cpl
DDE Sharesddeshare
Device Managerdevmgmt.msc
Direct X Control Panel (if installed)*directx.cpl
Direct X Troubleshooterdxdiag
Disk Cleanup Utilitycleanmgr
Disk Defragmentdfrg.msc
Disk Managementdiskmgmt.msc
Disk Partition Managerdiskpart
Display Propertiescontrol desktop
Display Propertiesdesk.cpl
Display Properties (w/Appearance Tab Preselected)control color
Dr. Watson System Troubleshooting Utilitydrwtsn32
Driver Verifier Utilityverifier
Event Viewereventvwr.msc
Files and Settings Transfer Toolmigwiz
File Signature Verification Toolsigverif
Findfastfindfast.cpl
Firefox (if installed)firefox
Folders Propertiesfolders
Fontscontrol fonts
Fonts Folderfonts
Free Cell Card Gamefreecell
Game Controllersjoy.cpl
Group Policy Editor (XP Prof)gpedit.msc
Hearts Card Gamemshearts
Help and Supporthelpctr
HyperTerminalhypertrm
Iexpress Wizardiexpress
Indexing Serviceciadv.msc
Internet Connection Wizardicwconn1
Internet Exploreriexplore
Internet Propertiesinetcpl.cpl
Internet Setup Wizardinetwiz
IP Configuration (Display Connection Configuration)ipconfig /all
IP Configuration (Display DNS Cache Contents)ipconfig /displaydns
IP Configuration (Delete DNS Cache Contents)ipconfig /flushdns
IP Configuration (Release All Connections)ipconfig /release
IP Configuration (Renew All Connections)ipconfig /renew
IP Configuration (Refreshes DHCP & Re-Registers DNS)ipconfig /registerdns
IP Configuration (Display DHCP Class ID)ipconfig /showclassid
IP Configuration (Modifies DHCP Class ID)ipconfig /setclassid
Java Control Panel (if installed)jpicpl32.cpl
Java Control Panel (if installed)javaws
Keyboard Propertiescontrol keyboard
Local Security Settingssecpol.msc
Local Users and Groupslusrmgr.msc
Logs You Out Of Windowslogoff
Malicious Software Removal Toolmrt
Microsoft Access (if installed)msaccess
Microsoft Chatwinchat
Microsoft Excel (if installed)excel
Microsoft Frontpage (if installed)frontpg
Microsoft Movie Makermoviemk
Microsoft Paintmspaint
Microsoft Powerpoint (if installed)powerpnt
Microsoft Word (if installed)winword
Microsoft Syncronization Toolmobsync
Minesweeper Gamewinmine
Mouse Propertiescontrol mouse
Mouse Propertiesmain.cpl
Nero (if installed)nero
Netmeetingconf
Network Connectionscontrol netconnections
Network Connectionsncpa.cpl
Network Setup Wizardnetsetup.cpl
Notepadnotepad
Nview Desktop Manager (if installed)nvtuicpl.cpl
Object Packagerpackager
ODBC Data Source Administratorodbccp32.cpl
On Screen Keyboardosk
Opens AC3 Filter (if installed)ac3filter.cpl
Outlook Expressmsimn
Paintpbrush
Password Propertiespassword.cpl
Performance Monitorperfmon.msc
Performance Monitorperfmon
Phone and Modem Optionstelephon.cpl
Phone Dialerdialer
Pinball Gamepinball
Power Configurationpowercfg.cpl
Printers and Faxescontrol printers
Printers Folderprinters
Private Character Editoreudcedit
Quicktime (If Installed)QuickTime.cpl
Quicktime Player (if installed)quicktimeplayer
Real Player (if installed)realplay
Regional Settingsintl.cpl
Registry Editorregedit
Registry Editorregedit32
Remote Access Phonebookrasphone
Remote Desktopmstsc
Removable Storagentmsmgr.msc
Removable Storage Operator Requestsntmsoprq.msc
Resultant Set of Policy (XP Prof)rsop.msc
Scanners and Camerassticpl.cpl
Scheduled Taskscontrol schedtasks
Security Centerwscui.cpl
Servicesservices.msc
Shared Foldersfsmgmt.msc
Shuts Down Windowsshutdown
Sounds and Audiommsys.cpl
Spider Solitare Card Gamespider
SQL Client Configurationcliconfg
System Configuration Editorsysedit
System Configuration Utilitymsconfig
System File Checker Utility (Scan Immediately)sfc /scannow
System File Checker Utility (Scan Once At The Next Boot)sfc /scanonce
System File Checker Utility (Scan On Every Boot)sfc /scanboot
System File Checker Utility (Return Scan Setting To Default)sfc /revert
System File Checker Utility (Purge File Cache)sfc /purgecache
System File Checker Utility (Sets Cache Size to size x)sfc /cachesize=x
System Informationmsinfo32
System Propertiessysdm.cpl
Task Managertaskmgr
TCP Testertcptest
Telnet Clienttelnet
Tweak UI (if installed)tweakui
User Account Managementnusrmgr.cpl
Utility Managerutilman
Windows Address Bookwab
Windows Address Book Import Utilitywabmig
Windows Backup Utility (if installed)ntbackup
Windows Explorerexplorer
Windows Firewallfirewall.cpl
Windows Magnifiermagnify
Windows Management Infrastructurewmimgmt.msc
Windows Media Playerwmplayer
Windows Messengermsmsgs
Windows Picture Import Wizard (need camera connected)wiaacmgr
Windows System Security Toolsyskey
Windows Update Launcheswupdmgr
Windows Version (to show which version of windows)winver
Windows XP Tour Wizardtourstart
Wordpadwrite

MS Windows Keyboard short(cuts)

More than 100 Keyboard Shortcuts must read (Microsoft Windows) 
1. CTRL+C (Copy)
2. CTRL+X (Cut) ... 
3. CTRL+V (Paste)
4. CTRL+Z (Undo)
5. DELETE (Delete)
6. SHIFT+DELETE (Delete the selected item permanently without placing the item in the Recycle Bin)
7. CTRL while dragging an item (Copy the selected item)
8. CTRL+SHIFT while dragging an item (Create a shortcut to the selected item)
9. F2 key (Rename the selected item) 
10. CTRL+RIGHT ARROW (Move the insertion point to the beginning of the next word)
11. CTRL+LEFT ARROW (Move the insertion point to the beginning of the previous word)
12. CTRL+DOWN ARROW (Move the insertion point to the beginning of the next paragraph)
13. CTRL+UP ARROW (Move the insertion point to the beginning of the previous paragraph)
14. CTRL+SHIFT with any of the arrow keys (Highlight a block of text) SHIFT with any of the arrow keys (Select more than one item in a window or on the desktop, or select text in a document) 15. CTRL+A (Select all)
16. F3 key (Search for a file or a folder)
17. ALT+ENTER (View the properties for the selected item)
18. ALT+F4 (Close the active item, or quit the active program)
19. ALT+ENTER (Display the properties of the selected object)
20. ALT+SPACEBAR (Open the shortcut menu for the active window)
21. CTRL+F4 (Close the active document in programs that enable you to have multiple documents opensimultaneously)
22. ALT+TAB (Switch between the open items)
23. ALT+ESC (Cycle through items in the order that they had been opened)
24. F6 key (Cycle through the screen elements in a window or on the desktop)
25. F4 key (Display the Address bar list in My Computer or Windows Explorer)
26. SHIFT+F10 (Display the shortcut menu for the selected item)
27. ALT+SPACEBAR (Display the System menu for the active window)
28. CTRL+ESC (Display the Start menu)
29. ALT+Underlined letter in a menu name (Display the corresponding menu) Underlined letter in a command name on an open menu (Perform the corresponding command)
30. F10 key (Activate the menu bar in the active program)
31. RIGHT ARROW (Open the next menu to the right, or open a submenu)
32. LEFT ARROW (Open the next menu to the left, or close a submenu)
33. F5 key (Update the active window)
34. BACKSPACE (View the folder onelevel up in My Computer or Windows Explorer)
35. ESC (Cancel the current task)
36. SHIFT when you insert a CD-ROMinto the CD-ROM drive (Prevent the CD-ROM from automatically playing) 

Dialog Box - Keyboard Shortcuts 

1. CTRL+TAB (Move forward through the tabs)
2. CTRL+SHIFT+TAB (Move backward through the tabs)
3. TAB (Move forward through the options)
4. SHIFT+TAB (Move backward through the options)
5. ALT+Underlined letter (Perform the corresponding command or select the corresponding option) 6. ENTER (Perform the command for the active option or button)
7. SPACEBAR (Select or clear the check box if the active option is a check box)
8. Arrow keys (Select a button if the active option is a group of option buttons)
9. F1 key (Display Help)
10. F4 key (Display the items in the active list)
11. BACKSPACE (Open a folder one level up if a folder is selected in the Save As or Open dialog box) 

Microsoft Natural Keyboard Shortcuts:

1. Windows Logo (Display or hide the Start menu) 
2. Windows Logo+BREAK (Display the System Properties dialog box)
3. Windows Logo+D (Display the desktop)
4. Windows Logo+M (Minimize all of the windows)
5. Windows Logo+SHIFT+M (Restorethe minimized windows)
6. Windows Logo+E (Open My Computer)
7. Windows Logo+F (Search for a file or a folder)
8. CTRL+Windows Logo+F (Search for computers)
9. Windows Logo+F1 (Display Windows Help)
10. Windows Logo+ L (Lock the keyboard)
11. Windows Logo+R (Open the Run dialog box)
12. Windows Logo+U (Open Utility Manager)
13. Accessibility Keyboard Shortcuts
14. Right SHIFT for eight seconds (Switch FilterKeys either on or off)
15. Left ALT+left SHIFT+PRINT SCREEN (Switch High Contrast either on or off)
16. Left ALT+left SHIFT+NUM LOCK (Switch the MouseKeys either on or off)
17. SHIFT five times (Switch the StickyKeys either on or off)
18. NUM LOCK for five seconds (Switch the ToggleKeys either on or off)
19. Windows Logo +U (Open Utility Manager)
20. Windows Explorer Keyboard Shortcuts
21. END (Display the bottom of the active window)
22. HOME (Display the top of the active window)
23. NUM LOCK+Asterisk sign (*) (Display all of the subfolders that are under the selected folder) 24. NUM LOCK+Plus sign (+) (Display the contents of the selected folder) 

MMC Console keyboard shortcuts 

1. SHIFT+F10 (Display the Action shortcut menu for the selected item)
2. F1 key (Open the Help topic, if any, for the selected item) 
3. F5 key (Update the content of all console windows)
4. CTRL+F10 (Maximize the active console window)
5. CTRL+F5 (Restore the active console window)
6. ALT+ENTER (Display the Properties dialog box, if any, for theselected item)
7. F2 key (Rename the selected item)
8. CTRL+F4 (Close the active console window. When a console has only one console window, this shortcut closes the console) 

 Remote Desktop Connection Navigation

1. CTRL+ALT+END (Open the Microsoft Windows NT Security dialog box)
2. ALT+PAGE UP (Switch between programs from left to right)
3. ALT+PAGE DOWN (Switch between programs from right to left)
4. ALT+INSERT (Cycle through the programs in most recently used order)
5. ALT+HOME (Display the Start menu)
6. CTRL+ALT+BREAK (Switch the client computer between a window and a full screen)
7. ALT+DELETE (Display the Windows menu)
8. CTRL+ALT+Minus sign (-) (Place a snapshot of the active window in the client on the Terminal server clipboard and provide the same functionality as pressing PRINT SCREEN on a local computer.)
9. CTRL+ALT+Plus sign (+) (Place asnapshot of the entire client window area on the Terminal server clipboardand provide the same functionality aspressing ALT+PRINT SCREEN on a local computer.)

Microsoft Internet Explorer Keyboard Shortcuts

1. CTRL+B (Open the Organize Favorites dialog box)
2. CTRL+E (Open the Search bar)
3. CTRL+F (Start the Find utility)
4. CTRL+H (Open the History bar)
5. CTRL+I (Open the Favorites bar)
6. CTRL+L (Open the Open dialog box)
7. CTRL+N (Start another instance of the browser with the same Web address)
8. CTRL+O (Open the Open dialog box,the same as CTRL+L)
9. CTRL+P (Open the Print dialog box)
10. CTRL+R (Update the current Web page)
11. CTRL+W (Close the current window)
12. CTRL+D (save current URL as a bookmark)





** Perhaps, this page from this blog will remove soon...