Purpose: Capturing all Links in a page, filtering and visiting filtered pages.
In general while testing any application we are facing a situation where we need to visit specific link available on the Web Page. The Following script help floks how to handle it.
In the following example, I am going to visit 'www.google.com', captures all the links available on the page, filter the required link and visit that link page.
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Links {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.tata.com/");
driver.manage().window().maximize();
// Extract all links from the webpage using selenium webdriver
List<WebElement> all_links_webpage = driver.findElements(By.tagName("a"));
// Print total no of links on the webpage
System.out.println("Print total no of links on the webpage---------------------------------------------");
System.out.println(all_links_webpage.size());
// Filter the Link and visit that link
System.out.println("Print Links------------------------------------------------------------------------");
for(int i=0;i<all_links_webpage.size();i++)
{
Thread.sleep(1000);
if(all_links_webpage.get(i).getText().toLowerCase().contains("contact"))
{
System.out.println("Search Resutl found");
String link = all_links_webpage.get(i).getAttribute("href");
driver.navigate().to(link);
Thread.sleep(3000);
break;
}
}
driver.quit();
}
}
Showing posts with label Switching between Windows using Selenium Webdriver. Show all posts
Showing posts with label Switching between Windows using Selenium Webdriver. Show all posts
Monday, 8 April 2013
Monday, 1 April 2013
Capturing all Links on a Web Page and visiting those links using Web Driver
Purpose: Capturing all Links on a Web Page and visiting those links
In general while testing any application we are facing a situation where we need to visit each link available on the Web Page. The Following script help floks how to handle it.
In the following example, I am going to visit 'www.google.com', captures all the links available on the page, visiting each link and coming back to home page.
public static void main(String[] args) throws InterruptedException {
System.out.println(linksCount);
for(int i=0;i<linksCount;i++)
In general while testing any application we are facing a situation where we need to visit each link available on the Web Page. The Following script help floks how to handle it.
In the following example, I am going to visit 'www.google.com', captures all the links available on the page, visiting each link and coming back to home page.
importjava.util.List;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class Links {
private static String homeWindow = null;
private static String[] links = null;
private static int linksCount = 0;
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
// Fllowing instruction extract all links from the webpage using selenium webdriver
List<WebElement> all_links_webpage = driver.findElements(By.tagName("a"));
// Print total no of links on the webpage
System.out.println("Print total no of links on the webpage----------------------------------------------------");
linksCount = all_links_webpage.size();System.out.println(linksCount);
links= new String[linksCount];// Following instruction stores each link and Prints on console
System.out.println("Print Links-----------------------------------------------------------------------------------");
for(int i=0;i<linksCount;i++)
{
links[i] = all_links_webpage.get(i).getAttribute("href");
System.out.println(all_links_webpage.get(i).getAttribute("href"));
}
// Following instruction Return an opaque handle to this window that uniquely identifies it within this driver instance.
// This can be used to switch to this window at a later date
homeWindow = driver.getWindowHandle().toString();
// Visiting Each Link in on the Page
System.
out.println("Visiting Each Links------------------------------------------------------------------------");
for(int i=0;i<linksCount;i++)
{
driver.navigate().to(
links[i]);
Thread.sleep(3000);
driver.switchTo().window(
homeWindow);
}
driver.quit();
}
}
Monday, 25 February 2013
Switching between Windows using Selenium Webdriver
Purpose: Switching between Windows using Selenium Webdriver
We come across to handle switching b/w windows while accessing different webpages. Inroder to automate switching b/w windows, following steps will help you to write Selenium Webdriver Script with the help of Java
It is not as easy as handling with Selenium Rc. We can achive with the help of getWindowHandle() and switchTo().window() methods.
Following is the Selenium Webdriver with Java script to handle popup windows:
package com.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SwitchWindow {
public static void main(String[] args) throws Exception
{
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.quackit.com/html/codes/");
// Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date
String parentWindow = driver.getWindowHandle().toString();
driver.findElement(By.linkText("Pop up windows")).click();
Thread.sleep(3000);
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Example HTML Popup Window Code:[\\s\\S]*$"));
driver.findElement(By.linkText("Open a popup window")).click();
// to switch control to the new popup window by name
driver.switchTo().window("popUpWindow");
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Copy/Paste HTML Codes[\\s\\S]*$"));
// closing the popup window
driver.close();
// to switch control from popup window to another window, here parent window
driver.switchTo().window(parentWindow);
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Example HTML Popup Window Code:[\\s\\S]*$"));
driver.close();
}
}
// Code for TestNG
package com.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
public class SwitchWindow {
FirefoxDriver driver;
@Test
public void testSwitchWindow() {
driver.get("http://www.quackit.com/html/codes/");
// Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date
String parentWindow = driver.getWindowHandle();
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*HTML Codes - Free[\\s\\S]*$"));
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Pop up windows[\\s\\S]*$"));
driver.findElement(By.linkText("Pop up windows")).click();
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Open a popup window[\\s\\S]*$"));
driver.findElement(By.linkText("Open a popup window")).click();
// to switch control to the new popup window by name
driver.switchTo().window("popUpWindow");
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Copy/Paste HTML Codes[\\s\\S]*$"));
// closing the popup window
driver.close();
// to switch control from popup window to another window, here parent window
driver.switchTo().window(parentWindow);
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Open a popup window[\\s\\S]*$"));
}
@BeforeClass
public void beforeClass() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@AfterClass
public void afterClass() {
driver.close();
}
}
We come across to handle switching b/w windows while accessing different webpages. Inroder to automate switching b/w windows, following steps will help you to write Selenium Webdriver Script with the help of Java
It is not as easy as handling with Selenium Rc. We can achive with the help of getWindowHandle() and switchTo().window() methods.
Following is the Selenium Webdriver with Java script to handle popup windows:
package com.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SwitchWindow {
public static void main(String[] args) throws Exception
{
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.quackit.com/html/codes/");
// Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date
String parentWindow = driver.getWindowHandle().toString();
driver.findElement(By.linkText("Pop up windows")).click();
Thread.sleep(3000);
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Example HTML Popup Window Code:[\\s\\S]*$"));
driver.findElement(By.linkText("Open a popup window")).click();
// to switch control to the new popup window by name
driver.switchTo().window("popUpWindow");
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Copy/Paste HTML Codes[\\s\\S]*$"));
// closing the popup window
driver.close();
// to switch control from popup window to another window, here parent window
driver.switchTo().window(parentWindow);
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Example HTML Popup Window Code:[\\s\\S]*$"));
driver.close();
}
}
// Code for TestNG
package com.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
public class SwitchWindow {
FirefoxDriver driver;
@Test
public void testSwitchWindow() {
driver.get("http://www.quackit.com/html/codes/");
// Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date
String parentWindow = driver.getWindowHandle();
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*HTML Codes - Free[\\s\\S]*$"));
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Pop up windows[\\s\\S]*$"));
driver.findElement(By.linkText("Pop up windows")).click();
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Open a popup window[\\s\\S]*$"));
driver.findElement(By.linkText("Open a popup window")).click();
// to switch control to the new popup window by name
driver.switchTo().window("popUpWindow");
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Copy/Paste HTML Codes[\\s\\S]*$"));
// closing the popup window
driver.close();
// to switch control from popup window to another window, here parent window
driver.switchTo().window(parentWindow);
assert(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Open a popup window[\\s\\S]*$"));
}
@BeforeClass
public void beforeClass() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
@AfterClass
public void afterClass() {
driver.close();
}
}
Subscribe to:
Posts (Atom)