10.2.2、编写邮箱登录测试脚本
下面我们就通过前面学习的 Page Object 设计模式,利用 testNG 框架组织和运行测试用例。在此之前简单的规划一下测试项目结构:
我们简单的将项目规划为三层架构,page 层用于封装页面元素的定位。cases 层调用 publics 层提供的动作实现具体的测试用例。
代码1【LoginPage】:
package mail.test.page;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LoginPage {
public static class getElement{
public static final String title = "126 网易免费邮--你的专业电子邮局";
// 通过 XPATH 获取页面元素
public static final String iframe_xpath = "//*[@id='loginDiv']/iframe";
public static final String username_name = "email";
public static final String password_name = "password";
public static final String login_button_id = "dologin";
//登陆成功的用户名
public static final String login_success_text_id = "spnUid";
}
private WebDriver driver; //实例化对象
private String Url; //登录页面的Url
//声明对象时,自动加载页面
public LoginPage(WebDriver driver , String Url){
this.driver = driver;
this.Url = Url;
this.driver.get(this.Url);//加载页面
}
//获取页面标题
public String getTitle() {
return this.driver.getTitle();
}
// 检测页面是否加载,判断 title 是否相等,返回 True/False
public boolean isLoaded() {
System.out.println(this.getTitle());
return LoginPage.getElement.title.equals(this.getTitle());
}
// 表单的进入
public void login_iframe() {
WebElement xf = this.driver.findElement(By.xpath(LoginPage.getElement.iframe_xpath));
this.driver.switchTo().frame(xf);
}
// 表单的退出
public void login_iframe_out() {
this.driver.switchTo().defaultContent();
}
// 登录用户名输入框
public void login_username(String username) {
WebElement element = this.driver.findElement(By.name(LoginPage.getElement.username_name));
element.clear();
element.sendKeys(username);
}
// 登录密码输入框
public void login_password(String password) {
WebElement element = this.driver.findElement(By.name(LoginPage.getElement.password_name));
element.clear();
element.sendKeys(password);
}
// 登录按钮
public void login_button() {
this.driver.findElement(By.id(LoginPage.getElement.login_button_id)).click();
}
//登陆方法,传入用户名和密码,并点击登陆按钮,然后根据 title 判断跳转是否成功
public void login(String username, String password) {
login_iframe();
login_username(username);
login_password(password);
login_button();
login_iframe_out();
}
// 返回登录成功之后的用户名
public String user() {
String text = this.driver.findElement(
By.id(LoginPage.getElement.login_success_text_id)).getText();
return text;
}
}
-------------------------------------------------------------------------------------------------------------------------------
代码2【LoginTest】:
package mail.test.cases;
import static org.testng.Assert.assertEquals;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import mail.test.page.LoginPage; //导入LoginPage文件
public class LoginTest {
private WebDriver driver;
private String baseUrl;
private LoginPage page;
@BeforeTest
public void setUp() throws Exception {
this.driver = new FirefoxDriver();
this.baseUrl = "http://www.126.com";
this.page = new LoginPage(this.driver, baseUrl);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void testCase() throws Exception {
this.page.login("testing", "123456");
Thread.sleep(5000);
System.out.println(this.page.user());
String username = this.page.user();
assertEquals(username, "testing@126.com");
}
}
-------------------------------------------------------------------------------------------------------------------------------
代码3【testNG】:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="suite1">
<test name="test1">
<classes>
<class name="mail.test.cases.LoginTest"/>
<class name="mail.test.cases.LoginTestData"/>
</classes>
</test>
</suite>