第十章 SeleniumWebDrive-高级

第九章 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";

        // Maximize the browser's window
        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;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/6/29
 */
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;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/6/29
 */
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';");
        //使用JS必须等待几秒 driver.get不用
       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;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/6/29
 */
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;

/**
 * @author 96510
 * @version 1.0
 * @date 2021/6/29
 */
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);//将目标位置与浏览器的顶部对齐
         //js.executeScript("arguments[0].scrollIntoView(false);", 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";

		// Maximize the browser's window
		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);
        //将driver强制转换为TakesScreenshot类型并使用截图功能
		FileUtils.copyFile(sourceFile, new File(directory+fileName));
        //将文件复制出来
		driver.quit();
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值