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

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();
}
}

selenium2.0处理case实例(一)的更多相关文章

  1. selenium2.0处理case实例(二)

    本文通过具体代码处理过程, 来展示selenium中一些比较不常用的类的用法 1.javascriptExcutor,通过将driver强转成JavascriptExecutor类型, 调用execu ...

  2. Python版:Selenium2.0之WebDriver学习总结_实例1

    Python版:Selenium2.0之WebDriver学习总结_实例1  快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 实属转载:本人看的原文地址 :http:/ ...

  3. selenium2.0 处理各种窗口问题解决方法

    selenium2.0处理muti-Windows . Frames .Popup Dialogs selenium2.0处理多窗口,弹窗等,只需要调用WebDriver 嵌套类:TargetLoca ...

  4. selenium1.0和selenium2.0页面等待处理详解

    一.selenium1.0页面等待 1.……AndWait 经常会看到, selenium action命令中很多有这种……AndWait后缀, 例如click和clickAndWait命令: cli ...

  5. selenium win7+selenium2.0+python环境搭建

    win7+selenium2.0+python环境搭建 by:授客 QQ:1033553122 步骤1:下载python 担心最新版的支持不太好,这里我下载的是python 2.7(selenium之 ...

  6. Oracle 11.2.0.4单实例打PSU,OJVM PSU补丁快速参考

    写在前面: 1.Oracel打每个补丁的操作有时存在差异,所以不管多熟悉,都应该在打任何补丁之前阅读新补丁中附带的readme. 2.Oracle每季度都会更新一个最新的PSU,本文最新指的是当前最新 ...

  7. selenium2.0(WebDriver) API

    1.1  下载selenium2.0的包 官方download包地址:http://code.google.com/p/selenium/downloads/list 官方User Guide:  h ...

  8. 在selenium2.0中使用selenium1.0的API

    Selenium2.0中使用WeDriver API对页面进行操作,它最大的优点是不需要安装一个selenium server就可以运行,但是对页面进行操作不如selenium1.0的Selenium ...

  9. selenium2.0的初步封装(java版本)

    我们都知道, 在本地创建java项目后,引入selenium-java-2.35.0.jar   selenium-support-2.35.0.jar junit-4.8.1.jar等等jar包之后 ...

随机推荐

  1. Java笔记(十四)……抽象类与接口

    抽象类概念 抽象定义: 抽象就是从多个事物中将共性的,本质的内容抽取出来. 例如:狼和狗共性都是犬科,犬科就是抽象出来的概念. 抽象类: Java中可以定义没有方法体的方法,该方法的具体实现由子类完成 ...

  2. HW3.23

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  3. HDU-4415 Assassin’s Creed 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4415 用贪心来解,开始分为两个集合的方法错了,没有考虑之间的相互影响,正确的姿势应该是这样的,分两种情 ...

  4. 小波变换和motion信号处理(二)(转)

    写的太好,这是第二篇:http://www.kunli.info/2011/02/18/fourier-wavelet-motion-signal-2/ 这是<小波变换和motion信号处理&g ...

  5. 最简单的基于FFMPEG的音频编码器(PCM编码为AAC)

    http://blog.csdn.net/leixiaohua1020/article/details/25430449 本文介绍一个最简单的基于FFMPEG的音频编码器.该编码器实现了PCM音频采样 ...

  6. PC-如何提高计算机的启动和关机的速度?

    如何提高计算机的启动和关机的速度? 一.bios的优化设置 在bios设置的首页我们进入"advanced bios features"选项,将光标移到"frist bo ...

  7. A Tour of Go Pointers

    Go has pointers, but no pointer arithmetic. Struct fields can be accessed through a struct pointer. ...

  8. 新发现:原来java正则表达式不写^和$也可以运行

    最近用了好多正则表达式,都是循规蹈矩的在前面加上^在后面加上$ 像这个样子"^[.]\\S+$",但实际上我在eclipse和editplus下都试了一下,不加前缀和后缀也是可以的 ...

  9. 第三周作业、实时操作系统µC/OS介绍及其它内容

    作业要求 见<实时控制软件设计>第三周作业 1 阅读笔记--µC/OS 1.1 基本介绍 µC/OS是由Micrium公司研发的实时操作系统,以µC/OS-II或µC/OS-III为内核, ...

  10. iOS 火星坐标相关整理及解决方案汇总(转)

    这几天在处理定位相关的代码,彻彻底底的被火星坐标恶心到了. 恶心列表 从 CLLocationManager 取出来的经纬度放到 mapView 上显示,是错的! 从 CLLocationManage ...