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

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. Linux入门1

    在计算机科学中,Shell俗称壳(用来区别于核),是指“提供使用者使用界面”的软件(命令解析器).它类似于DOS下的command和后来的cmd.exe.它接收用户命令,然后调用相应的应用程序. Li ...

  2. 【HTML】Advanced5:Accessible Forms

    1.label <form> <label for="yourName">Your Name</label> <input name=&q ...

  3. 多线程归并排序的实现 java

    多线程是非常适合归并排序的,因为归并排序是分治法,所以分割后可以独立运行,最后将结果归并起来就行了.如何写一个多线程程序呢?今天无聊,总结一下啊. 首先写个普通的归并排序,以后的多线程就调用这个排序. ...

  4. vijosP1359 Superprime

    vijosP1359 Superprime 链接:https://vijos.org/p/1359 [思路] 搜索+数学. 很明显的搜索,依次确定每一个数,用参数sum记录dfs即可. 本题的关键在于 ...

  5. Fixing ssh login long delay

    原文:http://injustfiveminutes.com/2013/03/13/fixing-ssh-login-long-delay/ For a long time I had a prob ...

  6. HW3.18

    import javax.swing.JOptionPane; public class Solution { public static void main(String[] args) { Str ...

  7. Codeforces149E - Martian Strings(KMP)

    题目大意 给定一个字符串T,接下来有n个字符串,对于每个字符串S,判断是否存在T[a-b]+T[c-d]=S(1 ≤ a ≤ b < c ≤ d ≤ length(T)) 题解 对于每个字符串S ...

  8. zepto下动画返回顶部

     function scroll(scrollTo, time) {                var scrollFrom = parseInt(document.body.scrollTop) ...

  9. C++或者C#中如何拿到一个窗口的标题

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:C++或者C#中如何拿到一个窗口的标题.

  10. vc关于文件拷贝

    单个文件的拷贝 system  针对单个文件 CopyFile  针对单个文件 /** @file_extension egg: .txt .png **/ void CopyFileToDir(CS ...