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

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. [codevs]失恋28天题目系列

    失恋28天-追女孩篇 题目描述 Description 呵呵,相信大家失恋33天都看过吧,里面的主人公黄小仙和王小贱都有印象吧!这回我要给大家讲的是我 们班同学的失恋经历,呵呵他总共失恋了28天.但是 ...

  2. Dynamic Vertex Buffers

    ynamic vertex buffers on the other hand allow us to manipulate the information inside the vertex buf ...

  3. WordPress模版结构

    一套完整的WordPress模版应至少包括如下文件: style.css : 样式表文件 index.php : 首页模板 archive.php : 文章归档/分类目录模板 404.php : 40 ...

  4. 关于Eclispse连接Mysql的Jdbc

    1.在Eclipse中新建Java工程 2.引入JDBC库(在bulid path 的extenrnal里) 3. 1)导入sql包(import java.sql.*) 2)加载(注册)mysql ...

  5. 【Java基础】基本类型的包装类作为参数传递是值传递还是引用传递

    突然想到这个问题,然后做了下实验,下面以Integer来讲解,其他的忽略: import java.util.Iterator; /** * Created by lili on 15/9/24. * ...

  6. 【解决】exited with a non-zero exit code 1

      安装好集群后运行hadoop-mapreduce-examples.jar 报错,比对cdh配置后,发现少了yarn.application.classpath,conf目录一定要配置,否则也会出 ...

  7. 最新Blog

    应该会长期稳定的 https://b.intmainreturn0.com/

  8. java枚举enum

    http://www.cnblogs.com/wenruo/p/5349614.html java的枚举通过关键字enum实现.可以理解为一个类,不过这个类由编译器自动加了一些方法. static v ...

  9. java 日志技术汇总(log4j , Commons-logging,.....)

    前言 在Tomcat 与weblogic 中的 日志(log4j) 配置系列一 在系列一 中, 有一个问题一直没有解决,就是部署到weblogic 中应用程序如何通过log4j写日志到文件中? 这里仅 ...

  10. PHP 判断用户是否手机访问

    $agent = check_wap(); if( $agent ) { header('Location: http://www.lewanau.com'); exit; } // check if ...