前面一篇,我介绍了BasePage.java的内容,解释了写这个类的好处。主要有二次封装selenium一些常见方法,还有就是创建所有页面类的父类。这篇,我们开始实现POM并测试我们的BasePage类
IDEA的目录如下:
1、LoginPage.java类的内容如下 :
构架第二层:我们现在在java下面新建一个pageobject包,里面放页面封装好的类,LoginPage.java就在其中,所有页面类,主要两部分,第一个部分是当前页面元素的定位,第二部分就是,一些业务逻辑方法的重构,方便测试脚本中调用。
package pageobject;
import framework.BasePage;
import framework.Excel;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.WebElement;
public class LoginPage extends BasePage {
@FindBy (xpath="/html/body/div[5]/div[2]/div/div/div[2]/form/div[1]/div/div[1]/input")
WebElement username_inputBox;
@FindBy (xpath="/html/body/div[5]/div[2]/div/div/div[2]/form/div[2]/div/div[1]/input")
WebElement password_inputBox;
@FindBy (xpath="/html/body/div[5]/div[2]/div/div/div[2]/form/div[3]/div/div[1]/input")
WebElement verifypic_inputBox;
@FindBy (xpath="/html/body/div[5]/div[2]/div/div/div[2]/form/div[4]/div/div/div[1]/div/input")
WebElement serveraddress_inputBox;
@FindBy (xpath="/html/body/div[5]/div[2]/div/div/div[2]/form/div[4]/div/div/div[2]/ul[2]/li[2]")
WebElement serveraddress_selector;
@FindBy (xpath="/html/body/div[5]/div[2]/div/div/div[3]/div/button")
WebElement Login_Button;
public LoginPage(WebDriver driver){
super(driver);
}
public void Login() throws Exception{
Excel excel=new Excel();
excel.setExcelFile("E:\\Java_project\\XTPTest_selenium\\suites\\test-data.xlsx","Login_info");
//Excel中的行和列都是从0开始的不是从1
type(username_inputBox,excel.getCellData(1,0));
type(password_inputBox,excel.getCellData(1,1));
type(verifypic_inputBox,excel.getCellData(1,2));
click(serveraddress_inputBox);
callWait(2);
click(serveraddress_selector);
callWait(2);
click(Login_Button);
}
}
2、测试脚本类的内容
框架第三层:们现在在java下面新建一个testsuit包,里面放最终的测试脚本,如登陆的测试脚本如下:
package testsuite;
import framework.BrowserEngine;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import pageobject.LoginPage;
import java.io.IOException;
public class Stock_order {
WebDriver driver;
@BeforeClass
public void setUp() throws IOException {
BrowserEngine browserEngine=new BrowserEngine();
browserEngine.initConfigData();
driver=browserEngine.getBrowser();
}
@Test
public void LoginSystem()throws Exception{
LoginPage loginpage= PageFactory.initElements(driver,LoginPage.class);
loginpage.Login();
}
}
运行脚本成功。