selenium2.0处理case实例(一)

通过自动化脚本, 判断下拉框选项值是否按照字母顺序(忽略大小写)显示

case场景如下:

1)打开www.test.com;
2)判断下拉框选项是否按照字母顺序排列(忽略大小写)
3)选择其中一个任意选项, 并判断已经选中
4)提交表单,验证弹出alert,并且验证提示内容为“successfully”

页面的html代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function submit_confirm(){
  alert("successfully");
}
</script>
</head>
<body>
<form name="selectOption" id="form1">
<select id="select1">
<option>American</option>
<option>china</option>
<option>Chinese</option>
<option>England</option>
<option>France</option>
</select>
<input type="submit" id="submit" value="submit" onclick="submit_confirm()">
</form>
</body>
</html>


完整的可运行的脚本如下:

 

package mavenSelenium;

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.openqa.selenium.Alert;
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;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestSelect extends Assert {
    WebDriver driver;
    WebDriverWait wait;
    
    @Before
    public void init(){
        driver=new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }
        
    @Test
    public void test(){
        //1、打开浏览器
        driver.get("http://localhost:8080/myWebSite/test.html");
        List<WebElement> elements=driver.findElements(By.xpath("//select[@id='select1']/option"));
        
        String[] s = new String[elements.size()];
        int i=0;
        for(WebElement element:elements){
            System.out.println(element.getText());
            s[i]=element.getText();
            i++;
        }
        //2、验证是否按照字母顺序排列, 不区分大小写
        for(int j=0;j<s.length-1;j++){
            String temp1=s[j].toLowerCase();
            String temp2=s[j+1].toLowerCase();
            System.out.println("temp1="+temp1);
            for(int k=0;k<temp1.length()&&k<temp2.length();k++){
                System.out.println("temp1.charAt(k)="+temp1.charAt(k));
                System.out.println("temp2.charAt(k)="+temp2.charAt(k));
                if(temp1.charAt(k)<temp2.charAt(k)){
                    break;
                }
                if(temp1.charAt(k)>temp2.charAt(k)){
                    fail("the option is not in order");
                }
            }
        }
        //3、选中选项,并验证已选中
        Select select=new Select(driver.findElement(By.id("select1")));//这里强转的话,会报类型转换错误,应以这种形式创建select
        String selectText="Chinese";
        select.selectByVisibleText(selectText);
        assertEquals(selectText, select.getFirstSelectedOption().getText());
        
        //4、提交表单,验证弹出alert,并且验证提示内容为“successfully”
        driver.findElement(By.id("submit")).click();
        Alert alert=driver.switchTo().alert();
        assertEquals("successfully", alert.getText());
        
    }
    @After
    public void tearDown(){
        driver.quit();
    }
}

 

 

你可能感兴趣的:(selenium)