`
zengshaotao
  • 浏览: 752689 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java+selenium2 自动化测试框架

 
阅读更多

package web.selenium2;

 

 

import junit.framework.TestCase;

 

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.openqa.selenium.interactions.Actions;

 

/**

 * 自动化测试:页面操作的基本功能

 * @author Administrator

 */

public class FirstCase  extends TestCase {

//声明一个chrome driver对象

    WebDriver driver = null;

    

    //每个测试方法执行前执行

    public void setUp() throws Exception{

        System.out.println("init......");

        //将当前浏览器的测试驱动注入到webdriver.chrome.driver中

        System.setProperty("webdriver.chrome.driver", "C:/Users/Administrator/Desktop/python/chromedriver.exe");

        driver = new ChromeDriver();

    }

    //每个测试方法结束后执行

    public void tearDown() throws Exception{

        System.out.println("destory......");

        //关闭浏览器

        driver.quit();

    }

 

    //测试百度登录

    public void testBaiduSearch() {

        //指定窗口的位置

        //Point p = new Point(300, 300);

        //driver.manage().window().setPosition(p);

        

        //打开baidu

        driver.get("http://www.baidu.com");

        

        //将后台脚本注入到前台执行

        String js ="alert(\"hello,this is a alert!\")";

        //firefox没有Object[]参数

        //((JavascriptExecutor) driver).executeScript(js,new Object[]{});

        

        //等待页面弹出的alert窗口关闭,否则后续代码执行窗口事件不匹配,测试不通过

        /*try {

Thread.sleep(2000);

} catch (InterruptedException e1) {

e1.printStackTrace();

}*/

        //定位搜索框

        WebElement searchInput = driver.findElement(By.id("kw"));

        //搜索框输入关键字

        //CharSequence c= "12";//不能通过new创建

        CharSequence cs[] = { "python"};

        searchInput.sendKeys(cs);

        //定位搜索按钮

        WebElement searchButton = driver.findElement(By.id("su"));

        //点击搜索按钮

        searchButton.click();

        try {

            Thread.sleep(2000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        //关闭浏览器

        driver.quit();

    }

    

    //测试ITeye登录

    public void testITeyeLogin() {

        

    try{

       driver.manage().window().maximize();//窗口最大化

       //打开iteye

       driver.get("http://zengshaotao.iteye.com/login");

       System.out.println(driver.getTitle());

       //定位搜索框

       WebElement usernameInput = driver.findElement(By.id("user_name"));

       WebElement pwdInput = driver.findElement(By.id("password"));

       CharSequence username[] = { "zengshaotao"};

       CharSequence password[] = { "******"};

       //在页面上输入用户名和密码

       usernameInput.sendKeys(username);

       pwdInput.sendKeys(password);

       //定位登录按钮

       WebElement searchButton = driver.findElement(By.id("button"));

       //searchButton.getText();

       //点击登录按钮

       searchButton.click();

       

       //此处是刚登录后页面内容的检查

       //自动化操作代码需要与具体的页面场景相对应,也即这里是登录成功后的页面场景,不能定位登录之前的页面内容

       //否则会因为定位不到出现错误提示

       WebElement loginResult = driver.findElement(By.id("user_visits"));

       //从登录结果页面定位user_visits对象

       String cssValue = loginResult.getAttribute("class");

       if("clearfix".equals(cssValue)){

       System.out.println("登录成功。。。。。");

       }else{

       System.out.println("登录失败。。。。。");

       }

       //如果元素显示了返回true,否则返回false

       if(loginResult.isDisplayed()){

       System.out.println("元素已经显示。。。。。。");

       }

       

       //通过class属性在搜索框中输入搜索内容,并点击搜索图标

       driver.findElement(By.cssSelector(".search_text")).sendKeys(new CharSequence[]{"java"});

       driver.findElement(By.cssSelector(".submit_search")).click();

       

       //元素是否被选择

       //WebElement checkbox = driver.findElement(By.id("checkbox_id"));

       //checkbox.isSelected();//会有返回值,如果勾选了。返回true,如果没有勾选返回false。

       

       //元素是否启用

       //WebElement enableEle = driver.findElement(By.id("loginBtn"));

       //enableEle.isEnabled();

       

       //提交表单的内容

       //<button class="btn btn-major" id="loginBtn" type="submit">登录</button>

       //WebElement submitEle = driver.findElement(By.id("submitBtn"));

       //submitEle.submit();

       

       //如果元素是处于iframe中,需要先切入到iframe里再定位具体的元素

       //driver.switchTo().frame("id");//传入的是iframe的id

       //driver.switchTo().defaultContent();//切入到默认的iframe

       

       //操作下拉框select

       /*WebElement element_province = driver.findElement(By.id("province"));

       Select province = new Select(element_province);

       province.selectByIndex(0); //根据所选值的位置来选择下拉框值,从0开始

       province.selectByValue("22");//根据value值来选择下拉框的值

       province .selectByVisibleText("北京");//根据可见文本来操作下拉菜单*/        

       

    }catch(Exception e){

    e.printStackTrace();

    }

        

    }

    //模拟实际鼠标的移动操作执行自动化测试

    public void testAction(){

   

    driver.get("https://www.baidu.com/");

    By inputText = By.id("kw");

        By searchButton = By.id("su");

        //实例化action对象

        Actions action = new Actions(driver);

        CharSequence contents[] = { "java"};

        //通过action模拟键盘输入java关键字到 输入框,只有使用了perform方法才会输入进去

        action.sendKeys(driver.findElement(inputText), contents).perform();

        //鼠标模拟移动到搜索按钮

        action.moveToElement(driver.findElement(searchButton)).perform();

        //模拟点击操作

        action.click().perform();

    }

 

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics