初学Selenium WebDriver,写个自动化脚本练习。
项目描述:登录QQ邮箱,进入写信页面,输入收件人,主题和内容,点击发送。本项目主要是针对收件人一栏进行自动化测试,比如收件人为空时,收件人格式错误等情况。本项目只涉及登录,写信操作,所以将登录页面,登录后的主页面,以及写信页面作为对象进行封装,便于操作和简化代码。
代码说明:
一. 登录页面只登录操作
二. 主页面有点击写信操作和退出登录操作
三. 写信页面有输入收件人,主题,内容和点击发送等操作。
接下来分别对这三个页面进行编写了。
登录页面代码如下,因登录框是在一个frame中,所以要加上一句:driver.switchTo().frame("login_frame"); 否则找不到对应的登录框元素。import java.util.concurrent.TimeUnit;
import org.junit.Test;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginPage {
WebDriver driver;
WebElement loginByUserLink;
WebElement username;
WebElement password;
WebElement loginbutton;
public LoginPage(WebDriver driver)
{
this.driver = driver;
driver.switchTo().frame("login_frame");
loginByUserLink = driver.findElement(By.linkText("帐号密码登录"));
loginByUserLink.click();
username = driver.findElement(By.id("u"));
password = driver.findElement(By.id("p"));
loginbutton = driver.findElement(By.id("login_button"));
}
public void login(String userName,String passWord)
{
username.clear();
password.clear();
username.sendKeys(userName);
password.sendKeys(passWord);
loginbutton.click();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
}
登录后,进入主页面。主页面只有写信和退出两个操作,比较简单,代码如下:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class MainPage {
WebDriver driver;
WebElement receivedBox;
WebElement writeLink;
WebElement logoutLink;
public MainPage(WebDriver driver)
{
this.driver = driver;
writeLink = driver.findElement(By.linkText("写信"));
logoutLink = driver.findElement(By.linkText("退出"));
}
public void toWrite()
{
writeLink.click();
}
public void logout()
{
logoutLink.click();
}
}
写信页面代码如下,主要说明有两点:
1. 整个写信页面是在一个名为mainFrame的frame中,所以填写收信人,主题时要先driver.switchTo().frame("mainFrame");而内容是在mainFrame中的一个iframe标签中,且该iframe没有id,但mainFrame中就只有一个iframe,可以用driver.switchTo().frame(driver.findElement(By.tagName("iframe")));进入该iframe编写正文内容。
2. 因收件人可以为多个,所以使用ArrayList来接收收件人信息。
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class WriteLetter {
WebDriver driver;
WebElement toUser;
WebElement title;
WebElement content;
WebElement sendButton;
public WriteLetter(WebDriver driver)
{
this.driver = driver;
driver.switchTo().frame("mainFrame"); //整个写信页面
sendButton = driver.findElement(By.linkText("发送"));
toUser = driver.findElement(By.xpath(".//*[@id='toAreaCtrl']/div[2]/input"));
title = driver.findElement(By.id("subject"));
}
public void writeLetter(ArrayList<String> ToUser,String Title,String Content)
{
for(String user : ToUser)
{
toUser.sendKeys(user);
}
title.sendKeys(Title);
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
content = driver.findElement(By.tagName("body")); //直接发送数据给iframe中的body
content.sendKeys(Content);
driver.switchTo().parentFrame();
sendButton.click();
}
}
页面对象全部写好了,接下来就是用几个测试用例验证了。欲知后事如何,且看下回分解。