2. Go to the URL3. Collect Window Handles through Set4. Create an iterator to iterate through Window Handles.5. Increment the iterator and store the Window Handle as Parent.6. Increment the iterator and store next Window Handle as Child.7. Switch to Child Browser using Child Window Handle.8. When Child browser get closed, Switch from Child browser to Parent Window.
Set windowHandles = driver.getWindowHandles();
Iterator it = windowHandles.iterator();
String parentBrowser= it.next();
String childBrowser = it.next();
driver.switchTo().window(childBrowser);
Thread.sleep(3000);
driver.close(); //close the current window(Child Browser)
driver.switchTo().window(parentBrowser); //Switch to Parent Browser
------------------------------------------------------------------------------------------------------------------------------
11. CALENDAR popups
Calendar PopUp - 1
Normal Calender(current month) Popup can be handled in the following way.
1. Create Driver for any Browser(Mozilla)
2. Go to the URL3. Fetch the Calender element and click to open.4. Fetch the required date through xpath and click.
/*IRCTC calendar*/
driver.findElement(By.id("calendar_icon1")).click();
driver.findElement(By.xpath("//div[@id='CalendarControl']/table[tbody[tr[td[text()='October
2012']]]]/descendant::a[text()='5']")).click();
------------------------------------------------------------------------------------------------------------------------------
Calendar PopUp - 2 (Customized wait)
In
a Calender if we want to click on future month which is not currently
displayed, we need to click on next link until we get the required
month.
This can be done by writing Customized wait. Check
for particular date element in each month, if not found move to next
month.
/*makemytrip calendar*/driver.get("http://www.makemytrip.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("deptDateRtripimgExact")).click(); //find Calendar
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
boolean flag=true;
while(flag){
try {
WebElement
el =
driver.findElement(By.xpath("//div[contains(@class,'ui-datepicker-group')
and descendant::span[text()='March']]/descendant::a[text()='5']")); //
Required future date
if(el !=null) //Check if the required date element is found or not
{
el.click(); // if required Date is found, then click the date
flag=false;
}
}
catch (Exception e) { //Catches exception if no element found
try {
Thread.sleep(500);
driver.findElement(By.xpath("//a[@title='Next']")).click(); //Click on next month
}
catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
------------------------------------------------------------------------------------------------------------------------------
12. Drop Down MENU
In
order to click on an menu item, first we need to move the mouse over
Parent menu, later we can click on any of the Menu child item.
Please follow the steps mentioned below.
1. Create Driver for any Browser(Mozilla)
2. Go to the URL3. Fetch the MENU Parent element and create a WebElement object.4. Create an Action object for Driver5. Through Action object, move to Parent element.6. Give a Delay for menu items to be displayed.7. Fetch the Child item through xpath and Click on it.
WebElement parentMenu = driver.findElement(By.linkText("Tourist Trains"));
Actions act = new Actions(driver); // Create an Action object
//move to the parent menu item
act.moveToElement(parentMenu).build().perform();
Thread.sleep(3000); //wait till the child items are displayed
driver.findElement(By.linkText("Bharat Tirth")).click();
------------------------------------------------------------------------------------------------------------------------------
13. Context Click (Right Click)
We can use keyboard keys to Make a Right Click.
Please follow the steps mentioned below.
1. Create Driver for any Browser(Mozilla).
2. Go to the URL.3. Fetch the MENU Parent element and create a WebElement object.4. Create an Action Object for Driver.5. Through Action Object, make a Context Click on Menu Parent object.6. Through Action Object, send keys for ARROW_DOWN/ARROW_UP/Keys.ENTER.
Example
WebElement parentMenu = driver.findElement(By.linkText("Tourist Trains"));
Actions act = new Actions(driver); //Create Action object for Driver
act.contextClick(parentMenu).build().perform(); //Context Click
act.sendKeys(Keys.ARROW_RIGHT).build().perform();
Thread.sleep(1000);
act.sendKeys(Keys.ARROW_DOWN).build().perform();
Thread.sleep(1000);
act.sendKeys(Keys.ENTER).build().perform();
------------------------------------------------------------------------------------------------------------------------------
14. JAVA SCRIPT example
We can use java script command to perform actions.
We can write code to fill up the text box through java script.
Please follow the steps mentioned below.
1. Create Driver for any Browser(Mozilla).
2. Go to the URL.
3. Create Java Script executor object for the Driver.
4. Store the Java Script command in a String Variable.
5. Java Script Executor object executes the command in the Variable.
JavascriptExecutor js = (JavascriptExecutor) driver;
String jsCmd = "document.getElementsByName('city')[0].value='ban'";
js.executeScript(jsCmd);
------------------------------------------------------------------------------------------------------------------------------
15. Multiple Elements
We can count the number of links present in the page. We can also print the link text of each Web link.
Please follow the steps mentioned below.
1. Create Driver for any Browser(Mozilla).
2. Go to the URL.
3. Fetch elements with tag //a in the entire page, store it in a List.
4. Get the count of Links present in the Page.
5. Loop through the links and print the Attributes
List allLinks= driver.findElements(By.xpath("//a"));
//display the count of links in the page
System.out.println(allLinks.size());
//display the text for each link on the page
for(int i=0;i
{
//display href for each link
System.out.println(allLinks.get(i).getAttribute("href"));
//display text for each link
System.out.println(allLinks.get(i).getText());
//perform click action
allLinks.get(i).click();
}
------------------------------------------------------------------------------------------------------------------------------
16. Other Browser (Internet Explorer)
Using Internet Explorer with Web Driver.
Please follow the steps mentioned below.1. Set System Property for the Driver and give path of the IE Driver.2. Create an Web Driver Object.3. Open an URL
System.setProperty("webdriver.ie.driver", "D:\\sel\\browserdrivers\\IEDriverServer.exe");
WebDriver driver =new InternetExplorerDriver();
driver.get("www.google.com");
------------------------------------------------------------------------------------------------------------------------------
17. Other Browser (Chrome)
Using Chrome with Web Driver.
Please follow the steps mentioned below.
1. Set System Property for the Driver and give path of the Chrome Driver.2. Create an Web Driver Object.3. Open an URL
System.setProperty("webdriver.chrome.driver", "D:\\sel\\browserdrivers\\Chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("www.google.com");
------------------------------------------------------------------------------------------------------------------------------
18. PROXY settings.
Please follow the steps mentioned below.
1. Import Selenium.Proxy
2. Create a Profile object for Firefox
3. Create a string variable with value.
4. Create a Proxy object.
5. Set the values through proxy.
6. Set the proxy preference to proxy object using profile object.
7. Pass the profile object to Firefox Driver.
import org.openqa.Selenium.Proxy
FirefoxProfile profile = new FirefoxProfile();
String PROXY = "xx.xx.xx.xx:xx";
Proxy proxy = new Proxy();
proxy.HttpProxy=PROXY;
proxy.FtpProxy=PROXY;
proxy.SslProxy=PROXY;
profile.SetProxyPreferences(proxy);
FirefoxDriver driver = new FirefoxDriver(profile);
------------------------------------------------------------------------------------------------------------------------------
19. Page Onload authentication
Sometimes when you are
Automating Web pages, you may come across Page onload Authentication
window. This window is not java popup/div. It is windows popup. Selenium
directly cannot handle this windows popup.
Hence we use Autoit sowftware tool. Through Selenium we can handle this situation using Autoit.
Please follow the steps mentioned below.
1.Download Autoit from the following URl
( http://www.autoitscript.com/site/autoit/downloads/ )
2.Install downloaded software.
3. Open Script Editor
Start=>ProgramFiles=>AutoIt =>SciTE Script Editor.
4.Open Object Identifier.
Start=>ProgramFiles=>AutoIt =>AutoIt Window Info.
5.Drag and Drop finder tool in AutoIt Window Info, to the Window you need to identify.
6.Collect the Title Name of window from (AutoIt Window Info.)
7.Write the Script in the Editor.
----------------------------------------------------AUTOIT CODE-----------------------------------------------------
WinWaitActive("Authentication Required")
Send("admin")
Send("{TAB} admin{TAB} {ENTER}")
------------------------------------------------------------------------------------------------------------------------------
8.Save the file as default save.(Authentication1.exe)
9.RUN/Compile the SCRIPT, it creates an exe.
10.Mention the exe path in the Program before creation of Driver.
EXAMPLE:
Process P = Runtime.getRuntime().exec("D:\\java_prj\\SELENIUM WEBDRIVER\\AUTOIT\\Authentication1.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://192.168.1.1");
------------------------------------------------------------------------------------------------------------------------------
20. File Download
Please follow the steps mentioned below.
1. Create a PROFILE object of Browser.
2. Set Preference, by giving Download destination Directory.
3.
Set Preference, by giving Default Folder. 0 => Desktop, 1=>System
Default Location, 2 => Indicates a custom Folder Location
4. Set Preference, A
comma-separated list of MIME types to save to disk without asking what
to use to open the file. Default value is an empty string.
After coding the above mentioned steps, now start the driver and click on Download button/link.
1. Create Driver for any Browser(Mozilla).
2. Go to the URL.
3. Fetch the Download web element and click.
FirefoxProfile Prof = new FirefoxProfile();
Prof.setPreference("browser.download.dir", "D:\\java prj");
Prof.setPreference("browser.download.folderList", 2);
Prof.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");
WebDriver driver = new FirefoxDriver(Prof);
driver.get("http://seleniumhq.org/download/");
driver.manage().timeouts().implicitlyWait(3,TimeUnit.MINUTES);
driver.findElement(By.xpath("//a[@name='client-drivers']/table/tbody/tr[1]/td[4]/a")).click();
------------------------------------------------------------------------------------------------------------------------------