首页 > 代码库 > WebDriver API 实例详解(一)
WebDriver API 实例详解(一)
一、访问某网页地址
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
方法1:
1 package test; 2 3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8 9 public class ChormeOpen { 10 WebDriver driver; 11 @Test 12 public void opentest() { 13 driver.get("http://www.baidu.com"); 14 try { 15 Thread.sleep(5000); 16 } catch (InterruptedException e) { 17 // TODO Auto-generated catch block 18 e.printStackTrace(); 19 } 20 } 21 @BeforeMethod 22 public void beforeMethod() { 23 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 24 driver = new ChromeDriver(); 25 } 26 27 @AfterMethod 28 public void afterMethod() { 29 driver.quit(); 30 } 31 32 }
方法2:
1 package test; 2 3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8 9 public class ChormeOpen { 10 WebDriver driver; 11 @Test 12 public void opentest() { 13 driver.navigate().to("http://www.baidu.com"); 14 try { 15 Thread.sleep(5000); 16 } catch (InterruptedException e) { 17 // TODO Auto-generated catch block 18 e.printStackTrace(); 19 } 20 } 21 @BeforeMethod 22 public void beforeMethod() { 23 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 24 driver = new ChromeDriver(); 25 } 26 27 @AfterMethod 28 public void afterMethod() { 29 driver.quit(); 30 } 31 32 }
二、返回上一个访问的网页(模拟点击浏览器的后退功能)
被测试网页的网址:
http://www.hao123.com
http://www.baidu.com
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8 9 public class ChormeOpen { 10 WebDriver driver; 11 String url = "http://www.baidu.com"; 12 String url2 = "http://www.hao123.com"; 13 @Test 14 public void opentest() { 15 driver.navigate().to(url); 16 driver.navigate().to(url2); 17 driver.navigate().back();//返回 18 try { 19 Thread.sleep(5000); 20 } catch (InterruptedException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } 24 } 25 @BeforeMethod 26 public void beforeMethod() { 27 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 28 driver = new ChromeDriver(); 29 } 30 31 @AfterMethod 32 public void afterMethod() { 33 driver.quit(); 34 } 35 36 }
三、从上次访问网页前进到下一个网页(模拟单击浏览器的前进功能)
被测试网页的网址:
http://www.hao123.com
http://www.baidu.com
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8 9 public class ChormeOpen { 10 WebDriver driver; 11 String url = "http://www.baidu.com"; 12 String url2 = "http://www.hao123.com"; 13 @Test 14 public void opentest() { 15 driver.navigate().to(url); 16 driver.navigate().to(url2); 17 driver.navigate().back();//返回 18 driver.navigate().forward();//前进 19 try { 20 Thread.sleep(5000); 21 } catch (InterruptedException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 } 26 @BeforeMethod 27 public void beforeMethod() { 28 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 29 driver = new ChromeDriver(); 30 } 31 32 @AfterMethod 33 public void afterMethod() { 34 driver.quit(); 35 } 36 37 }
四、刷新当前网页
被测试网页的网址:
http://www.hao123.com
Java语言版本的API实例代码:
1 package test; 2 3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDriver; 7 import org.testng.annotations.AfterMethod; 8 9 public class ChormeOpen { 10 WebDriver driver; 11 String url2 = "http://www.hao123.com"; 12 @Test 13 public void opentest() { 14 driver.navigate().to(url2); 15 driver.navigate().refresh();//刷新 16 try { 17 Thread.sleep(5000); 18 } catch (InterruptedException e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } 22 } 23 @BeforeMethod 24 public void beforeMethod() { 25 System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe"); 26 driver = new ChromeDriver(); 27 } 28 29 @AfterMethod 30 public void afterMethod() { 31 driver.quit(); 32 } 33 34 }
WebDriver API 实例详解(一)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。