23 December, 2014

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

18 December, 2014

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.

10 December, 2014

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<WebDriver> drivers;  
     public Test( )  
      {  
          this.drivers = new ArrayList<WebDriver>();  
      }  
 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);  
    }  
 }  

}