selenium测试(Java)--浏览器控制(四)
1. 控制浏览器窗口大小
1 package com.test.window; 2 3 import org.openqa.selenium.Dimension; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.firefox.FirefoxDriver; 6 7 public class WindowSize { 8 9 public static void main(String[] args) { 10 WebDriver driver; 11 // 获取Driver 12 driver = new FirefoxDriver(); 13 driver.get("http://www.baidu.com/"); 14 // 将屏幕最大化 15 driver.manage().window().maximize(); 16 waitTime(3000); 17 18 // 将浏览器大小设置成宽800,高480 19 Dimension arg0 = new Dimension(800, 480); 20 driver.manage().window().setSize(arg0); 21 waitTime(3000); 22 23 driver.quit(); 24 } 25 26 static public void waitTime(int time) { 27 28 try { 29 Thread.sleep(time); 30 } catch (InterruptedException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } 34 } 35 }
2. 控制浏览器前进后退
1 package com.test.window; 2 3 import org.openqa.selenium.WebDriver; 4 import org.openqa.selenium.firefox.FirefoxDriver; 5 6 public class WindowBackAndForward { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 11 WebDriver driver = new FirefoxDriver(); 12 13 //进入百度首页 14 driver.get("http://www.baidu.com"); 15 driver.manage().window().maximize(); 16 waitTime(3000); 17 //进入百度新闻 18 driver.get("http://news.baidu.com"); 19 waitTime(3000); 20 //回到百度首页 21 driver.navigate().back(); 22 waitTime(3000); 23 //前进到百度新闻 24 driver.navigate().forward(); 25 waitTime(3000); 26 27 driver.quit(); 28 } 29 30 static public void waitTime(int time){ 31 32 try { 33 Thread.sleep(time); 34 } catch (InterruptedException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 } 39 40 }
3. 模拟浏览器刷新
1 package com.test.window; 2 3 import org.openqa.selenium.WebDriver; 4 import org.openqa.selenium.firefox.FirefoxDriver; 5 6 public class WindowRefresh { 7 8 public static void main(String[] args) { 9 WebDriver driver = new FirefoxDriver(); 10 11 driver.get("http://news.baidu.com"); 12 driver.manage().window().maximize(); 13 waitTime(3000); 14 15 //刷新 16 driver.navigate().refresh(); 17 waitTime(3000); 18 19 driver.quit(); 20 21 } 22 23 static public void waitTime(int time) { 24 25 try { 26 Thread.sleep(time); 27 } catch (InterruptedException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 } 31 } 32 33 }
本文来自博客园,作者:月色深潭,交流群:733423266,转载请注明原文链接:https://www.cnblogs.com/moonpool/p/5657752.html