Skip to content

Latest commit

 

History

History
122 lines (97 loc) · 3.08 KB

form.java.md

File metadata and controls

122 lines (97 loc) · 3.08 KB

form的操作

场景

表单对象的操作比较简单,只需要记住下面几点

  • 使用sendKeys方法往多行文本框和单行文本框赋值;
  • 使用click方法选择checkbox
  • 使用click方法选择radio
  • 使用click方法点击button
  • 使用click方法选择option,从而达到选中select下拉框中某个具体菜单项的效果

代码

form.html

form.rb

<html>
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
		<title>form</title>		
		<script type="text/javascript" async="" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
		<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" />		
		<script type="text/javascript">
			$(document).ready(function(){
					$('input[type=submit]').click(function(){
						alert('watir-webdriver is better than selenium webdriver');
					});
			});
		</script>
	</head>
		
	<body>
		<h3>form</h3>
		<div class="row-fluid">
			<div class="span6 well">		
				<form>
					<fieldset>
						<legend>Legend</legend>						
						<label class="checkbox">
							<input type="checkbox"> Check me out
						</label>
						
						<label class="radio">
							<input type="radio"> select me 
						</label>
						
						<label class="select">
							<select>
								<option>0</option>
								<option>1</option>
								<option>2</option>
							</select> select one item
						</label>
						
						<input type="submit" class="btn" value="submit" />
					</fieldset>
				</form>
			</div>
		</div>
	</body>
	<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</html>

###Form.java

	import java.io.File;
	import java.util.List;

	import org.openqa.selenium.Alert;
	import org.openqa.selenium.By;
	import org.openqa.selenium.Keys;
	import org.openqa.selenium.WebDriver;
	import org.openqa.selenium.WebElement;
	import org.openqa.selenium.chrome.ChromeDriver;


	public class Form {

		public static void main(String[] args) throws InterruptedException {
			WebDriver dr = new ChromeDriver();
			
			File file = new File("src/form.html");
			String filePath = "file:///" + file.getAbsolutePath();
			System.out.printf("now accesss %s \n", filePath);
			
			dr.get(filePath);
			Thread.sleep(1000);
			
	//		选中checkbox
			dr.findElement(By.cssSelector("input[type=checkbox]")).click();
			Thread.sleep(1000);
			
	//		选中radio
			dr.findElement(By.cssSelector("input[type=radio]")).click();
			Thread.sleep(1000);
			
	//		选择下拉菜单中的最后一项
			List<WebElement> options = dr.findElement(By.tagName("select")).findElements(By.tagName("option"));
			options.get(options.size() - 1).click();
			Thread.sleep(1000);
			
	//		点击提交按钮
			dr.findElement(By.cssSelector("input[type=submit]")).click();
			
			Alert alert = dr.switchTo().alert();
			System.out.println(alert.getText());
			alert.accept();
			
			Thread.sleep(1000);
			System.out.println("browser will be close");
			dr.quit();	
		}

	}