WebDriver高级应用实例(6)
6.1精确比较网页截图图片
目的:对于核心界面进行截屏,并且使用测试过程中的截图和以前测试过程中的截图进行比较。确认页面是否发生了改变
被测网页的网址:
http://www.baidu.com
Java语言版本的API实例代码
import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.awt.image.BufferedImage; import java.awt.image.DataBuffer; 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.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; public class TestCompareImages { WebDriver driver; String url = "http://www.baidu.com"; @Test public void testImageComparison() throws InterruptedException, IOException { File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); Thread.sleep(3000); //对百度首页进行截屏 FileUtils.copyFile(screenshot, new File("e:\\baiduHomePage_actual.jpg")); //生成两个文件对象,一个是期望图片(期望图片需自己先准备),一个是测试过程中产生的图片 File fileInput = new File("e:\\baiduHomePage_expected.jpg"); File fileOutPut = new File("e:\\baiduHomePage_actual.jpg"); /* * 以下部分分为两个文件进行像素比对的算法实现,获取文件的像素个数大小,然后使用循环对两张图片进行比对如果有任何一个像素不相同则退出循环 * */ BufferedImage bufileInput = ImageIO.read(fileInput); DataBuffer dafileInput = bufileInput.getData().getDataBuffer(); int sizefileInput = dafileInput.getSize(); BufferedImage bufileOutPut = ImageIO.read(fileOutPut); DataBuffer dafileOutPut = bufileOutPut.getData().getDataBuffer(); int sizefileOutPut = dafileOutPut.getSize(); Boolean matchFlag = true; if(sizefileInput == sizefileOutPut){ for(int j = 0; j<sizefileInput;j++){ if(dafileInput.getElem(j)!= dafileOutPut.getElem(j)){ matchFlag = false; break; } } } else matchFlag = false; Assert.assertTrue(matchFlag,"测试过程中的截图和期望的截图并不一致"); } @BeforeMethod public void beforeMethod() { System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get(url); } @AfterMethod public void afterMethod() { driver.quit(); } }
6.2高亮显示正在被操作的元素
目的:可以提示测试人员正在操作哪些元素
被测网页的网址:
http://www.baidu.com
Java语言版本的API实例代码
import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterMethod; public class TestHighLightWebElement { WebDriver driver; String url = "http://www.baidu.com"; @Test public void testHighLigHtWebElement() throws InterruptedException { WebElement searInpuBox = driver.findElement(By.id("kw")); WebElement submitButton = driver.findElement(By.id("su")); //调用封装好的的方法高亮显示搜索框 highLightElement(searInpuBox); searInpuBox.sendKeys("seleium"); //暂停3秒查看效果 Thread.sleep(3000); //调用封装好的方法高亮显示搜索按钮 highLightElement(submitButton); Thread.sleep(3000); submitButton.click(); } public void highLightElement(WebElement element) { JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript("element = arguments[0];" + "original_style = element.getAttribute('style');" + "element.setAttribute('style', original_style + \";" + "background: yellow; border: 2px solid red;\");" + "setTimeout(function(){element.setAttribute('style', original_style);}, 1000);", element); } @BeforeMethod public void beforeMethod() throws Exception{ System.setProperty("webdriver.chrome.driver","D:\\WebDriver\\chromedriver_win32\\chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get(url); } @AfterMethod public void afterMethod() throws Exception{ driver.quit(); } }