Study:基于Selenium进行firefox自动化测试的例子。

Maven配置:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.hisky</groupId>
  <artifactId>seleniumTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>seleniumTest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.seleniumhq.selenium</groupId>
    	<artifactId>selenium-java</artifactId>
    	<version>2.35.0</version>
    </dependency>
  </dependencies>
</project>
 代码事例:
package com.hisky.seleniumTest;

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.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * 
 * @author zhenglin.yang
 * 
 */
public class WebTest {
	public static void main(String[] args) {
		// testSearch();
		testLogin();
	}

	private static void testSearch() {
		// System.setProperty ( "webdriver.firefox.driver" ,
		// "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" );
		WebDriver driver = new FirefoxDriver();

		driver.get("http://www.baidu.com");
		WebElement element = driver.findElement(By.name("wd"));
		element.sendKeys("中国");
		element.submit();

		(new WebDriverWait(driver, 20)).until(new ExpectedCondition<Boolean>() {
			public Boolean apply(WebDriver d) {
				return d.getTitle().toLowerCase().startsWith("中国");
			}
		});

		System.out.println("Page title is: " + driver.getTitle());

		// Close the browser
		driver.quit();
	}

	private static void testLogin() {
		// System.setProperty ( "webdriver.firefox.driver" ,
		// "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" );
		WebDriver driver = new FirefoxDriver();
		System.out.println("starts browser...");

		driver.get("http://yun.baidu.com");
		System.out.println("access login page...");

		WebElement element = driver.findElement(By.name("userName"));
		element.sendKeys("your user name...");
		System.out.println("input user name...");

		element = driver.findElement(By.name("password"));
		element.sendKeys("your password...");
		System.out.println("input password");
		element.submit();

		System.out.println("Page title is: " + driver.getTitle());

		// Close the browser
		driver.quit();
	}
}

 以上内容参考selenium官网教程。firefox的版本不能高于20,否则无法启动。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值