WebDriver高级应用实例(1)

  1.1使用JavaScriptExecutor单击元素

  被测网页的网址:

  http://www.baidu.com

  Java语言版本的API实例代码

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;

public class TestDemo {
    WebDriver driver;
    String url = "http://www.baidu.com";
  @Test
  public void testHandleiFrame() throws Exception {
      WebElement searchInputBox = driver.findElement(By.id("kw"));
      WebElement searchButton = driver.findElement(By.id("su"));
      searchInputBox.sendKeys("使用JavaScript语句进行页面元素单击");
      JavaScriptClick(searchButton);
  }
  public void JavaScriptClick(WebElement element)throws Exception {
      try {
          //判断传入的参数是否处于页面上及是否可单击
        if(element.isEnabled() && element.isDisplayed()){
            System.out.println("使用JavaScript语句进行页面元素单击");
            ((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
            
        }else{
            System.out.println("页面上的元素无法进行单击操作");
        }
    } catch (StaleElementReferenceException e) {
            System.out.println("页面元素没有附加在网页中"+e.getStackTrace());
    }catch(NoSuchElementException e){
        System.out.println("在页面中没有找到要操作的页面元素"+e.getStackTrace());
    }catch(Exception e){
        System.out.println("无法完成单击动作"+e.getStackTrace());
    }
    
  }
@BeforeMethod
  public void beforeMethod() {
      System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
      driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.get(url);
  }

  @AfterMethod
  public void afterMethod() {
      driver.quit();
  }

}

  代码解释:

  JavaScriptClick方法里面的代码实现的是一种封装,把常用的操作写在一个函数方法里,可以方便调用,减少冗余代码的编写,提高测试代码的编写效率

  1.2在Ajax方法产生的浮动框中,单击选择包含某个关键字的选项

  被测网页的网址:

  https://www.sogou.com/

  Java语言版本的API实例代码

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Ajax {
    WebDriver driver;
    String url = "https://www.sogou.com/";
  @Test
  public void testAjaxDivOption() throws Exception {
      //设置显示等待最长为10秒
      WebDriverWait wait = new WebDriverWait(driver,10);
      WebElement serchInputBox = driver.findElement(By.id("query"));
      serchInputBox.click();
      /*因为浮动框的内容总是发生改变如果想选择其中的第几项可以参考注释代码
      WebElement serchInputBox1 = driver.findElement(By.xpath("//*[@id='vl']/div[1]/ul/li[1]"));
      serchInputBox1.click();*/
      //显示等待浮动框的第一个值  防止因页面加载未找到元素而导致失败,未等待可以会因为程序运行过快浮动框的值还未出现,后面的列表可能会取不到值
     wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='vl']/div[1]/ul/li[1]")));
      List<WebElement>suggetionOptions = driver.findElements(By.xpath("//*[@id='vl']/div[1]/ul/li"));
      //遍历浮动框的值
      for(WebElement element:suggetionOptions){
          //找到指定内容项
          if(element.getText().contains("康宁研发柔性玻璃")){
              System.out.println(element.getText());
              element.click();
              break;
          }
      }
     
  }
  @BeforeMethod
  public void beforeMethod() {
      System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
      driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.get(url);
  }

  @AfterMethod
  public void afterMethod() {
      driver.quit();
  }
}

  代码解释:

    此代码主要是测试页面包含Ajax的局部刷新机制,产生显示多条数据的浮动框,因为浮动框的内容总是发生改变如果想选择其中的第几项可以参考注释代码通过数字进行选择

  1.3设置一个页面的属性

  被测网页的网址源码: 

  <html>
    <head>
      <title>设置文本框属性</title>
    </head>
    <body>
      <input id='text' type='text' value='今年夏天西瓜相当甜!' size=100/>文本框</input>
    </body>
  </html>

  Java语言版本的API实例代码  

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 org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class dataPicker {
    WebDriver driver;
    String url = "file:///E:/%E6%9D%90%E6%96%99/selenium/%E5%B1%9E%E6%80%A7.html";
  @Test
  public void testdataPicker() {
      WebElement textInputBox = driver.findElement(By.id("text"));
      //调用方法改变文本框中的文字
      setAttribute(driver,textInputBox,"value","文本框的文字属性和长度已被改变");
      //调用方法改变文本框的长度
      setAttribute(driver, textInputBox, "size", "10");
      //删除文本框的size属性
      removeAttribute(driver,textInputBox,"size");
  }

  public void setAttribute(WebDriver driver, WebElement element, String attributeName, String value) {
      //调用JavaScript代码修改页面元素的属性
      JavascriptExecutor js = (JavascriptExecutor) driver;
      js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", element,attributeName,value);
  }
 public void removeAttribute(WebDriver driver, WebElement element, String attributeName) {
     JavascriptExecutor js = (JavascriptExecutor) driver;
     js.executeScript("arguments[0].removeAttribute(arguments[1],arguments[2])", element,attributeName);
}
@BeforeMethod
  public void beforeMethod() {
      System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
      driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.get(url);
  }

  @AfterMethod
  public void afterMethod() {
      //driver.quit();
  }
}
posted @ 2019-03-07 10:30  心生意动  阅读(163)  评论(0编辑  收藏  举报