Facebook

For any corporate training requirements Please mail To sudhakar@qtpsudhakar.com
Course Name Date Time Duration Type Location
An important update from QtpSudhakar. I stopped giving classroom training in MindQ Systems. I have bigger plans and I want to put an end to regular classroom training because very less people are practicing. From now onwards every session of mine is going to have explanation and practice. Everyone should practice in presence of mine and should go out of classroom with practical knowledge. So I started my own institute AnyTechLabs in Madhapur. Please reach out to +91-8328152362 for course details. Visit AnyTechLabs.com for address details.
Selenium + Cucumber 22nd September 9 AM IST to 2.00 PM IST 4 WeekEnds Theory + Practical AnyTechLabs
Selenium + Cucumber 8th October 7.30 AM IST to 9.00 AM IST 45 Days Theory+Practical AnyTechLabs

A book "Cracking the QTP interview" authored by Sudhakar.Read More. Buy From FlipKart

Monday, December 26, 2016

Xpath Tester for Selenium

import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class IEXpath {
 static WebDriver driver;

 public static void main(String[] args) {

  // configure browser driver
  System.setProperty("webdriver.ie.driver",
    "F:\\SeleniumSoftware\\BrowserDrivers\\IEDriverServer64.exe");

  // opens new window
  driver = new InternetExplorerDriver();
  driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
  // navigates to url
  driver.get("http://facebook.com/");

  xpathTester(driver);

  // prints opened page title
  System.out.println(driver.getTitle());

  // close the browser
  //driver.quit();

 }

 public static void xpathTester(WebDriver driver) {
  
  //intialize scanner and javascript executor
  Scanner scn = new Scanner(System.in);
  JavascriptExecutor js = (JavascriptExecutor) driver;
  
  String elmFind = "y";

  while (elmFind.toLowerCase().equalsIgnoreCase("y")) {
   //ask for xpath
   System.out.println("give element xpath");
   String elmXpath = scn.next();
   
   //to find hidden element count
   int hiddenElm = 0;
   
   //get list of elements using the given xpath
   List< WebElement> lstElm = driver.findElements(By.xpath(elmXpath));

   //highlights all elements that are found with the given xpath
   for (int elmIndex = 0; elmIndex < lstElm.size(); elmIndex++) {
    js.executeScript("arguments[0].style.border='5px dotted red'",
      lstElm.get(elmIndex));
    
    //for counting hidden elements
    if (lstElm.get(elmIndex).isDisplayed()) {
     hiddenElm++;
    }
   }

   //get the element count
   int nElm = lstElm.size();
   //print elements count
   System.out.println("Number of Elelemnts Found:" + nElm);
   
   //print hidden element count
   System.out.println("Number of Elelemnts Hidden:" + hiddenElm);
   System.out.println("See the highlighted elements in Application");

   //ask for test again
   System.out.println("Do you want to Test Again? \n Type 'y'");
   elmFind = scn.next();

   //if 'y' enetered reset highlighting
   if (elmFind.toLowerCase().equalsIgnoreCase("y")) {
    for (int elmIndex = 0; elmIndex < lstElm.size(); elmIndex++) {

     js.executeScript("arguments[0].style.border=''",
       lstElm.get(elmIndex));
    }
   }
  }
 }
}

No comments :

Post a Comment