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
}
Advertisements

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

How to detect an element from the Drop down menu using Variable name.

We all know that we can select the elements from the Drop-down menu using three common methods. i.e. selectByIndex, selectByValue and selectByVisibleText.
But the problem arises when we have to detect the web-element by Variable name.
Here might be the situation,  When we are passing the Variable value by the Variable name  and want to select the option from the Drop-down menu using Variable name then we cannot use any of the available methods to select the Drop-down option.

So in this situation we can try in below manner to get the required Drop-down option from the Drop-down menu.

 WebElement Drop_down_menu = driver.findElement(By.xpath("//select[@name='RubricID']"));  
List Drop_down_option = Drop_down_menu.findElements(By.xpath(".//option[contains(text(),'" + Variable_name_ + "')]"));
for (WebElement option : Drop_down_option)
{
if (!option.isSelected())
{
option.click();
}
}

where   ‘Drop_down_menu‘ will save the path of Drop down box on your web page.
             ‘Drop_down_option‘ will  be the xpath of the required element from the Drop down box in                  which we will use Variable name.
             ‘Variable_name‘  will be the variable name used in your code.