@@@Advanced selenium@@@
1).Switching between windows: Generally
selenium will focus on the 1st window, whenever the application
moves to the next page or tab , selenium can’t automatically switch to that
window so we need to make the selenium to switch to that latest window with the
help of following syntax.
Webdriverreference.switchTo().window(
"windowId" );
To get the window id we have a method
in selenium is getWindowHandles();
public class switchtooooo {
public static
String freshwid;
public static void
main(String[] args) throws
InterruptedException {
WebDriver wd=new
FirefoxDriver();
wd.get("http:hdfc.com");
wd.findElement(By.xpath(".//*[@id='node-18']/div/div/div/div/div[2]/nav/ul/li[2]/a/div")).click();
wd.findElement(By.xpath(".//*[@id='node-4']/div/div/div/div/div/section/div/section[2]/article[2]/div/a")).click();
Set<String> s=wd.getWindowHandles();
Iterator<String>itr=s.iterator();
while(itr.hasNext())
{
freshwid=itr.next();
s=wd.getWindowHandles();
itr=s.iterator();
while(itr.hasNext())
{
freshwid=itr.next();
}
wd.switchTo().window(freshwid);
Thread.sleep(2000l);
wd.findElement(By.xpath(".//*[@id='block-views-e693e51d3efb33676083fb5f5afa29aa']/div/nav/ul/li[2]/a/span")).click();
for(String
handle:wd.getWindowHandles())
{
wd.switchTo().window(handle);
}
Switch
to drawback is overcome
WebDriver wd=new FirefoxDriver();
wd.get("http:w3schools.com");
wd.findElement(By.xpath(".//*[@id='leftcol']/a[3]")).click();
wd.findElement(By.xpath(".//*[@id='menyen']/a[8]")).click();
wd.findElement(By.xpath("html/body/div[4]/div/div[2]/div[2]/div[1]/div/div[1]/div[3]/a")).click();
wd.findElement(By.xpath(".//*[@id='menyen']/a[15]")).click();
wd.findElement(By.xpath(".//*[@id='menyen']/a[21]")).click();
hdfc
WebDriver wd=new
FirefoxDriver();
wd.get("http:hdfc.com");
wd.findElement(By.xpath(".//*[@id='node-18']/div/div/div/div/div[2]/nav/ul/li[2]/a/div")).click();
wd.findElement(By.xpath(".//*[@id='block-views-e693e51d3efb33676083fb5f5afa29aa']/div/nav/ul/li[2]/a/span")).click();
Thread.sleep(4000l);
wd.findElement(By.xpath(".//*[@id='block-views-e693e51d3efb33676083fb5f5afa29aa']/div/nav/ul/li[1]/a/span")).click();
Thread.sleep(4000l);
wd.findElement(By.xpath(".//*[@id='block-views-e693e51d3efb33676083fb5f5afa29aa']/div/nav/ul/li[1]/a")).click();
}
}2) How to find the
screen shot in selenium
WebDriver driver = new FirefoxDriver();
driver.get("http:google.com");
File scrFile =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile,
new File("d:\\screenshot.png"));
3).Simulating the keyboard operations
using selenium
We can make the selenium perform the keyboard
related operations in the following way with the help of keys
Syntax: keys.KeyName
e.g.: keys.TAB, keys.ENTER.
Program to demonstrate keyboard
operations
package integer;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
public class alert {
public static void main(String[] args) {
WebDriver wd =new FirefoxDriver();
wd.get("http:gmail.com");
wd.findElement(By.xpath(".//*[@id='Email']")).sendKeys("akhilreddyhyd@gmail.com");
wd.findElement(By.xpath(".//*[@id='Email']")).sendKeys(Keys.TAB);
wd.findElement(By.xpath(".//*[@id='Passwd']")).sendKeys("venkanna143");
wd.findElement(By.xpath(".//*[@id='signIn']")).sendKeys(Keys.ENTER);
}
}
4)Finding the coordinates of web
element:
To find the coordinates of a web element in
Selenium web driver we have Point class and getLocation() method is available
to us so we can do it very easily in the following way.
public class coordinatessss {
public static void main(String[] args) {
WebDriver wd=new FirefoxDriver();
wd.get("http://www.w3schools.com/tags/ref_eventattributes.asp");
Point p=wd.findElement(By.xpath(".//*[@id='main']/h2[4]")).getLocation();
System.out.println(p.x);
System.out.println(p.y);
}
}
5)Event Listeners: Event
Listener is a concept provided by web driver which is used for automatically
doing some operations whenever some operation is performed (like clicking on
some object, navigate forward, navigate back).
Procedure for creating listener and handling
events:
First create one Listener class
Extend it to AbstractWebDriverEventListener class.
Override the predefined functions of
AbstractWebDriverEventListener class with your own body or logic
Create a new class for testing the listener which
is created by you.
Create a web driver reference
Create an EventFiringWebDriver reference and then
pass the web driver reference in the EventFiringWebDriver constructor
Create a reference for first created listener
class here in this class
Register the listener class reference to the
EventFiringWebDriver reference
Then go for your own tests and enhancements as you
like.
Note: one event
listener class is enough to handle events. We can use any of the event handling
methods and must override AbstractWebDriverEventListener class methods. We
shouldn’t use user defined methods for handling events but if u want to use
it(user defined functions) then you will go for your own logic.
what we can do with webDriver class reference the same can do with EventFiringWebDriver class reference and also
makes the listener class to listen the corresponding events registered with it.
Pop up or java script Alert messages handling: it
one of the main concepts in which we are regularly facing while doing web
application testing. To handle this, in selenium we have predefined class
called Alert.
New example monster
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.AbstractWebDriverEventListener;
System.out.println("akhil
click operation performed");
}
public void afterNavigateBack(WebDriver
driver) {
System.out.println("akhil
you asked to navigate back");
}
public void
beforeClickOn(WebElement element, WebDriver driver){
System.out.println("akhil
before clicking on ...");
}
}
TAKE ANOTHER CLASS NOW
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.support.events.EventFiringWebDriver;
public class listclass {
public static void main(String[] args) {
EventFiringWebDriver
efwd=new EventFiringWebDriver(wd);
listener
l=new listener();
efwd.register(l);
efwd.navigate().to("http:monster");
efwd.findElement(By.xpath(".//*[@id='wrapper']/table[6]/tbody/tr[1]/td/div/table/tbody/tr[1]/td[3]/h2")).click();
efwd.navigate().back();
}
}
6)Converter
public class covert {
public static void main(String[] args) {
int i=10;
String
s1=String.valueOf(i);
int i1=Integer.parseInt(s);
System.out.println(s1);
System.out.println(i1);
}
}
7)MOUSE MOVEMENTS
public class mouse {
public static void main(String[] args) {
WebDriver wd=new FirefoxDriver();
EventFiringWebDriver
efwd=new EventFiringWebDriver(wd);
listener
l=new listener();
efwd.register(l);
efwd.navigate().to("http:naukri.com");
efwd.findElement(By.xpath(".//*[@id='footN']/div[2]/div[1]/div/a[11]")).click();
efwd.navigate().back();
}
8)The
following example demonstrates pop up (alert) handling
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class webbb {
/**
* @param args
*/
WebDriver
wd=new FirefoxDriver();
wd.get("http://mypage.rediff.com/login");
wd.findElement(By.xpath(".//*[@id='toprightinfo']/a[1]")).click();
wd.findElement(By.xpath(".//*[@id='c_uname']")).click();
wd.findElement(By.xpath(".//*[@id='btn_login']")).click();
Alert
abox =wd.switchTo().alert();
abox
.accept();
//abox.dismiss();
}
}
9)Dynamic
web table handling:
The
following example demonstrates dynamic handling of web tables:
WebElement
table=wd.findElement(By.xpath(".//*[@id='main']/table"));
List<WebElement>
rows=table.findElements(By.tagName("tr"));
for(int
i=1;i<rows.size();i++)
{
System.out.println(i);
List<WebElement>
cells=rows.get(i).findElements(By.tagName("td"));
for(int
j=0;j<cells.size();j++)
{
System.out.println(j);
System.out.println(cells.get(j).getText());
}
}
}
Webbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
WebDriver wd=new FirefoxDriver();
wd.get("http://www.w3schools.com/html/html_tables.asp");
WebElement
table=wd.findElement(By.xpath("html/body/div[4]/div/div[2]/div[2]/div[1]/div/table[1]"));
List<WebElement>
rows=table.findElements(By.tagName("tr"));
//for(int i=4;i<rows.size();i++)
//{
//System.out.println(i);
//List<WebElement>
cells=rows.get(i).findElements(By.tagName("td"));
List<WebElement>
cells=rows.get(rows.size()-1).findElements(By.tagName("td"));
//for(int j=2;j<cells.size();j++)
//{
//System.out.println(j);
System.out.println(cells.get(2).getText());
//}
}
}
10) How to maximize the window
WebDriver driver=new FirefoxDriver();
driver.get("http://google.com");
driver.manage().window().maximize();
driver.close();
11)how to get the title of the page in selenium?
wd.get("http:irctc.com");
String title = wd.getTitle();
System.out.println(title);
12) How to
store page source using Selenium?
WebDriver wd=new FirefoxDriver();
wd.get("http:irctc.com");
String
ps = wd.getPageSource();
System.out.println(ps);
No comments:
Post a Comment