import static org.junit.jupiter.api.Assertions.*;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import Test1.ChromeDriveDemo;
class JavaScriptWinodowSize {
/*
* JavaScript获取网页大小
*/
WebDriver driver;
String baseurl;
private JavascriptExecutor js;
@BeforeEach
void setUp() throws Exception {
//本地驱动
System.setProperty("webdriver.chrome.driver", "/Users/lisen/webselenium/selenium/chromedriver");
//谷歌浏览器
driver =new ChromeDriver();
//访问网址
baseurl="https://www.baidu.com/";
//强转
js=(JavascriptExecutor) driver;
//设置隐性等待
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
//窗口最大化
driver.manage().window().maximize();
}
@Test
void test() throws Exception {
//打开网址
js.executeScript("window.location='https://www.baidu.com/';");
//等待
Thread.sleep(3000);
//高度
long height=(long) js.executeScript("return window.innerHeight;");
//宽度
long width =(long)js.executeScript("return window.innerWidth;");
System.out.println("窗口的高度为"+height);
System.out.println("窗口的宽度为"+width);
}
@AfterEach
void tearDown() throws Exception {
//关闭浏览器
driver.quit();
}
}