What is "Headless Browser" ?

1. A headless Browser is a Web Browser without a GUI, In other words it is a piece of software that access web-pages but doesn’t  show them to any human being.
2. They are actually used to provide content of web-pages to other programs.
3. Headless Browser can used for below mentioned reasons,

  •   How wide the page would appear for  a user.
  •   What color of the Text would be?
  •   The font family used.
  •   What the x/y coordinates of an object is  and many more.

Basically it understands web-pages like a browser.

3. ‘Phantomjs‘ is one of the example of the Headless Browser.

This entry was posted on January 9, 2015. 1 Comment

How to know if web-page is open or closed?

After calling driver.close() the value of driver is set to
FirefoxDriver: firefox on WINDOWS(4b4ffb1e-7c02-4d9c-b37b-310c771492ac)
But if you call driver.quit() then it sets the value of driver to
FirefoxDriver: firefox on WINDOWS (null)
So if you’re checking the browser window after calling driver.quit() then you will be able to know by below implementation.
WebDriver driver = new FirefoxDriver();
driver
.get("http://www.google.com");
driver
.quit();
if(driver.toString().contains("null"))
{

System.out.print("All Browser windows are closed ");
}
else
{
//open a new Browser
}

How to switch the Browser windows using different driver instances.

In the below program we have created two browser instances i.e driver and driver1.
with one driver instance we are opening one URL and with second driver instance other URL.
Below code shows how we can switch the browser windows using different driver instances.

 import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class FrameWorkBase {
// 'driver' and 'driver1' are two instances created.
public static WebDriver driver,driver1;
public static WebDriverWait wait;
public static String Parentwindow;
public static void main (String []args) throws Exception
{
driver= new FirefoxDriver();
driver.manage().window().maximize();
wait=new WebDriverWait(driver, 10);
driver.get("http://www.yahoo.com");
System.out.println(driver.getTitle());
Parentwindow = driver.getWindowHandle();
for ( String currentwindow : driver.getWindowHandles())
driver.switchTo( ).window(currentwindow);
{
driver1=new FirefoxDriver();
driver1.manage().window().maximize();
driver1.get("https://translate.google.co.in/");
System.out.println(driver1.getTitle());
driver.switchTo().window(Parentwindow);
}
System.out.println(driver.getTitle());
}
}

How to open a Link in new tab using Selenium Web-driver.

  • If you want to manually open the Link in New Tab you can achieve this by performing Context Click on the Link and selecting ‘Open in new Tab’ option. Below is the implementation in Selenium web-driver with Java binding.
Actions newTab= new Actions(driver);
WebElement link = driver.findElement(By.xpath("//xpath of the element"));

//Open the link in new window
newTab.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
  • Web-driver handles new tab in the same way to that of new window. You will have to switch to new open tab by its window name.
driver.switchTo().window(windowName);
You can keep track of window-names which will help you to easily navigate between tabs.

How to Close three different Browser Instances using ‘driver.quit()’ ?

 Below code will open three browsers (IE,Firefox and Chrome) and will open URL and finally it closes all the three opened browsers by ‘driver.quit()’ function.

 import java.util.ArrayList;  
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Test
{
private List drivers;
public Test( )
{
this.drivers = new ArrayList();
}
public void CloseAll()
{
for(WebDriver d : drivers)
{
d.quit();
}
}
public static void main(String[] args)
{
Test1 sc = new Test1();
sc.IE("http://google.com");
sc.Firefox("http://google.com");
sc.Chrome("http://google.com");
sc.CloseAll();
}
//Internet Explorer driver
public void IE(String URL)
{
//Set you IEDriver path as per your System Path
System.setProperty("webdriver.ie.driver","S:\\IE Driver\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get(URL);
this.drivers.add(driver);
}
//Firefox driver
public void Firefox(String URL)
{
WebDriver driver = new FirefoxDriver();
driver.get(URL);
this.drivers.add(driver);
}
//Chrome driver
public void Chrome(String URL)
{
//Set you ChromeDriver path as per your System Path
System.setProperty("webdriver.chrome.driver","S://Chrome Driver//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(URL);
this.drivers.add(driver);
}
}

}

How to perform Scroll operation in Selenium-Webdriver

Scroll Down:

 import org.openqa.selenium.JavascriptExecutor;   
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 250)"); //y value '250' can be altered

————————————————————————————————-
Scroll up:

 JavascriptExecutor jse = (JavascriptExecutor)driver;  
jse.executeScript("scroll(250, 0)"); //x value '250' can be altered


—————————————————————————————————-
Scroll bottom of the Page:

 JavascriptExecutor jse = (JavascriptExecutor)driver;  
jse.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));");
(or)
Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();


—————————————————————————————————–
Full scroll to bottom in slow motion:

 for (int second = 0;; second++)   
{
if(second >=60)
{
break;
}
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,400)", ""); //y value '400' can be altered
Thread.sleep(3000);
}
(or)
JavascriptExecutor jse = (JavascriptExecutor)driver;
for (int second = 0;; second++)
{
if(second >=60)
{
break;
}
jse.executeScript("window.scrollBy(0,800)", ""); //y value '800' can be altered
Thread.sleep(3000);
}


———————————————————————————————————————-
Scroll automatically to your WebElement:

 

 Point hoverItem =driver.findElement(By.xpath("Value")).getLocation();  
((JavascriptExecutor)driver).executeScript("return window.title;");
Thread.sleep(6000);
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+(hoverItem.getY())+");");
// Adjust your page view by making changes right over here (hoverItem.getY()-400)
(or)
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", driver.findElement(By.xpath("Value')]")));
(or)
WebElement element = driver.findElement(By.xpath("Value"));
Coordinates coordinate = ((Locatable)element).getCoordinates();
coordinate.onPage();
coordinate.inViewPort();

How to perform Right-click/Context-click in Selenium Webdriver.

Using ‘Action class‘ we can easily perform Right-click/Context-click on any Web-element.

Step 1: Store the Path of Web-element to be Right-clicked  in to the Web-element.
             Lets say  We have a Web-element name Test_element which we are right-clicking.
Step2:  We will use Action class to perform the Right-click/Context-click on our Web-element.
                              

 Actions actions = new Actions(driver);    
Action action=actions.contextClick(Test_element).build();
action.perform();