How to TakeScreenshot Using Selenium 4
mvn clean test
Dependencies :
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.6.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.2.3</version>
</dependency>
package com.base;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Base {
public static WebDriver driver;
public static WebDriver initializeBrowser(){
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
return driver;
}
}
package com.test;
import com.base.Base;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.testng.annotations.*;
import java.io.File;
import java.io.IOException;
public class TakeSnapTest extends Base {
public static WebDriver driver;
@BeforeMethod
public static void setUp() {
driver = initializeBrowser();
}
@org.testng.annotations.Test(enabled = true)
public void TestOne() throws IOException, InterruptedException {
driver.get("https://en.wikipedia.org/wiki/Infosys");
// driver.findElement(By.name("q")).sendKeys("Hello World");
WebElement title = driver.findElement(By.xpath(" //*[@alt=' Logo']"));
File screen = title.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screen,new File(".//image1.png"));
/*
driver.get("https://www.bmwusa.com/home.html");
Thread.sleep(3000);
File screen = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screen,new File(".//image1.png"));
Thread.sleep(3000);
*/
/*
ITesseract it = new Tesseract();
try {
String str = it.doOCR(new File("//Users//Documents//NewTest//ScanProject//image1.png"));
System.out.println(" The title is : "+str);
} catch (TesseractException e) {
System.out.println(" The exception :" + e.getMessage());
}
*/
}
@AfterMethod
public void tearDown(){
driver.quit();
}
}
How to create testng.xml
DO the right click on Project and Create a file and named as testng.xml
<suite name = "SuiteOne" parallel="classes" thread-count="2">
<test name = "testOne" >
<classes>
<class name = "com.test.TakeSnapTest"></class>
<class name = "testJava.OCR"></class>
</classes>
</test>
</suite>
Comments
Post a Comment