Friday, April 27, 2018

How to click on hidden element with Selenium WebDriver ?

How to click on hidden element with Selenium WebDriver ?

Solution :

It is very hot question asked in the Automation Interview.

Here, I will show you how you can click on Hidden Element with Selenium & Javascript.

For interacting with Hidden Elements we need to use "JavascriptExecutor" Interface.


Now, I need to perform click operation on the button which is disabled in DOM by using style=”display: none;”

Lets first create the instance of WebDriver :

WebDriver driver;
System.setProperty("webdriver.chrome.driver", "C:/Users/User/Downloads/chromedriver/chromedriver.exe");
driver = new ChromeDriver();
Next, we will open the url and find the WebElement i.e Button.

driver.get("file:///C:/Users/User/Desktop/Blog/Hidden.html");
WebElement btn = driver.findElement(By.cssSelector("button[id='myButton']"));
Here, we will create the object of "JavascriptExecutor" Interface and cast our WebDriver to "JavascriptExecutor".

After that we will use executeScript Method of JavascriptExecutor which will perform our task i.e "Click on hidden button".

JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;
jsExecutor.executeScript("arguments[0].click();", btn);
Hope this will help you. Please share your views in Comment section below.

Thanks a lot.

How to get the color code of dropdown text with Selenium WebDriver ?

How to get the color code of dropdown text with Selenium WebDriver ?

Solution :

Lets first create the instance of WebDriver :

WebDriver driver;
System.setProperty("webdriver.chrome.driver", "C:/Users/User/Downloads/chromedriver/chromedriver.exe");
driver = new ChromeDriver();


Next, we will open the url and find the WebElement i.e dropdown.

driver.get("file:///C:/Users/User/Desktop/Blog/ColorText.html");
WebElement dropDown = driver.findElement(By.cssSelector("select[class='selectDiv']"));
Now, create the instance of "Select" class and click on the option "Audi" from dropdown
by using "selectByVisibleText()" method.

Select select = new Select(dropDown);
select.selectByVisibleText("Audi");
Now, we will get the selected option color code and by using "Color" class of java we will convert the rgb color code to hex color code.

String colorValue = select.getFirstSelectedOption().getCssValue("color");
String hexColorCode = Color.fromString(colorValue).asHex();
System.out.println("Selected Option color code is " + hexColorCode );
Hope you like this post. Thanks

How to handle a Web Table with Selenium Web Driver?

How to handle a Web Table with Selenium Web Driver?


Solution:

Web Table : A table is collection of rows and columns, table created on web is known as Web Table.
The <table> tag defines the HTML table where we use the <tr> tag for rows , the <td> tag for columns and the element defines a table header.

There are two types of Web Table :

a). Static Table :  A table where number of rows and number of columns are fixed. E.g :- Days of week table.

b). Dynamic Table : A table where number of rows and number of columns are dynamic means it may vary as per data. E.g:- Match Score Card and Organisational Sales Chart.

We will learn the following things now:

1). How to print all the data from table?
2). How to print data of specific column ?
3). How to print data of specific row?
4). How to print data of specific cell based data of another cell?
5.) How to find & print sum of all of values of column?.
6.) How to find the shares whose Current Price is less than Rs. 800?

Let Say we have a following table.
CompanyGroupPrev Close (Rs)Current Price (Rs)% Change
L&T Infotech A 1,539.15 1,716.85 + 11.55
Indiabulls Ventures A 428.70 470.40 + 9.73
NIIT Technologies A 998.55 1,080.90 + 8.25
Raymond Ltd A 1,065.50 1,132.35 + 6.27
Persistent Systems A 725.85 770.00 + 6.08
L&T Technology Serv A 1,221.35 1,289.80 + 5.60
Religare Enterprises A 56.15 58.95 + 4.99
Indiabulls Real Estate A 196.80 204.90 + 4.12
Dewan Housing A 585.10 608.55 + 4.01

First, How to print all the data from table in console ? 
Solution:-
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "C:/Users/User/Downloads/chromedriver/chromedriver.exe");
driver = new ChromeDriver();

//Open URL
driver.get("any**webtable**url");

// Now locate the tables rows and create List of webelements
List tableRows=driver.findElements(By.xpath("//*[@id='leftcontainer']/table/tbody/tr"));
  int rowSize = rows.size();
  
  for(int i=0;i<rowSize;i++){
   
   List<WebElement> cells = driver.findElements(By.xpath("//table[@class='dataTable']/tbody/tr["+i+"]/td"));
   int cellSize = cells.size();
   for(int j=0;j<cellSize;j++){
    System.out.println(cells.get(j).getText());
   }
  }
Second, How to print data of specific column ?
 List<WebElement> cell= driver.findElements(By.xpath("//table[@class='dataTable']/tbody/tr/td[3]"));
         int cellSize = cell.size();
          for(int i=0;i < cellsize; i++){
            System.out.println(cell.getText());
               }
Third, How to print data of specific row? ?
 List<WebElement> row= driver.findElements(By.xpath("//table[@class='dataTable']/tbody/tr[3]"));
         int rowSize = row.size();
          for(int i=0;i < rowSize; i++){
            System.out.println(row.getText());
               }
Fourth, How to print data of specific cell based data of another cell?
List<WebElement> rowList = driver.findElements(By.xpath("//*[@id='leftcontainer']/table/tbody/tr"));
   for(int i = 0;i < rowList.size();i++){

            String rowText = rowList.get(i).getText();

//Now, we will check if String contains value "NIIT", then we will find its Current Price  
     if(rowText.contains("NIIT")){

      List<WebElement> list = driver.findElements(By.xpath("//*[@id='leftcontainer']/table/tbody/tr/td[3]"));

      String cellText=list.get(i).getText();

      System.out.println(cellText);

   }
}
Fifth, How to find & print sum of all of values of column?.
                
List<WebElement> currentPrice = driver.findElements(By.xpath("//table[@class='dataTable']/tbody/tr/td[3]"));

  double sum = 0;

  for(int i=0;i < currentPrice.size();i++){

   sum = sum + Double.parseDouble(currentPrice.get(i).getText().replace(",", ""));

  }
  System.out.println("Total Value of all shares current price is : " + sum);

Sixth, How to find the shares price whose Current Price is less than Rs. 800?
                
List<WebElement> currentPrice = driver.findElements(By.xpath("//table[@class='dataTable']/tbody/tr/td[3]"));


  for(int i=0;i < currentPrice.size();i++){

  String refineValue = currentPrice.get(i).getText().replace(",", "");

   if(Double.parseDouble(refineValue) < = 800.00){
    System.out.println("Share Price is : " + refineValue);
   }

  }

Questions Asked in Interview for Manual Testing Engineer?

1. What is Boundary Value Analysis and Equivalence Partitioning ? 2. What is Responsive web design ? 3. What is Globalization Te...