Thursday, 5 February 2015

How to capture screenshot in WebDriver?



public void test() throws IOException {
            // Code to capture the screenshot File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            // Code to copy the screenshot in the desired location
FileUtils.copyFile(scrFile, new File("C:\\CaptureScreenshot\\google.jpg"));                  
     }

How to mouse hover on a web element using WebDriver?









// Instantiating Action Interface
Actions actions=new Actions(driver);
// howering on the dropdown
actions.moveToElement(driver.findElement(By.id("id of the dropdown"))).perform();
// Clicking on one of the items in the list options
WebElement subLinkOption=driver.findElement(By.id("id of the sub link"));
subLinkOption.click();

How to handle frame in WebDriver?



Select iframe by id 
driver.switchTo().frame(“ID of the frame“);

Locating iframe using tagName driver.switchTo().frame(driver.findElements(By.tagName(“iframe”).get(0));

Locating iframe using index
frame(index) driver.switchTo().frame(0);

frame(Name of Frame)  
driver.switchTo().frame(“name of the frame”);
frame(WebElement element)
Select Parent 
Window driver.switchTo().defaultContent();