• selenium2基本控件介绍及其代码


    输入框:input

     表现形式:
         1.在html中一般为:<input id="user" type="text">
    主要操作:
         1.driver.findElement(By.id("user")).sendKeys("test");
         2.driver.findElement(By.id("user")).clear()
    说明:
        1.sendKeys代表输入,参数为要输入的值
        2.clear代表清除输入框中原有的数据

    超链接:a

    表现形式:
         1.在html中一般为: <a class="baidu" href="http://www.baidu.com">baidu</a>
    主要操作:
         1.driver.findElement(By.xpath("//div[@id='link']/a")).click();
    说明:
        1.click代表点击这个a链接

    下拉菜单:select

    表现形式:

    1.在HTML中一般为:
        <select name="select">
                <option value="volvo">Volvo</option>
                <option value="saab">Saab</option>
               <option value="opel">Opel</option>
               <option value="audi">Audi</option>
        </select>

    主要操作:
        WebElement element = driver.findElement(By.cssSelector("select[name='select']"));
        Select select = new Select(element);
        select.selectByValue("opel");
        select.selectByIndex(2);
        select.selectByVisibleText("Opel");
    说明:
       1.需要一个Select的类
       2.selectByValue的参数为option中的value属性
       3.selectByIndex的参数为option的顺序
       4.selectByVisibleText的参数为option的text值

    单选:radiobox

      表现形式:
         1.在HTML中一般为:<input class="Volvo" type="radio" name="identity">
    主要操作:
         List<WebElement> elements = driver.findElements(By.name("identity"));
         elements.get(2).click();
         boolean select = elements.get(2).isSelected();
    说明:
        1.click代表点击选中这个单选框
        2.isSelected代表检查这个单选框有没有被选中

    多选:checkbox

       表现形式:
         1.在HTML中一般为:<input type="checkbox" name="checkbox1">
    主要操作:
         List<WebElement> elements = driver.findElements(By.xpath("//div[@id='checkbox']/input"));
         WebElement element = elements.get(2);
         element.click();
         boolean check = element.isSelected();
    说明:
        1.click代表点击选中这个多选框
        2.isSelected代表检查这个多选框有没有被选中

    按钮:button

      表现形式:
        1.在HTML中,一般有两种表现形式:
          <input class="button" type="button" disabled="disabled" value="Submit">
          <button class="button" disabled="disabled" > Submit</button>
    主要操作:
         WebElement element = driver.findElement(By.className("button"));
         element.click();
         boolean button = element.isEnabled();
    说明:
        1.click代表点击这个按钮
        2.isEnabled代表检查这个按钮是不是可用的

    Alert 类介绍

       表现形式:
          1.Alert 类,是指windows弹窗的一些操作
    主要操作:
         driver.findElement(By.className("alert")).click();
         Alert alert = driver.switchTo().alert();
         String text = alert.getText();
         alert.accept();
     说明:
          1.先要switch到windows弹窗上面
          2.该Alert类有二个重要的操作getTest(),取得弹窗上面的字符串,accept是指点击确定/ok类的按钮,使弹窗消失

    Action类介绍

      表现形式:
          1.一般是在要触发一些js函数或者一些JS事件时,要用到这个类
          2.<input class="over" type="button" onmouseout="mouseOut()" onmouseover="mouseOver()" value="Action">
    主要操作:
         WebElement element = driver.findElement(By.className("over"));
         Actions action = new Actions(driver);
         action.moveToElement(element).perform();
    说明:
         1.先要new一个Actions的类对象
         2.主要操作大家可以自已去看下API,但是最后的perform()一定要加上,否则执行不成功,切记!

    上传文件操作

     表现形式:
           1.在html中表现为:<input id="load" type="file">
     说明:
           1.一般是把路他径直接sendKeys到这个输入框中
           2.如果输入框被加了readonly属性,不能输入,则需要用JS来去掉readonly属性!

    调用JS介绍

     目的:
           1.执行一段JS,来改变HTML
           2.一些非标准控件无法用selenium2的API时,可以执行JS的办法来取代
    主要操作:
          JavascriptExecutor j = (JavascriptExecutor)driver;
          j.executeScript("alert('hellow rold!')");
     说明:
          1.executeScript这个方法的参数为字符串,为一段JS代码
          2.注意,JS代码需要自已根本项目的需求来编写!

    Iframe操作

    表现形式:

       1.在HTML中为:<iframe width="800" height="330" frameborder="0" src="./demo1.html" name="aa">

    主要操作:
       driver.switchTo().frame("aa");
    说明:
       1.如果iframe标签有能够唯一确定的id或者name,就可以直接用id或者name的值:driver.switchTo().frame("aa");
       2.如果iframe标签没有id或者name,但是我能够通过页面上确定其是第几个(也就是通过index来定位iframe,index是从0开始的):driver.switchTo().frame(0);
       3.也可以通过xpath的方式来定位iframe,写法如下:
          ①WebElement iframe = driver.findElement(By.xpath("//iframe[@name='aa']"));
          ②driver.switchTo().frame(iframe);

    多窗口切换

    表现形式:
          1.在HTML中一般为:<a class="open" target="_bank" href="http://baidu.com">Open new window</a>
    主要操作:
          Set<String> handles = driver.getWindowHandles();
          driver.switchTo().window()
    说明:
          1.getWindowHandles是取得driver所打开的所有的页面的句柄;
          2.switchTo是指切换到相应的窗口中去,window中的参数是指要切过去的窗口的句柄;

    Wait 机制及实现

    目的:

       1.让元素对象在可见后进行操作, 取代Thread.sleep, 使其变成智能等待
    主要操作:
       boolean wait = new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
       public Boolean apply(WebDriver d) {
       return d.findElement(By.className("red")).isDisplayed();
       } });
    说明:
       1.在规定的时间内只要符合条件即返回,上面的代码中是只要isDisplayed即返回;
       2.应用到了WebDriverWait类,这种写法,请大家务必熟练;

    具体代码如下:

    package com.browser.test;
    
    import java.util.List;
    import java.util.Set;
    
    import org.openqa.selenium.Alert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.Select;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class BasicControl {
    	public static WebDriver Driver;
    
    	/**
    	 * @param args
    	 * @throws InterruptedException
    	 */
    	public static void main(String[] args) throws InterruptedException {
    		// TODO Auto-generated method stub
    		startFireFox("file:///D:/BaiduYunDownload/selenium2/demo.html");
    		testWait();
    		// testMultiWindows();
    		// testIframe();
    		// testjs();
    		// testUpload();
    		// testAction();
    		// testAlert();
    		// testButton();
    		// testCheckBox();
    		// testRadioBox();
    		// testSelect();
    		// testLink();
    	}
    
    	public static void testWait() throws InterruptedException {
    		WebElement element = Driver.findElement(By.className("wait"));
    		element.click();
    		// Thread.sleep(6000);
    		boolean wait = new WebDriverWait(Driver, 10)
    				.until(new ExpectedCondition<Boolean>() {
    					public Boolean apply(WebDriver d) {
    						return d.findElement(By.className("red")).isDisplayed();
    					}
    				});
    		element = Driver.findElement(By.className("red"));
    		System.out.println(element.getText());
    	}
    
    	public static void testMultiWindows() throws InterruptedException {
    		WebElement element = Driver.findElement(By.className("open"));
    		element.click();
    		Set<String> handles = Driver.getWindowHandles();// get all handles
    		String handle = Driver.getWindowHandle();// current window handle
    		handles.remove(handle);// remove the first handle(current window handle)
    
    		Driver.switchTo().window(handles.iterator().next());
    		Thread.sleep(2000);
    		Driver.findElement(By.id("kw")).sendKeys("mystring");
    		Thread.sleep(2000);
    		Driver.close();
    		// Driver.quit();// all webDriver will be closed.
    
    		Driver.switchTo().window(handle);
    		Thread.sleep(2000);
    		Driver.findElement(By.id("user")).sendKeys("new test");
    	}
    
    	public static void testIframe() throws InterruptedException {
    		Driver.findElement(By.id("user")).sendKeys("test");
    		WebElement element = Driver.findElement(By
    				.xpath("//iframe[@name='aa']"));
    		Driver.switchTo().frame(element);
    		// Driver.switchTo().frame("aa");
    		// Driver.switchTo().frame(0);
    		Driver.findElement(By.id("user")).sendKeys("ifreame test");
    		Thread.sleep(1000);
    		Driver.switchTo().defaultContent();
    		Driver.findElement(By.id("user")).sendKeys("new test");
    	}
    
    	public static void testjs() {
    		Driver.get("http://www.haosou.com/");
    		String ret = (String) ((JavascriptExecutor) Driver)
    				.executeScript("return document.getElementById('search-button').value;");
    		System.out.println(ret);
    
    		JavascriptExecutor j = (JavascriptExecutor) Driver;
    		j.executeScript("alert('hellow rold!')");
    	}
    
    	public static void testUpload() {
    		WebElement element = Driver.findElement(By.id("load"));
    		element.sendKeys("D:\BaiduYunDownload\selenium2\第五周 selenium2常用类介绍\第五周");
    	}
    
    	public static void testAction() {
    		WebElement element = Driver.findElement(By.className("over"));
    		Actions action = new Actions(Driver);
    		action.moveToElement(element).perform();
    		System.out.println(Driver.findElement(By.id("over")).getText());
    		// action.click(element).perform();
    		// System.out.println(Driver.findElement(By.id("over")).getText());
    	}
    
    	public static void testAlert() {
    		Driver.findElement(By.className("alert")).click();
    		Alert alert = Driver.switchTo().alert();
    		System.out.println(alert.getText());
    		alert.accept();
    
    		WebElement element = Driver.findElement(By.className("alert"));
    		Actions action = new Actions(Driver);
    		action.click(element).perform();
    		Alert alert1 = Driver.switchTo().alert();
    		System.out.println(alert1.getText());
    		alert1.accept();
    	}
    
    	public static void testButton() {
    		WebElement element = Driver.findElement(By
    				.xpath("//div[@id='button']/input"));
    		System.out.println("The button is enabled:" + element.isEnabled());
    		element.click();
    		System.out.println("The button is selected:" + element.isSelected());
    	}
    
    	public static void testCheckBox() {
    		List<WebElement> elements = Driver.findElements(By
    				.xpath("//div[@id='checkbox']/input"));
    		System.out.println("The second checkbox is selected:"
    				+ elements.get(2).isSelected());
    		elements.get(2).click();
    		System.out.println("The second checkbox is selected:"
    				+ elements.get(2).isSelected());
    		for (int i = 0; i < elements.size(); i++) {
    			if (elements.get(i).isSelected() == false) {
    				elements.get(i).click();
    			}
    		}
    	}
    
    	public static void testRadioBox() {
    		List<WebElement> elements = Driver.findElements(By.name("identity"));
    		elements.get(2).click();
    		System.out.println(elements.get(2).isSelected());
    
    	}
    
    	public static void testSelect() {
    		Select select = new Select(Driver.findElement(By
    				.xpath("//select[@name='select']")));
    		select.selectByValue("audi");
    		select.selectByIndex(2);
    		select.selectByVisibleText("Audi");
    	}
    
    	public static void testLink() {
    		Driver.findElement(By.xpath("//div[@id='link']/a")).click();
    	}
    
    	public static void testInput(String inputText) {
    		Driver.findElement(By.id("user")).sendKeys(inputText);
    		Driver.findElement(By.id("user")).clear();
    	}
    
    	public static void startFireFox(String url) {
    		Driver = new FirefoxDriver();
    		Driver.manage().window().maximize();
    		Driver.navigate().to(url);
    	}
    
    	public static void closeFireFox() {
    		Driver.close();
    		Driver.quit();
    	}
    }
    

    最后打个广告,不要介意哦~

    最近我在Dataguru学了《软件自动化测试Selenium2》网络课程,挺不错的,你可以来看看!要是想报名,可以用我的优惠码 G863,立减你50%的固定学费!

    链接:http://www.dataguru.cn/invite.php?invitecode=G863

  • 相关阅读:
    RPM Repositories for CentOS 6, 7 and 8
    oracle user clone script
    在 Linux 上安装和配置 VNC 服务器
    阿里云ORACLE RAC共享磁盘组扩容实战笔记
    如何在 SQL Server 中创建和配置链接服务器以连接到 MySQL
    oracle 11.2.0.4 windows64位下载
    How To Drop And Recreate Temp Tablespace In Oracle
    查询mysql没有主键的表
    hadoop常用端口及定义方法
    MySQL主从复制中问题排查过程总结
  • 原文地址:https://www.cnblogs.com/yajing-zh/p/4898688.html
Copyright © 2020-2023  润新知