Automation Using Selenium Webdriver

Monday 24 October 2016

Captcha using Selenium Webdriver Automating(Breaking)

Automating(Breaking) captcha using Selenium Webdriver

Usually most of the companies either use their own captchas or one of the third party captchas
(GooglejQuery plugins) in the user registration page of their sites .So these pages can't be
 automated fully.
Infact Captcha itself is implemented to prevent automation. As per official captcha site

A CAPTCHA is a program that  protects  websites against bots  by generating and grading tests that humans can pass but 
current computer programs cannot.

Captchas are not brakeable but there are some third party captchas that can be breakable and one of
the example for it is "jQuery Real Person" captcha . Here is the documentation  :)


Here is the sample code to brake the "jQuery Real Person" Captcha using Selenium WebDriver.

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class captchaAutomtion { 
 
 WebDriver driver;
 
 @BeforeTest
 public void start(){
  driver = new FirefoxDriver();
 }
 
 @Test
 public void Test(){ 
  //Loading jQuery Real Person Captcha demonstration page
  driver.get("http://keith-wood.name/realPerson.html");
  JavascriptExecutor js = (JavascriptExecutor) driver;
  //Setting the captcha values
  js.executeScript("document.getElementsByName('defaultRealHash')[0]
.setAttribute('value', '-897204064')");
  driver.findElement(By.name("defaultReal")).sendKeys("QNXCUL");
  //Submit the form
  driver.findElement(By.xpath(".//*[@id='default']/form/p[2]/input")).
   .submit(); 
 }

}








Getting google search auto suggestions using Webdriver(Selenium 2)

Getting google search auto suggestions using Webdriver(Selenium 2)

In the google search when we start typing any search query google will start auto suggestions. All these search suggestions are part of a WebTable. If we would like to capture all these search suggestions then we have to just iterate through the table.

Here is the sample code which will start typing "vam" and then capture all search suggestions .
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class SearchSuggestion {
 
WebDriver driver;
 
 @BeforeTest
 public void start(){
   driver = new FirefoxDriver(); 
 }
  
 @Test
  public void SearchSuggestion() {
  
  driver.get("http://google.com");
  driver.findElement(By.id("gbqfq")).sendKeys("vam");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  
   WebElement table = driver.findElement(By.className("gssb_m")); 
   List rows = table.findElements(By.tagName("tr")); 
   Iterator i = rows.iterator(); 
   System.out.println("-----------------------------------------"); 
   while(i.hasNext()) { 
           WebElement row = i.next(); 
           List columns = row.findElements(By.tagName("td")); 
           Iterator j = columns.iterator(); 
           while(j.hasNext()) { 
                   WebElement column = j.next(); 
                   System.out.println(column.getText()); 
           } 
           System.out.println(""); 
            
   System.out.println("-----------------------------------------"); 
   } 
  } 
}

Here is what we will see in the browser after running the above code.





5 different ways to refresh a webpage using Selenium Webdriver

5 different ways to refresh a webpage using Selenium Webdriver

Here are the 5 different ways, using which we can refresh a webpage.There might be even more :)

There is no special extra coding. I have just used the existing functions in different ways to get
 it work. Here they are :
1.Using sendKeys.Keys method
driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);

2.Using navigate.refresh()  method
driver.get("https://accounts.google.com/SignUp");  
driver.navigate().refresh();

3.Using navigate.to() method
driver.get("https://accounts.google.com/SignUp");  
driver.navigate().to(driver.getCurrentUrl());

4.Using get() method
driver.get("https://accounts.google.com/SignUp");  
driver.get(driver.getCurrentUrl());

5.Using sendKeys() method
driver.get("https://accounts.google.com/SignUp"); 
driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");

Find All Links In a page

Find All Links
Testers might be in a situation to find all the links on a website. We can easily
do so by finding all elements with the Tag Name "a", as we know that for any
link reference in HTML, we need to use "a" (anchor) tag.

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class getalllinks
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.calculator.net");
java.util.List<WebElement> links =
driver.findElements(By.tagName("a"));
System.out.println("Number of Links in the Page is " +
links.size());
for (int i = 1; i<=links.size(); i=i+1)
{
System.out.println("Name of Link# " + i - +
links.get(i).getText());
}
}
}
Output
The output of the script would be thrown to the console as shown below. Though
there are 65 links, only partial output is shown below.