selenium2入门

http://code.google.com/p/selenium/是开源的自动化测试框架,支撑主流的所有的游览器,支持多语言开发,

要使用selenium2的pom依赖如下

<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>2.7.0</version>
        </dependency>

新建一个baidu的首页的页面对象,里面有各个WebElement元素。

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class BaiduSearchPage {
    // Here's the element
    private WebElement kw;//关键字的输入
    
    private WebElement su;//提交表单

    @FindBy(how = How.NAME, using = "tj_login")
    private WebElement  tj_login; //百度用户登录用户名显示
    
    private WebElement s_username_top;
    
    //点击登录操作
    public void linkLogin(){
        tj_login.click();
    }
    //获取登录后用户名
    public String getUsername(){
		return s_username_top.getText();
    	
    }
    //搜索关键字
    public void searchFor(String text) {
        // And here we use it. Note that it looks like we've
        // not properly instantiated it yet....
        kw.sendKeys(text);
        su.submit();
    }
}

登录页面

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class BaiduLoginPage {

	@FindBy(how = How.NAME, using = "username")
	private WebElement username_normal;
	private WebElement normModPsp;
	@FindBy(how = How.XPATH, using = "/html/body/center/div/div[2]/div/table/form")
	private WebElement login;

	public void login(String username, String passwd) {
		username_normal.sendKeys(username);
		normModPsp.sendKeys(passwd);
		login.submit();
	}
}

测试代码如下

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;

public class AppTest {

    @Test
    public void testindex() {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.baidu.com/");
        BaiduSearchPage page = PageFactory.initElements(driver, BaiduSearchPage.class);
        page.searchFor("koubei");
        Assert.assertEquals("百度搜索_koubei", driver.getTitle());
        driver.close();
    }

    @Test
    public void testlogin() {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.baidu.com/");
        BaiduSearchPage baiduSearchPage =PageFactory.initElements(driver, BaiduSearchPage.class);
        baiduSearchPage.linkLogin();
        BaiduLoginPage page = PageFactory.initElements(driver, BaiduLoginPage.class);
        page.login("username", "passwd");
        Assert.assertEquals("username", baiduSearchPage.getUsername());
        Assert.assertEquals("百度一下,你就知道", driver.getTitle());
        driver.close();
    }

这里使用chrome主要是因为启动比firefox快一点需要在http://code.google.com/p/chromium/downloads/list下一个chromedriver并且要把path中

相关推荐