I accept received many requests from spider web log readers to part event on how to capture specific chemical constituent screenshot In selenium webdriver. As you lot know, Capturing entire page screenshot Is real tardily chore every bit described In THIS POST. But If you lot wants to capture screenshot of specific spider web
element in addition to then you lot volition experience that It Is petty challenging task. Let me endeavour to brand It tardily for you.
We demand to role many dissimilar classes in addition to methods of coffee in addition to selenium webdriver to capture spider web chemical constituent screenshot. Test execution sequence to capture screenshot of chemical constituent Is every bit bellow.
- Locate Image chemical constituent in addition to call captureElementScreenshot component to capture Its screenshot.
- Capture sum screenshot every bit buffered Image
- Get element's top in addition to width using getSize() method every bit described In THIS POST.
- Get element's X Y coordinates using Point class as described In THIS POST.
- Read buffered Image.
- Crop buffered Image using element's x y coordinate seat in addition to top width parameters.
- Save cropped Image at finish place physically.
Bellow given event volition last executed every bit execution sequence described above. Detailed description Is provided amongst each syntax.
Example to capture screenshot of Image element.
package Testing_Pack; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.Point; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; world aeroplane elementScreenshot { WebDriver driver; @BeforeTest world void setup() throws Exception { driver =new FirefoxDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get(" "); } @Test world void captureScreenshot() throws Exception { //Locate Image chemical constituent to capture screenshot. WebElement Image = driver.findElement(By.xpath("//img[@border='0']")); //Call captureElementScreenshot component to capture screenshot of element. captureElementScreenshot(Image); } world void captureElementScreenshot(WebElement element) throws IOException{ //Capture entire page screenshot every bit buffer. //Used TakesScreenshot, OutputType Interface of selenium in addition to File aeroplane of coffee to capture screenshot of entire page. File covert = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); //Used selenium getSize() method to transcend top in addition to width of element. //Retrieve width of element. int ImageWidth = element.getSize().getWidth(); //Retrieve top of element. int ImageHeight = element.getSize().getHeight(); //Used selenium Point aeroplane to transcend x y coordinates of Image element. //get location(x y coordinates) of the element. Point betoken = element.getLocation(); int xcord = point.getX(); int ycord = point.getY(); //Reading sum icon screenshot. BufferedImage img = ImageIO.read(screen); //cut Image using height, width in addition to x y coordinates parameters. BufferedImage dest = img.getSubimage(xcord, ycord, ImageWidth, ImageHeight); ImageIO.write(dest, "png", screen); //Used FileUtils aeroplane of apache.commons.io. //save Image screenshot In D: drive. FileUtils.copyFile(screen, novel File("D:\\screenshot.png")); } }
Run inwards a higher house event In eclipse in addition to opened upwards D: get at the halt of exam execution. There volition last created novel file amongst bring upwards "screenshot.png". Open It to verify screenshot Is proper. This way, You tin bathroom capture screenshot of whatever spider web element.
More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM
http://www.software-testing-tutorials-automation.com/