selenium2 定位 窗体切换等等 (二)

定位用的html素材有两个

demo.html

<html>
<head>
    <title>UI Automation Testing</title>
</head>
<style type="text/css">
    table{font-size:20;text-align:center;width:80%;border-collapse:collapse;margin:auto}    
    table th{background: #ccc}
    .display{color:red}
    .widgetStyle{text-align:left;padding-left:100px}    
</style>
<script>
function display_alert(){
  alert("I am an alert box!!")
}
function show_div(){   
    setTimeout("create_div()", 3000);   
}      
function create_div(){   
    d = document.createElement('div');     
    d.className = "red";  
    d.style.color='red';
    d.innerHTML="wait for display";
    document.getElementById("display").appendChild(d);   
}   
function mouseOver(){
    document.getElementById("over").innerHTML="<div class='display'>Hello World!</div>";
}
function mouseOut(){
    document.getElementById("over").innerHTML="";
}
</script>
<body>
<div>
    <div class="text" style="font-size:36;text-align:center"><b>UI Automation Testing</b></div>
    <br/>
    <table cellpadding="10" cellspacing="0" border="1">
        <thead>
            <tr>
                <th width=20% class="widgetleft">Widget Name</th>
                <th width=80% class="widgetStyle">Widget Operate</th>                
            </tr>
        </thead>
                <tbody>
                    <tr>
                        <td>Input</td>
                        <td class="widgetStyle"><div id='input'><input type="text" id="user"/></div></td>                                            
                    </tr>
                    <tr>
                        <td>Link</td>
                        <td class="widgetStyle"><div id='link'><a href="http://www.baidu.com" class='baidu'>baidu</a></div></td>                        
                    </tr>
                    <tr>
                        <td>Select</td>
                        <td class="widgetStyle"><div id='select'>
                                <select name='select'>
                                  <option value ="volvo">Volvo</option>
                                  <option value ="saab">Saab</option>
                                  <option value="opel">Opel</option>
                                  <option value="audi">Audi</option>
                                </select>
                            </div>
                        </td>                        
                    </tr>
                    <tr>
                        <td>RadioBox</td>
                        <td class="widgetStyle">
                            <div id='radio'>
                                <input type='radio' name="identity" class='Volvo'/><label>Volvo</label></br>
                                <input type='radio' name="identity" class='Saab'/><label>Saab</label></br>
                                <input type='radio' name="identity" class='Opel'/><label>Opel</label></br>
                                <input type='radio' name="identity" class='Audi'/><label>Audi</label>
                            </div>
                        
                        </td>                        
                    </tr>
                    <tr>
                        <td>CheckBox</td>
                        <td class="widgetStyle">
                            <div id='checkbox'>
                                <input type="checkbox" name="checkbox1"/><label>Volvo</label></br>
                                <input type="checkbox" name="checkbox2"/><label>Saab</label></br>
                                <input type="checkbox" name="checkbox3"/><label>Opel</label></br>
                                <input type="checkbox" name="checkbox4"/><label>Audi</label> 
                            </div>
                        </td>                        
                    </tr>
                    <tr>
                        <td>Button</td>
                        <td class="widgetStyle"><div id='button'><input type='button' class='button' value='Submit' disabled="disabled"/></div></td>                        
                    </tr>
                    <tr>
                        <td>Alert</td>
                        <td class="widgetStyle"><div id='alert'><input type='button' class='alert' value='Alert' onclick='display_alert()'/></div></td>                        
                    </tr>
                    <tr>
                        <td>Upload</td>
                        <td class="widgetStyle"><div id='upload'><input type='file' id='load'/></div></td>                        
                    </tr>
                    <tr>
                        <td>Open New Window</td>
                        <td class="widgetStyle"><div id='open'><a href="http://baidu.com" class='open' target='_bank'>Open new window</a></div></td>                        
                    </tr>
                    <tr>
                        <td>Action</td>
                        <td class="widgetStyle"><div id='action'>
                                <input type='button' class='over' value='Action'onmouseover="mouseOver()" onmouseout="mouseOut()"/><div id='over'></div>
                            </div>
                        </td>                        
                    </tr>
                    <tr>
                        <td>Wait</td>
                        <td class="widgetStyle">
                            <div id='wait'><input type='button' class='wait' value='Wait' onclick = "show_div()"/></div>
                            <div id='display'></div>
                        </td>                        
                    </tr>
                    <tr>
                        <td>Iframe</td>
                        <td class="widgetStyle">
                            <iframe width=800 height=330 name=aa frameborder=0 src="./demo1.html"></iframe>
                        </td>                        
                    </tr>
                </tbody>
            
        
    </table>
    <br/>
</div>
</body>
</html>
View Code

demo1.html

<html>
<head>
    <title>UI Automation Testing</title>
</head>
<style type="text/css">
    table{font-size:20;text-align:center;width:80%;border-collapse:collapse;margin:auto}    
    table th{background: #ccc}
    .display{color:red}
    .widgetStyle{text-align:left;padding-left:100px}    
</style>
<script>
function display_alert(){
  alert("I am an alert box!!")
}
function show_div(){   
    setTimeout("create_div()", 5000);   
}      
function create_div(){   
    d = document.createElement('div');     
    d.className = "red";  
    d.style.color='red';
    d.innerHTML="wait for display";
    document.getElementById("display").appendChild(d);   
}   
function mouseOver(){
    document.getElementById("over").innerHTML="<div class='display'>Hello World!</div>";
}
function mouseOut(){
    document.getElementById("over").innerHTML="";
}
</script>
<body>
<div>
    <div class="text" style="font-size:36;text-align:center"><b>UI Automation Testing</b></div>
    <br/>
    <table cellpadding="10" cellspacing="0" border="1">
        <thead>
            <tr>
                <th width=20% class="widgetleft">Widget Name</th>
                <th width=80% class="widgetStyle">Widget Operate</th>                
            </tr>
        </thead>
                <tbody>
                    <tr>
                        <td>Iframe:Input</td>
                        <td class="widgetStyle"><div id='input'><input type="text" id="user"/></div></td>                                            
                    </tr>
                    <tr>
                        <td>Iframe:Link</td>
                        <td class="widgetStyle"><div id='link'><a href="http://www.baidu.com" class='baidu'>baidu</a></div></td>                        
                    </tr>
                    <tr>
                        <td>Iframe:Select</td>
                        <td class="widgetStyle"><div id='select'>
                                <select name='select'>
                                  <option value ="volvo">Volvo</option>
                                  <option value ="saab">Saab</option>
                                  <option value="opel">Opel</option>
                                  <option value="audi">Audi</option>
                                </select>
                            </div>
                        </td>                        
                    </tr>
                    <tr>
                        <td>Iframe:RadioBox</td>
                        <td class="widgetStyle">
                            <div id='radio'>
                                <input type='radio' name="identity" class='Volvo'/><label>Volvo</label></br>
                                <input type='radio' name="identity" class='Saab'/><label>Saab</label></br>
                                <input type='radio' name="identity" class='Opel'/><label>Opel</label></br>
                                <input type='radio' name="identity" class='Audi'/><label>Audi</label>
                            </div>
                        
                        </td>                        
                    </tr>
                    <tr>
                        <td>Iframe:CheckBox</td>
                        <td class="widgetStyle">
                            <div id='checkbox'>
                                <input type="checkbox" name="checkbox1"/><label>Volvo</label></br>
                                <input type="checkbox" name="checkbox2"/><label>Saab</label></br>
                                <input type="checkbox" name="checkbox3"/><label>Opel</label></br>
                                <input type="checkbox" name="checkbox4"/><label>Audi</label> 
                            </div>
                        </td>                        
                    </tr>
                    <tr>
                        <td>Iframe:Button</td>
                        <td class="widgetStyle"><div id='button'><input type='button' class='button' value='Submit' disabled="disabled"/></div></td>                        
                    </tr>
                    <tr>
                        <td>Iframe:Alert</td>
                        <td class="widgetStyle"><div id='alert'><input type='button' class='alert' value='Alert' onclick='display_alert()'/></div></td>                        
                    </tr>
                    <tr>
                        <td>Iframe:Upload</td>
                        <td class="widgetStyle"><div id='upload'><input type='file' id='load'/></div></td>                        
                    </tr>
                    <tr>
                        <td>Iframe:Open New Window</td>
                        <td class="widgetStyle"><div id='open'><a href="http://baidu.com" class='open' target='_bank'>Open new window</a></div></td>                        
                    </tr>
                    <tr>
                        <td>Iframe:Action</td>
                        <td class="widgetStyle"><div id='action'>
                                <input type='button' class='over' value='Action'onmouseover="mouseOver()" onmouseout="mouseOut()"/><div id='over'></div>
                            </div>
                        </td>                        
                    </tr>
                    <tr>
                        <td>Iframe:Wait</td>
                        <td class="widgetStyle">
                            <div id='wait'><input type='button' class='wait' value='Wait' onclick = "show_div()"/></div>
                            <div id='display'></div>
                        </td>                        
                    </tr>                    
                </tbody>
            
        
    </table>
    <br/>
</div>
</body>
</html>
View Code

定位代码:

代码1:

package info.milan.webdriver;
import java.io.File;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class day4 {
    public WebDriver Driver;
    public void startFirefox(){
        Driver = new FirefoxDriver();
        //最大化
        Driver.manage().window().maximize();
    }
    public void closeFirefox(){
        Driver.close();
        Driver.quit();
    }
    public void goTo(){
        File f = new File("html/demo.html");
        Driver.navigate().to(f.getAbsolutePath());
    }
    //输入框定位
    public void testInput(String str){
        ///对于文本框的操作,都使用sendkeys来操作
        Driver.findElement(By.xpath(".//*[@id='user']")).sendKeys(new String[]{str});
        //Driver.findElement(By.xpath(".//*[@id='user']")).clear();
    }
    //超链接定位
    public void testLink(){
        Driver.findElement(By.xpath("//a[@class='baidu']")).click();
    }
    //下拉框定位
    public void testOption(){
        WebElement element= Driver.findElement(By.cssSelector("select[name='select']"));
        Select select = new Select(element);
        select.selectByValue("audi");
        select.selectByIndex(1);
        select.selectByVisibleText("Audi");
        System.out.println(select.getFirstSelectedOption().getText());
        List<WebElement> options= select.getOptions();
        int optionsSize = options.size();
        System.out.println("options一共有多少个:"+optionsSize);
        for(int i =0;i<optionsSize;i++){
            select.selectByIndex(i);
        }
    }
    //单选框定位
    public void testRadio(){
        //name定位
        List<WebElement> elements= Driver.findElements(By.name("identity"));
        WebElement el= elements.get(2);
        el.click();
        boolean isSelect = el.isSelected();
        System.out.println("是否被选中:"+elements.get(3).isSelected());
    }
    //定位复选框
    public void testCheckBox(){
        //xpath定位
        List<WebElement> elements= Driver.findElements(By.xpath(".//div[@id='checkbox']/input"));
        elements.get(2).click();
//        elements.get(0).click();
        for (WebElement e : elements){
            if (!e.isSelected()){
                e.click();
            }
        }
    }
    public void testButton(){
        WebElement element= Driver.findElement(By.xpath(".//div[@id='alert']/input"));
        element.click();
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        day4 t = new day4();
        t.startFirefox();
        t.goTo();
        t.testInput("你好");    
        //t.testLink();
        t.testCheckBox();
        t.testRadio();
    }

}

代码2:

package info.milan.webdriver;
import java.io.File;
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.WebDriverWait;
public class day5 {
    public WebDriver Driver;
    public void startFirefox(){
        Driver = new FirefoxDriver();
        Driver.manage().window().maximize();
    }
    public void closeFirefox(){
        Driver.close();
        Driver.quit();
    }
    public void goTo(){
        File f = new File("html/demo.html");
        Driver.navigate().to(f.getAbsolutePath());
    }
    //模拟点击弹出窗alert的确定。
    public void testAlert(){
        WebElement element= Driver.findElement(By.className("alert"));
        //element.click();
        Actions action = new Actions(Driver);
        action.click(element).perform();
        //获取当前弹出的alert窗
        Alert alert = Driver.switchTo().alert();
        String alertMessage  = alert.getText();
        System.out.println(alertMessage);
        ///模拟点击alert确认按钮
        alert.accept();
    }
    public void testAction(){
        WebElement element= Driver.findElement(By.className("over"));
        Actions action = new Actions(Driver);
        //将鼠标移动到Action的按钮上面
        action.moveToElement(element).perform();
        //输出鼠标移动到action按钮上面后显示的文字。
        System.out.println( Driver.findElement(By.id("over")).getText());;
    }
    //上传
    public void testUpload(){
        WebElement element= Driver.findElement(By.id("load"));
        element.sendKeys("C:\\Users\\Administrator\\Desktop\\123.xls");
    }
    //运行一段js
    public void testJs(){
        JavascriptExecutor javascript =(JavascriptExecutor)Driver;
        javascript.executeScript("alert('你好!')");
        
    }
    //对iframe中元素的操作
    public void testIframe(){
        Driver.findElement(By.id("user")).sendKeys("my test");
        
        WebElement element = Driver.findElement(By.name("aa"));
        Driver.switchTo().frame(element);
        //Driver.switchTo().frame("aa");
        //Driver.switchTo().frame(0);
        Driver.findElement(By.id("user")).sendKeys("iframe test");
        
        Driver.switchTo().defaultContent();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Driver.findElement(By.id("user")).sendKeys("my end test");
    }
    //对多个窗口进行切换操作
    public void testMultiWindow(){
        WebElement element = Driver.findElement(By.className("open"));
        element.click();
        Set<String> handles = Driver.getWindowHandles();
        String handle = Driver.getWindowHandle(); //得到默认最初窗体句柄
        
        handles.remove(handle);
        String newHandle = handles.iterator().next();
        print(newHandle);
        print(handle);
        Driver.switchTo().window(newHandle);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Driver.findElement(By.id("kw")).sendKeys("myString");
        //Driver.close();
        Driver.switchTo().window(handle);
        Driver.findElement(By.id("user")).sendKeys("new test");
        
    }
    //获取延时信息,常用于操作ajax请求返回的信息
    public void testWait(){
        WebElement element = Driver.findElement(By.className("wait"));
        element.click();
        //方法1 用Thread.sleep,设置了10秒,只能10秒后才能获取数据。
//        try {
//            Thread.sleep(6000);
//        } catch (InterruptedException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
        //Driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
        //方法2  用WebDriverWait,设置了10秒 如果数据5秒就返回的话,5秒就能获取到数据
        boolean wait = new WebDriverWait(Driver,10).until(new    ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver    d) {
                return    d.findElement(By.className("red")).    isDisplayed();}
        });
        
        print(String.valueOf(wait) );
        WebElement elementRed = Driver.findElement(By.className("red"));
        String str = elementRed.getText();
        print(str);
        
        
    }
    public void print(String str){
        System.out.println(str);
        
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        day5 t = new day5();
        t.startFirefox();
        t.goTo();
//        t.testMultiWindow();    
//        t.testWait();
        //t.testAction();
        //t.testUpload();
        t.testWait();
    }

}

 

posted @ 2015-07-10 15:16  米蓝  阅读(237)  评论(0编辑  收藏  举报