截圖捕獲功能可以幫助我們?cè)谛枰谶\(yùn)行時(shí)抓取截圖,在特別是當(dāng)故障發(fā)生。隨著截圖的幫助和日志信息,我們將能夠更好地分析結(jié)果
截圖是本地執(zhí)行和Selenium 網(wǎng)格(遠(yuǎn)程)處決配置不同。讓我們來(lái)看看他們每一個(gè)例子
在下面的例子中,我們將計(jì)算百分比之后的截圖。請(qǐng)確保給一個(gè)有效的路徑,用以保存屏幕截圖。
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(); } }
在執(zhí)行這個(gè)腳本,截圖保存在“D:screenshots”文件夾中名為'screenshots1.jpg“,如下圖所示。
當(dāng)Selenium網(wǎng)格工作,我們應(yīng)該確保從遠(yuǎn)程系統(tǒng)采取正確的截圖。我們將充分利用增強(qiáng)的驅(qū)動(dòng)程序。
我們將連接到集線器Firefox的節(jié)點(diǎn)上執(zhí)行該腳本。更多關(guān)于配置集線器和節(jié)點(diǎn),請(qǐng)參閱Selenium網(wǎng)格章節(jié)。
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上一篇:Selenium IDE下一篇:多瀏覽器測(cè)試