Selenium+Java——测试Web端的方法集合(一)
声明driver
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class Example { public static void main(String[] args) { // 使用Chrome浏览器 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); // 或者使用Firefox浏览器 System.setProperty("webdriver.gecko.driver", "path/to/geckodriver"); WebDriver driver = new FirefoxDriver(); // 在这里可以使用driver执行其他操作 // 关闭浏览器并退出 driver.quit(); } }
1、查找元素
定位元素,定位方法有:id,name,xpath,class,css,tag,link text,partial link text
//通过id查找元素 WebElement element = driver.findElement(By.id("elementId")); //通过name查找元素 WebElement element = driver.findElement(By.name("elementName")); //通过xpath查找元素 WebElement element = driver.findElement(By.xpath("//xpathExpression")); //通过css selector查找元素 WebElement element = driver.findElement(By.cssSelector("cssSelector")); //通过tag name查找元素 WebElement element = driver.findElement(By.tagName("tagName")); //通过link text查找元素 WebElement element = driver.findElement(By.linkText("linkText")); //通过partial link text查找元素 WebElement element = driver.findElement(By.partialLinkText("partialLinkText"));
2、元素获取
//获取元素的属性值 //attributeName:textContent,innerText,innertHTML,outerHTML,outerText,style等等 WebElement element = driver.findElement(By.id("elementId")); String attributeValue = element.getAttribute("attributeName"); //获取元素的class属性 WebElement element = driver.findElement(By.id("elementId")); String classAttribute= element.getClass(); //获取元素的CSS值(需要传入要获取的CSS属性名称作为参数) WebElement element = driver.findElement(By.id("elementId")); String cssValue = element.getCssValue("propertyName"); //获取元素在页面中的位置 WebElement element = driver.findElement(By.id("elementId")); Point location = element.getLocation(); int xCoordinate = location.getX(); int yCoordinate = location.getY(); //获取元素的标签名称 WebElement element = driver.findElement(By.id("elementId")); String tagName = element.getTagName(); //获取文本内容 WebElement element = driver.findElement(By.id("elementId")); String text = element.getText(); //获取元素的大小(宽度和高度),返回一个Dimension对象 WebElement element = driver.findElement(By.id("elementId")); Dimension size = element.getSize(); int width = size.getWidth(); int height = size.getHeight(); //获取元素的位置和大小 WebElement element = driver.findElement(By.id("elementId")); org.openqa.selenium.Rectangle rect = element.getRect(); System.out.println("X 坐标: " + rect.getX()); System.out.println("Y 坐标: " + rect.getY()); System.out.println("宽度: " + rect.getWidth()); System.out.println("高度: " + rect.getHeight()); //用于获取当前浏览器窗口或特定元素的屏幕截图(返回一个File对象或者Base64字符串) //1.获取整个浏览器窗口的屏幕截图并保存为文件 File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshotFile, new File("screenshot.png")); //2.获取特定元素的屏幕截图并保存为文件 WebElement element = driver.findElement(By.id("elementId")); File screenshotFile = element.getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshotFile, new File("elementScreenshot.png"));
3、点击
点击操作有很多,包括:点击,双击,左键,右键,长按等
//1.点击(单击) driver.findElement(By.xpath("//xpathExpression")).click(); //2.点击(单击) WebElement element = driver.findElement(By.id("elementId")); Actions actions = new Actions(driver); actions.moveToElement(element).click().build().perform(); //3.点击(单击) WebElement element = driver.findElement(By.id("elementId")); Actions actions = new Actions(driver); actions.click(element).build().perform(); //4.点击(单击) WebElement element = driver.findElement(By.id("elementId")); ((org.openqa.selenium.JavascriptExecutor)driver).executeScript("argument[0].click();",element); //5.点击(单击) Robot robot=new Robot(); robot.moveMove(x,y)//元素坐标 robot.movePress(KeyEvent.Button1_DOWN_MASK); robot.mouseRelease(InputEvent.Button1_DOWN_MASK); //6.点击(单击) element = driver.findElement(By.id("elementId")).submit(); /*------------------------------------------------------------------------------------------------*/ //1.双击 WebElement element = driver.findElement(By.id("elementId")); Actions actions = new Actions(driver); actions.doubleClick(element).build().perform(); //2.双击 driver.findElement(By.xpath("//xpathExpression")).click(); Robot robot=new Robot(); robot.movePress(KeyEvent.Button1_DOWN_MASK); robot.mouseRelease(InputEvent.Button1_DOWN_MASK); robot.movePress(KeyEvent.Button1_DOWN_MASK); robot.mouseRelease(InputEvent.Button1_DOWN_MASK); //3.双击 Robot robot=new Robot(); robot.moveMove(x,y)//元素坐标,int x,int y robot.movePress(KeyEvent.Button1_DOWN_MASK); robot.mouseRelease(InputEvent.Button1_DOWN_MASK); robot.movePress(KeyEvent.Button1_DOWN_MASK); robot.mouseRelease(InputEvent.Button1_DOWN_MASK); //4.双击 WebElement element = driver.findElement(By.id("elementId")); String js = "var element = document.getElementById('" + element + ');\n" + "var clickEvent = document.createEvent( 'MouseEvents');\n" +"clickEvent.initEvent('dblclick', true, true);\n" + "element.dispatchEvent(clickEvent);"; ((org.openqa.selenium.JavascriptExecutor)driver).executeScript(js, element); /*------------------------------------------------------------------------------------------------*/ //1.右键 WebElement element = driver.findElement(By.id("elementId")); Actions actions = new Actions(driver); actions.contextClick(element).build().perform(); //2.右键 driver.findElement(By.xpath("//xpathExpression")).click(); Robot robot=new Robot(); robot.movePress(KeyEvent.Button3_DOWN_MASK); robot.mouseRelease(InputEvent.Button3_DOWN_MASK); /*-----------------------------------------------------------------------------------------------*/ //1.左键 WebElement element = driver.findElement(By.id("elementId")); Actions actions = new Actions(driver); actions.click(element).build().perform(); //2.左键 driver.findElement(By.xpath("//xpathExpression")).click(); Robot robot=new Robot(); robot.movePress(KeyEvent.Button1_DOWN_MASK); robot.mouseRelease(InputEvent.Button1_DOWN_MASK); /*-----------------------------------------------------------------------------------------------*/ //长按 WebElement element = driver.findElement(By.id("elementId")); Actions actions = new Actions(driver); actions.clickAndHold(element).perform(); Thread.sleep((long)3*1000); actions.release(element).perform();
如果有写的不对或者有追加的地方欢迎补充私聊
#自动化测试##selenium##测试#自动化测试学习 文章被收录于专栏
记录工作中的自动化测试学习内容,例如:手机自动化测试学习,web端自动化测试学习等..