一、使用js/jQuery代码
@BeforeTest
public static void init() {
System.setProperty("webdriver.chrome.driver", "chromedriver");
}
@Test(description = "")
public static void test06() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://www.baidu.com/");
JavascriptExecutor javascriptExecutor= (JavascriptExecutor) webDriver;
javascriptExecutor.executeScript("document.getElementById('kw').value=(\"222\")");
javascriptExecutor.executeScript("$(\"#kw\").val(\"1111\")");
Thread.sleep(5000);
webDriver.quit();
}
二、滑块
@Test(description = "")
public static void test01() throws InterruptedException {
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://trains.ctrip.com/");
webDriver.findElement(By.cssSelector("#nav-bar-set-reg > a > span")).click();
webDriver.findElement(By.xpath("//*[@id=\"agr_pop\"]/div[3]/a[2]")).click();
WebElement element = webDriver.findElement(By.xpath("//*[@id=\"slideCode\"]/div[1]/div[2]"));
WebElement element2 = webDriver.findElement(By.cssSelector("#slideCode"));
System.out.println(element2.getSize().getWidth());
Actions actions=new Actions(webDriver);
Thread.sleep(1000);
actions.dragAndDropBy(element,element2.getSize().getWidth(),-element.getSize().getHeight()).perform();
Thread.sleep(5000);
webDriver.quit();
}
三、模糊定位start_with和end_with
webDriver.findElement(By.xpath("//*[start_with(@id,'su')]")).click();
webDriver.findElement(By.xpath("//*[contains(@id,'su')]")).click();
webDriver.findElement(By.xpath("//*[ens_with(@id,'su')]")).click();
四、设置cookies
Set<Cookie> cookies = webDriver.manage().getCookies();
for (Cookie cookie : cookies) {
System.out.println("domain:"+cookie.getDomain());
webDriverNew.manage().addCookie(cookie);
}
五、截图
File screenshotAs = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
File file = new File("/Users/aa.jpg");
Files.copy(screenshotAs, file);
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://i.qq.com/");
Thread.sleep(2000);
webDriver.switchTo().defaultContent();
WebElement login_div = webDriver.findElement(By.className("login_wrap"));
webDriver.switchTo().frame("login_frame");
WebElement element = webDriver.findElement(By.cssSelector("#qrlogin_img"));
int weight = element.getSize().getWidth();
int height = element.getSize().getHeight();
Point point = element.getLocation();
File screenshotAs = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
File file = new File("/Users/bb.jpg");
Files.copy(screenshotAs, file);
BufferedImage bufferedImage = ImageIO.read(screenshotAs);
int x = point.getX() + login_div.getLocation().getX();
int y = point.getY() + login_div.getLocation().getY();
System.out.println(x + " " + y + " " + weight + " " + height);
BufferedImage subimage = bufferedImage.getSubimage(x, y, weight, height);
ImageIO.write(subimage, "png", screenshotAs);
File file2 = new File("/aa.png");
Files.copy(screenshotAs, file2);
Thread.sleep(3000);
webDriver.quit();
六、execl文件的读入和写出
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
@Test(description = "读取xlsx文件值")
public static void test06() throws InterruptedException, IOException {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("ccc.xlsx"));
XSSFSheet sheetAt = xssfWorkbook.getSheetAt(0);
int lastRowNum = sheetAt.getLastRowNum();
for (int row = 0; row <= lastRowNum; row++) {
short totleNum = sheetAt.getRow(row).getLastCellNum();
for (int num = 0; num < totleNum; num++) {
System.out.println(sheetAt.getRow(row).getCell(num));
}
}
}
@Test(description = "写出到xlsx文件")
public static void test07() throws InterruptedException, IOException {
File file = new File("ddd.xlsx");
if (!file.exists()) {
file.createNewFile();
}
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
XSSFSheet newSheet = xssfWorkbook.createSheet();
XSSFRow row = newSheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("新建第一行第一个元素");
FileOutputStream fileOutputStream = new FileOutputStream(file);
xssfWorkbook.write(fileOutputStream);
fileOutputStream.close();
}
七、json文件
{
"respData": {
"aaa": "0",
"bbb": "0"
},
"respCode": "0",
"response":[
{
"key": "0",
"value": "0"
},
{
"key": "1",
"value": "1"
}
]
}
JsonObject js= (JsonObject) new JsonParser().parse(new FileReader("eee.json"));
JsonElement respData = js.get("response");
|