第九章 SeleniumWebDrive-高级
1.日历中日期选择(1.点击弹出日历选择框直接点击选择日期2.遍历日历中的日期并选择一个)
package jssd;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class calendarclickDemo {
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception {
driver = new ChromeDriver();
baseUrl = "https://www.expedia.cn";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
public void test1() throws Exception {
driver.get(baseUrl);
WebElement checkInField = driver.findElement(By.xpath("//input[@id='hotel-checkin-hp-hotel']"));
checkInField.click();
Thread.sleep(3000);
WebElement dateToSelect = driver.findElement(By.xpath("//caption[contains(text(),'十二月')]//parent::table//button[text()='31']"));
dateToSelect.click();
}
@Test
public void test2() throws Exception {
driver.get(baseUrl);
WebElement checkInField = driver.findElement(By.xpath("//input[@id='hotel-checkin-hp-hotel']"));
checkInField.click();
Thread.sleep(3000);
WebElement calMonth = driver.findElement(By.xpath("//caption[contains(text(),'十二月')]//parent::table"));
List<WebElement> allValidDated = calMonth.findElements(By.xpath("//button[@class='datepicker-cal-date']"));
for(WebElement date:allValidDated){
if (date.getText().equals("31")){
date.click();
break;
}
}
}
@After
public void tearDown() throws Exception {
Thread.sleep(3000);
driver.quit();
}
}
2.搜索自动拓展选择(定位ul并遍历)
package jssd;
import org.checkerframework.checker.units.UnitsTools;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class AutoComplete {
WebDriver driver;
String url;
@Before
public void setUp() throws Exception {
driver = new ChromeDriver();
url= "https://www.expedia.com/cn/";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
@Test
public void name() throws InterruptedException {
driver.get(url);
String model = "北京天安门皇家驿栈";
WebElement e1 = driver.findElement(By.xpath("//button[@data-stid='location-field-destination-menu-trigger']"));
e1.click();
Thread.sleep(2000);
driver.findElement(By.xpath("//input[@id='location-field-destination']")).sendKeys("北京天安门");
WebElement es = driver.findElement(By.xpath("//ul[@class='uitk-typeahead-results no-bullet']"));
List<WebElement> eslist = es.findElements(By.xpath("//ul[@class='uitk-typeahead-results no-bullet']//strong"));
for (WebElement e:eslist){
if (e.getText().equals(model)){
e.click();
}
}
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
@After
public void tearDown() throws Exception {
System.out.println("Test执行后");
}
}
3.使用JavaScript命令打开一个网站
package jssd;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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 javax.xml.bind.Element;
import java.util.concurrent.TimeUnit;
public class JavaScriptExecution {
WebDriver driver;
String url;
private JavascriptExecutor js;
@Before
public void setUp() throws Exception {
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
}
@Test
public void name() throws InterruptedException {
js.executeScript("window.location = 'http://www.baidu.com';");
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("kw"));
element.sendKeys("测试");
}
@After
public void tearDown() throws Exception {
System.out.println("Test执行后");
}
}
3.使用JavaScript获取窗口的大小
package jssd;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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 java.util.concurrent.TimeUnit;
public class JavaScriptExecution {
WebDriver driver;
String url;
private JavascriptExecutor js;
@Before
public void setUp() throws Exception {
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void name() throws InterruptedException {
js.executeScript("window.location = 'http://www.baidu.com';");
Thread.sleep(3000);
long width = Long.valueOf(String.valueOf(js.executeScript("return window.innerWidth;")));
long height = Long.valueOf(String.valueOf(js.executeScript("return window.innerHeight;")));
System.out.println(width);
System.out.println(height);
WebElement element = driver.findElement(By.id("kw"));
element.sendKeys("测试");
System.out.println(width);
System.out.println(height);
}
@After
public void tearDown() throws Exception {
System.out.println("Test执行后");
}
}
4.使用JavaScript实现页面滚动
package jssd;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class JavaScriptExecution {
WebDriver driver;
String url;
private JavascriptExecutor js;
@Before
public void setUp() throws Exception {
driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void name() throws InterruptedException {
js.executeScript("window.location = 'http://www.baidu.com';");
Thread.sleep(3000);
long width = Long.valueOf(String.valueOf(js.executeScript("return window.innerWidth;")));
long height = Long.valueOf(String.valueOf(js.executeScript("return window.innerHeight;")));
System.out.println(width);
System.out.println(height);
driver.findElement(By.id("kw")).sendKeys("测试" + Keys.ENTER);
Thread.sleep(3000);
js.executeScript("window.scrollBy(0,1900)");
Thread.sleep(3000);
js.executeScript("window.scrollBy(0,-2000);");
Thread.sleep(3000);
WebElement element = driver.findElement(By.xpath("//table[@class='c-gap-bottom-large c-gap-top-xsmall']"));
js.executeScript("arguments[0].scrollIntoView(true);", element);
Thread.sleep(3000);
js.executeScript("window.scrollBy(0,-2000);");
}
@After
public void tearDown() throws Exception {
System.out.println("Test执行后");
}
}
5.web页面截图
package jssd;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Screenshots {
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception {
driver = new ChromeDriver();
baseUrl = "https://login.yahoo.com";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void testScreenshots() throws Exception {
driver.get(baseUrl);
driver.findElement(By.id("login-username")).sendKeys("test");
driver.findElement(By.id("login-signin")).click();
}
public static String getRandomString(int length) {
StringBuilder sb = new StringBuilder();
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
for (int i = 0; i < length; i++) {
int index = (int) (Math.random() * characters.length());
sb.append(characters.charAt(index));
}
return sb.toString();
}
@After
public void tearDown() throws Exception {
Thread.sleep(3000);
String fileName = getRandomString(10)+".png";
String directory = "D:\\screenshot\\";
File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File(directory+fileName));
driver.quit();
}
}