Wednesday, October 15, 2014

Functions

For pressing the enter key in keyboard

robot robot1=new robot();
robot1.keypress(keyevent.vk_enter);
robot1.keyrelease(keyevent.vk_enter);

For taking screen shots

file scrfile=((TakesScreenshot)driver).getScreenshotAs(Outputtype.FILE);
fileutils.copyfile(scrfile,new file("location"));

Handling keyboard and mouse events

Actions action = new Actions(driver);
action.moveToElement(element).click().perform();

Actions actions = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText("menulink"));
actions.moveToElement(mainMenu);

WebElement subMenu = driver.findElement(By.cssSelector("subLinklocator"));
actions.moveToElement(subMenu);
actions.click().build().perform();

Tooltip

String tooltip=driver.findElement(By.id("hplogo")).getAttribute("title");
System.out.println(tooltip);

Javascript alert

myTestDriver.findElement(By.xpath("//input[@value = 'prompt']")).click();

Alert javascriptprompt = myTestDriver.switchTo().alert();
javascriptprompt.sendKeys("This is Selenium Training");

System.out.println(javascriptprompt.getText()); // Get text on alert box
        Thread.sleep(5000);
javascriptprompt.accept();
javascriptprompt = myTestDriver.switchTo().alert();

System.out.println(javascriptprompt.getText()); // Get text on alert box
javascriptprompt.accept();

myTestDriver.findElement(By.xpath("//input[@value = 'prompt']")).click();

javascriptprompt = myTestDriver.switchTo().alert();

System.out.println(javascriptprompt.getText()); // Get text on alert box
Thread.sleep(5000);
javascriptprompt.dismiss();
javascriptprompt = myTestDriver.switchTo().alert();

System.out.println(javascriptprompt.getText()); // Get text on alert box
javascriptprompt.accept();

Window Handler

public class windowhandler
{
static WebDriver driver;
@Test
public void windows()
{
driver=new FirefoxDriver();
//It will open Naukri website with multiple windows
driver.get("http://www.naukri.com/");

//It will get the window handles and print the Main window handler in Alpha numeric
String mainWindow=driver.getWindowHandle();
System.out.println(mainWindow);
Set<String> handler=driver.getWindowHandles();
// Handler will have all the three window handles
for (String handlesname : handler)
{
driver.switchTo().window(handlesname);
// It will get the Title of each window,
String var=driver.getTitle();
System.out.println(var);
//This will check Windows and not. If it is not the parent window it will close the child window
if(!handlesname.contains(mainWindow))
{
driver.close();
}
else{
System.out.println("it is the main window");
driver.switchTo().defaultContent();
}
driver.switchTo().window(mainWindow);
System.out.println(driver.getTitle());
}
}
}

No comments:

Post a Comment