zoukankan      html  css  js  c++  java
  • 基于Selenium2+Java的UI自动化(4)

    1. 启动浏览器

    前边有详细介绍启动三种浏览器的方式(IE、Chrome、Firefox);

            private WebDriver driver = null;
    
    	private String chromeDriverDir = "D:\workspace\A_Test\resource\chromedriver.exe";
    	
    	/**
    	 * 打开谷歌浏览器;
    	 */
    	public void openChromeBrowser(){
    		System.setProperty("webdriver.chrome.driver", chromeDriverDir);
    		driver = new ChromeDriver();
    	}

    2.访问网页地址

    方式一:

            /**
    	 * 访问网页地址方式一;
    	 */
    	public void visitURL1(){
    		String baseUrl = "http://www.baidu.com/";
    		driver.get(baseUrl);
    	}

    方式二:

           /**
    	 * 访问网页地址方法二;
    	 */
    	public void visitURL2(){
    		String baseUrl = "http://www.sogou.com/";
    		driver.navigate().to(baseUrl);
    	}

    3. 模拟后退功能

            /**
    	 * 模拟后退功能;
    	 */
    	public void visitRecentUrl(){
    		String url1 = "http://www.baidu.com/";
    		String url2 = "http://www.sogou.com/";
    		driver.navigate().to(url1);       //先访问百度
    		driver.navigate().to(url2);       //再访问搜狗
    		driver.navigate().back();         //返回上一次返回的百度页面;
    	}

    4.模拟前进功能

            /**
    	 * 模拟前进功能;
    	 */
    	public void visitNextUrl(){
    		String url1 = "http://www.baidu.com/";
    		String url2 = "http://www.sogou.com/";
    		driver.navigate().to(url1);           //先访问百度
    		driver.navigate().to(url2);           //再访问搜狗
    		driver.navigate().back();             //返回上一次返回的百度页面;
    		driver.navigate().forward();          //从百度页面跳转的搜狗页面;
    	}

    5. 刷新页面

            /**
    	 * 刷新当前页面;
    	 */
    	public void refreshCurrentPage(){
    		String baseUrl = "http://www.sogou.com/";
    		driver.navigate().to(baseUrl);
    		driver.navigate().refresh();       //刷新当前页面
    	}

    6.窗口最大化

            /**
    	 * 窗口最大化
    	 */
    	public void maxWindows(){
    		String baseUrl = "http://www.sogou.com/";
    		driver.navigate().to(baseUrl);
    		driver.manage().window().maximize();    //窗口最大化;
    	}

    7.获取当前页面的title属性值

            /**
    	 * 获取当前页面的title属性值;
    	 */
    	public void getTitle(){
    		String url = "http://www.baidu.com/";
    		driver.navigate().to(url);
    		String title = driver.getTitle();  //获取当前页面的title值;
    		System.out.println(title);
    	}

    8.获取当亲页面的源代码

            /**
    	 * 获取当前页面的源代码;
    	 */
    	public void getPageSource(){
    		String url = "http://www.baidu.com/";
    		driver.navigate().to(url);
    		String source = driver.getPageSource();  //获取当前页面的源代码;
    		System.out.println(source);
    	}

    9. 获取当前页面的网址

            /**
    	 * 获取当前页面的网址;
    	 */
    	public void getCurrentUrl(){
    		String url = "http://www.baidu.com/";
    		driver.navigate().to(url);
    		String currentUrl = driver.getCurrentUrl();  //获取当前页面的网址;
    		System.out.println(currentUrl);
    	}

    10. 在输入框中清空原有的文字内容

            /**
    	 * 清空输入框原有的文字内容
    	 */
    	public void clearText(){
                    //获取输入框对象;
    		WebElement inputText = driver.findElement(By.id("kw"));
    		//清空输入框中的默认文字
    		inputText.clear();
    	}

    11.在输入框中输入指定文本内容

            /**
    	 * 在输入框中输入指定文本
    	 */
    	public void inputText(){
    		//获取输入框对象;
    		WebElement inputText = driver.findElement(By.id("kw"));
    		//编辑需要输入的文本;
    		String text = "UI 自动化";
    		//在输入框中的输入文本
    		inputText.sendKeys(text);  //sendKeys()方法,是用于输入;
    	}

    12.单击按钮

            /**
    	 * 单击按钮
    	 */
    	public void clickButton(){
    		//获取按钮对象;
    		WebElement button = driver.findElement(By.id("su"));
    		//判断按钮是否可用
    		boolean isEnabled = button.isEnabled();
    		//如果按钮可以点击,就点击按钮;
    		if(isEnabled){
    			button.click();
    		}
    	}

    13.双击元素

            /**
    	 * 双击某个元素
    	 */
    	public void doubleClick(){
    		//获取输入框对象;
    		WebElement inputText = driver.findElement(By.id("kw"));
    		//声明Action对象
    		Actions action = new Actions(driver);
    		//使用doubleClick方法,双击输入框;
    		action.doubleClick(inputText).build().perform();
    	}

    14.右击元素

    	/**
    	 * 右击某个元素
    	 */
    	public void rightClick(){
    		//获取输入框对象;
    		WebElement inputText = driver.findElement(By.id("kw"));
    		//声明Action对象
    		Actions action = new Actions(driver);
    		//使用contextClick方法,右击输入框;
    		action.contextClick(inputText).build().perform();
    	}

    15.操作单选下拉列表

    (1)使用下拉列表的下标选择子选项;

    	/**
    	 * 通过下标选择下拉框的值;
    	 */
    	public void operateDropListByIndex(){
    		//获取下拉列表元素对象;
    		WebElement selectElement = driver.findElement(By.id("session_kept"));
    		//声明Select对象;
    		Select select = new Select(selectElement);
    		//通过子选项的下标来选择:下标从0开始
    		select.selectByIndex(0);  //表示选择第一个子选项:不保存登录状态;
    	}

    (2)使用value选择;

    	/**
    	 * 通过子选项的value选择下拉框的值;
    	 */
    	public void operateDropListByValue(){
    		//获取下拉列表元素对象;
    		WebElement selectElement = driver.findElement(By.id("session_kept"));
    		//声明Select对象;
    		Select select = new Select(selectElement);
    		//通过子选项的选项的value值来选择:
    		String value = "60" ; //例如:“保存一小时 ” 这个选项的value值是:60
    		select.selectByValue(value); 
    	}

    (3)通过可见文本选择;

    	/**
    	 * 通过子选项的可见文本选择下拉框的值;
    	 */
    	public void operateDropListByVisibleText(){
    		//获取下拉列表元素对象;
    		WebElement selectElement = driver.findElement(By.id("session_kept"));
    		//声明Select对象;
    		Select select = new Select(selectElement);
    		//通过子选项的选项的value值来选择:
    		String visibleText = "保存一天" ; //例如:第6个选项的 可见文本是:“保存1天 ” ;
    		select.selectByVisibleText(visibleText); 
    	}

    16.操作链接

    	/**
    	 * 点击链接对象;
    	 */
    	public void clickLinkText(){
    		//获取链接元素对象;
    		WebElement link = driver.findElement(By.linkText("快速注册"));
    		//点击链接对象;
    		link.click();
    	}

    17.操作单选按钮

    	/**
    	 * 操作单选框RadioButton
    	 */
    	public void clickRadioButton(){
    		//获取单选按钮元素对象;
    		WebElement radio = driver.findElement(By.id("identity"));
    		//判断单选按钮是否已经被选中;
    		boolean isSelect = radio.isSelected();
    		//如果没有选中,就点击,如果已经选中,就不操作;
    		if(!isSelect){
    			radio.click();
    		}
    	}

    18.操作复选框

    	/**
    	 * 操作复选框CheckBox
    	 */
    	public void clickCheckBox(){
    		//获取复选框元素对象;
    		WebElement checkbox = driver.findElement(By.id("checked"));
    		//判断复选框是否已经被选中;
    		boolean isSelect = checkbox.isSelected();
    		//如果没有选中,就点击,如果已经选中,就不操作;
    		if(!isSelect){
    			checkbox.click();
    		}
    	}


    我们专注于持续集成,更多原创请关注:www.hordehome.com

  • 相关阅读:
    nodejs日志管理log4js
    js数组去重的几种方法
    Echarts同一页面多个图表自适应浏览器窗口大小——window.onresize
    Echarts自适应浏览器大小
    [原创]Nodejs 远程执行linux shell
    ssh无需密码登录linux服务器
    Nodejs mysql pool使用实例
    Activity基础知识
    最大熵模型介绍
    node-sass element-plus 安装失败
  • 原文地址:https://www.cnblogs.com/hordehome/p/6186627.html
Copyright © 2011-2022 走看看