捕捉屏幕截圖

捕捉屏幕截圖

截圖捕獲功能可以幫助我們在需要在運行時抓取截圖,在特別是當故障發生。隨着截圖的幫助和日誌信息,我們將能夠更好地分析結果

截圖是本地執行和Selenium 網格(遠程)處決配置不同。讓我們來看看他們每一個例子

本地主機執行

在下面的例子中,我們將計算百分比之後的截圖。請確保給一個有效的路徑,用以保存屏幕截圖。

import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class webdriverdemo { public static void main(String[] args) throws IOException { WebDriver driver = new FirefoxDriver(); //Puts a Implicit wait, Will wait for 10 seconds before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Launch website driver.navigate().to("http://www.calculator.net/"); //Maximize the browser driver.manage().window().maximize(); // Click on Math Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Percent Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 50 in the second number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Click Calculate Button driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Get the Result Text based on its xpath String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("D:screenshotsscreenshots1.jpg")); //Print a Log In message to the screen System.out.println(" The Result is " + result); //Close the Browser. driver.close(); } }

輸出

在執行這個腳本,截圖保存在「D:screenshots」文件夾中名爲'screenshots1.jpg「,如下圖所示。

selenium_ide_172

Selenium網格- 捕捉屏幕截圖

當Selenium網格工作,我們應該確保從遠程系統採取正確的截圖。我們將充分利用增強的驅動程序。

例子

我們將連接到集線器Firefox的節點上執行該腳本。更多關於配置集線器和節點,請參閱Selenium網格章節。

package TestNG; import org.openqa.selenium.remote.Augmenter; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.TakesScreenshot; import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Parameters; import org.testng.annotations.Test; import java.io.File; import java.net.URL; import java.net.MalformedURLException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.remote.RemoteWebDriver; import java.io.IOException; public class TestNGClass { public WebDriver driver; public String URL, Node; protected ThreadLocal<RemoteWebDriver> threadDriver = null; @Parameters("browser") @BeforeTest public void launchapp(String browser) throws MalformedURLException { String URL = "http://www.calculator.net"; if (browser.equalsIgnoreCase("firefox")) { System.out.println(" Executing on FireFox"); String Node = "http://10.112.66.52:5555/wd/hub"; DesiredCapabilities cap = DesiredCapabilities.firefox(); cap.setBrowserName("firefox"); driver = new RemoteWebDriver(new URL(Node), cap); //Puts a Implicit wait, Will wait for 10 seconds before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Launch website driver.navigate().to(URL); driver.manage().window().maximize(); } else { throw new IllegalArgumentException("The Browser Type is Undefined"); } } @Test public void calculatepercent() throws IOException { driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Math Calculators
driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Click on Percent Calculators // Make use of augmented Driver to capture Screenshots. WebDriver augmentedDriver = new Augmenter().augment(driver); File screenshot = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("D:screenshots
emotescreenshot1.jpg")); // Please note - Screenshot would be saved on the system where the script is executed and NOT on remote machine. driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Enter value 50 in the second number of the percent Calculator
driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Click Calculate Button String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); // Get the Result Text based on its xpath System.out.println(" The Result is " + result); //Print a Log In message to the screen if(result.equals("5")) { System.out.println(" The Result is Pass"); } else { System.out.println(" The Result is Fail"); } } @AfterTest public void closeBrowser() { driver.quit(); } }

輸出

當執行該腳本,截圖被捕獲並儲存在指定的位置,如下所示。

selenium_ide_173