Chinaunix首页 | 论坛 | 博客
  • 博客访问: 408575
  • 博文数量: 119
  • 博客积分: 1470
  • 博客等级: 上尉
  • 技术积分: 1258
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-24 13:50
文章分类

全部博文(119)

文章存档

2018年(6)

2017年(11)

2016年(4)

2013年(8)

2012年(1)

2011年(2)

2010年(4)

2009年(37)

2008年(16)

2006年(30)

我的朋友

分类: Java

2017-06-28 11:18:17

(1) 操作页面元素之 单选按钮
  1. package seleniumtest.seleniumtest;

  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.List;

  5. import org.testng.annotations.*;
  6. import static org.testng.AssertJUnit.assertEquals;
  7. import static org.testng.Assert.assertTrue;
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.JavascriptExecutor;
  10. import org.openqa.selenium.WebDriver;
  11. import org.openqa.selenium.WebDriverException;
  12. import org.openqa.selenium.WebElement;
  13. import org.openqa.selenium.firefox.FirefoxDriver;
  14. import org.openqa.selenium.remote.DesiredCapabilities;
  15. import org.openqa.selenium.remote.RemoteWebDriver;

  16. public class testRadioButton {
  17.     WebDriver driver;
  18.     
  19.     @BeforeTest
  20.     public void setup(){
  21.         System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
  22.         driver = new FirefoxDriver();
  23.         driver.get("");
  24.     }
  25.     
  26.     @AfterTest
  27.     public void teardown(){
  28.         driver.close();
  29.     }
  30.     
  31.     @Test
  32.     public void testRadioButton() throws Exception {
  33.         WebElement femaleRadioButton = driver.findElement(By.xpath("/html/body/div/div[4]/div[7]/form/input[2]"));
  34.         Thread.sleep(5000);
  35.         
  36.         if(! femaleRadioButton.isSelected()){
  37.             femaleRadioButton.click();
  38.         }
  39.         
  40.         Thread.sleep(5000);
  41.         
  42.         assertTrue(femaleRadioButton.isSelected());
  43.     }
  44.     
  45. }
(2)操作页面元素之多选按钮:
  1. package seleniumtest.seleniumtest;

  2. import static org.testng.Assert.assertTrue;
  3. import org.testng.annotations.*;
  4. import org.openqa.selenium.By;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.WebElement;
  7. import org.openqa.selenium.firefox.FirefoxDriver;

  8. public class testCheckbox {
  9.     WebDriver driver = new FirefoxDriver();
  10.     
  11.     @BeforeTest
  12.     public void setup() throws Exception {
  13.         driver.get("");
  14.     }
  15.     
  16.     @Test
  17.     public void testnewCheckbox() throws Exception{
  18.         WebElement bikecheckbox = driver.findElement(By.xpath("/html/body/div/div[4]/div[7]/form/input[1]"));
  19.         if(!bikecheckbox.isSelected()){
  20.             bikecheckbox.click();
  21.         }
  22.         
  23.         Thread.sleep(6000);
  24.         assertTrue(bikecheckbox.isSelected());
  25.         
  26.         WebElement carcheckbox = driver.findElement(By.cssSelector("input[value='Car']"));
  27.         
  28.         if(!carcheckbox.isSelected()){
  29.             carcheckbox.click();
  30.         }
  31.         
  32.         Thread.sleep(6000);
  33.         assertTrue(carcheckbox.isSelected());
  34.         
  35.     }
  36.     
  37.     @AfterTest
  38.     public void teardown(){
  39.         driver.quit();
  40.     }
  41. }
(3)操作弹出窗口之验证标题:
  1. package seleniumtest.seleniumtest;

  2. import static org.testng.Assert.*;
  3. import java.util.Set;
  4. import org.testng.annotations.*;
  5. import org.openqa.selenium.firefox.*;
  6. import org.openqa.selenium.By;
  7. import org.openqa.selenium.WebDriver;
  8. import org.openqa.selenium.WebElement;

  9. public class testMutipleWindowsTitle {
  10.     WebDriver driver = new FirefoxDriver();
  11.     
  12.     @BeforeTest
  13.     public void setup(){
  14.         driver.get("");
  15.     }
  16.     
  17.     @Test
  18.     public void testwin() throws Exception{
  19.         String parentWindowId = driver.getWindowHandle();
  20.         
  21.         Thread.sleep(6000);
  22.         assertEquals("HTML DOM open() 方法",driver.getTitle());
  23.         
  24.         WebElement tryItButton = driver.findElement(By.xpath("/html/body/div/div[4]/div[6]/dl/dt[1]/a"));
  25.         tryItButton.click();
  26.         
  27.         Set <String> allWindowsId = driver.getWindowHandles();
  28.         
  29.         for(String windowId:allWindowsId){
  30.             if(driver.switchTo().window(windowId).getTitle().contains("V2")){
  31.                 driver.switchTo().window(windowId);
  32.                 break;
  33.             }
  34.         }
  35.         assertEquals("W3School在线测试工具 V2",driver.getTitle());
  36.         
  37.         driver.switchTo().window(parentWindowId);
  38.         assertEquals("HTML DOM open() 方法",driver.getTitle());
  39.         Thread.sleep(5000);
  40.     }
  41.     
  42.     @AfterTest
  43.     public void teardown(){
  44.         driver.quit();
  45.     }
  46. }
(4)操作弹出窗口之验证内容:
  1. package seleniumtest.seleniumtest;

  2. import static org.testng.Assert.*;
  3. import org.testng.annotations.*;

  4. import org.openqa.selenium.WebDriver;
  5. import org.openqa.selenium.WebElement;
  6. import org.openqa.selenium.By;
  7. import org.openqa.selenium.firefox.*;
  8. import java.util.Set;

  9. public class testpagecontent {
  10.     WebDriver driver = new FirefoxDriver();
  11.     
  12.     @BeforeTest
  13.     public void setup(){
  14.         driver.get("");
  15.     }
  16.     
  17.     @Test
  18.     public void pagecontent() throws Exception{
  19.         String parenthandler = driver.getWindowHandle();
  20.         
  21.         Thread.sleep(3000);
  22.         assertEquals("HTML DOM open() 方法",driver.getTitle());
  23.         WebElement clickbutton = driver.findElement(By.xpath("/html/body/div/div[4]/div[6]/dl/dt[1]/a"));
  24.         clickbutton.click();
  25.         
  26.         Thread.sleep(3000);
  27.         Set <String> allhandler = driver.getWindowHandles();
  28.         for (String ahandler : allhandler){
  29.             if(driver.switchTo().window(ahandler).getPageSource().contains("打开窗口")){
  30.                 driver.switchTo().window(ahandler);
  31.                 break;
  32.             }
  33.         }
  34.         
  35.         Thread.sleep(3000);
  36.         assertEquals("W3School在线测试工具 V2",driver.getTitle());
  37.         driver.switchTo().window(parenthandler);
  38.         Thread.sleep(3000);
  39.         assertEquals("HTML DOM open() 方法",driver.getTitle());
  40.         
  41.     }
  42.     
  43.     @AfterTest
  44.     public void teardown(){
  45.         driver.quit();
  46.     }
  47. }
阅读(543) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~