0
Showing posts with label SeleniumWebDriver. Show all posts
Showing posts with label SeleniumWebDriver. Show all posts
Reading Data from the table is somewhat different from accessing normal elements from the webpages.Because generally developers when develop the html page they provide id,name etc.But for table they don't provide any id, name etc.So we generally go with XPath.
Below Html code is:
Examples:
package Screenshotexp;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class googleScreenshot {
WebDriver driver;
@Test
public void googleScreenShot()
{
driver=new FirefoxDriver();
driver.get("https://www.google.co.in");
driver.manage().window().maximize();
//we have given wrong xpath intentionally.so test case fails
driver.findElement(By.xpath("//*[@id='gs_htif']")).sendKeys("Selenium");
}
//After Method always execute
@AfterMethod
public void tearDown(ITestResult result)
{
//ITestResult.FAILURE for failed testcases
if(ITestResult.FAILURE==result.getStatus())
{
try{
File screenshot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//result.getname() will get testcase name and save same file for screenshot which is taken
FileUtils.copyFile(screenshot, new File("./Screenshots/"+result.getName()+".png"));
System.out.println("Screenshot taken");
}catch(Exception e)
{
System.out.println("Unable to take screen shot:"+e.getMessage());
}
driver.quit();
}
}
}
Output:
Screenshot taken
FAILED: googleScreenShot
Multiple Windows:
Multiple window handling in selenium webdriver is done by using "getWindowHandle()" is for the parent window and "getWindowHandles()" is for multiple child window handling.We have to switch window from parent (or) main window to child window.
- Below i have given the html sample code also for the below program.
- save that html code by .html extension and pass that path in driver get method.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class multiplewindow {
@Test
public void mulwindows() throws InterruptedException
{
WebDriver driver=new FirefoxDriver();
driver.get("H:\\websites.html");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
System.out.println("Parent window title is:"+driver.getTitle());
//get window handle method is used and stored in variable for further refernces.
String parent_window=driver.getWindowHandle();
driver.findElement(By.linkText("google")).click();
//Gets all the links present in main window and its return type is Set of strings.
Set<String> allwindows=driver.getWindowHandles();
for(String child_window:allwindows)
{
if(!child_window.equals(parent_window))
{
//Switching from parent window to child window
driver.switchTo().window(child_window);
System.out.println("Title is:"+driver.getTitle());
driver.findElement(By.className("gsfi")).sendKeys("selenium");
}
}
driver.close();
//Switching back to parent window.
driver.switchTo().window(parent_window);
Thread.sleep(3000);
driver.quit();
}
}
Output:
Parent window title is: mulwindow
Title is:Google
PASSED: mulwindows
HTML Code:
<!DOCTYPE html>
<html>
<body>
<title>mulwindow</title>
<p><a href="http://www.google.com" target="_blank">google</a></p>
<p><a href="http://www.flipkart.com"target="_blank">flipkart</a></p>
</body>
</html>
Selenium Web Driver(ver:2.0)
Selenium is an open source Automation Testing Tool.It is used mainly for web based application.Selenium cannot automate window based (or) standalone application.Selenium works on many operating system.also we can do multiple operating system.we can write in different languages like java,c#,Ruby,Python,Php,Pearl.If the application developed in any language we can automate by using above languages.
Features:
- It is open source.
- It supports multiple operating system.(windows,Linux,Mac)
- It supports multiple browsers.(Internet Explorer,Chrome,Firefox,Safari)
- Mobile Testing.
- Free of cost.
Subscribe to:
Posts (Atom)



