selenium+java+testng分层设计(三)

来源:互联网 发布:网络电视必须买盒子么 编辑:程序博客网 时间:2024/06/01 08:40

这篇主要学会将我们在第二篇中使用到的对元素的操作按照不同的页面放到对应的类中,从第一篇中可知,我们主要涉及到的是一个登录页面,和一个查询页面。则我们可以创建一个action文件夹,包含一个登录页面元素操作,一个查询页面元素操作.。

1.新建一个包com.test.action,在com.test.page中创建两个类,LoginAction、QueryAction.


2.LoginAction的代码如下:

package com.test.action;import org.openqa.selenium.WebDriver;import com.test.page.Loginpage;public class LoginAction {        private WebDriver driver;    //1)构造方法的方法名必须与类名相同。    //2)构造方法没有返回类型,也不能定义为void,在方法名前面不声明方法类型。    //3)构造方法的主要作用是完成对象的初始化工作,它能够把定义对象时的参数传给对象的域。    public LoginAction(WebDriver driver){        this.driver = driver;    }        public void Login(String user,String pw) {        Loginpage.userName(driver).sendKeys(user);        Loginpage.passWord(driver).sendKeys(pw);        Loginpage.loginButton(driver).click();    }        } 
3.QueryAction的代码如下:

package com.test.action;import org.openqa.selenium.WebDriver;import com.test.page.Querypage;public class QueryAction {  private WebDriver driver;        public QueryAction(WebDriver driver){  this.driver = driver;    }    public void AppQueryButton() throws InterruptedException{  Thread.sleep(2000);  Querypage.NextPage(driver).click();  Thread.sleep(2000);  Querypage.LastPage(driver).click();  Thread.sleep(2000);  Querypage.PageInput(driver).sendKeys("20");  Querypage.Go(driver).click();  Thread.sleep(2000);  Querypage.PreviousPage(driver).click();  Thread.sleep(3000);  Querypage.FirstPage(driver).click();  Thread.sleep(3000);  }}
4.test类代码更新为:

package testng;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.testng.annotations.Test;import org.testng.annotations.BeforeTest;import org.testng.annotations.AfterTest;import com.test.action.LoginAction;import com.test.action.QueryAction;public class test {LoginAction login = null;QueryAction query = null;WebDriver webdriver = new FirefoxDriver();//注意,一定要将该行放在此处,如果放在beforeTest方法中会导致其他方法执行时报空指针异常@BeforeTestpublic void beforeTest() {login = new LoginAction(webdriver);//初始化类webdriver.get("http://123.57.56.45:7778/test/initLogin");webdriver.manage().window().maximize();}@Test(priority=1)public void login()throws InterruptedException {//登录login.Login("999111", "111111");}@Test(priority=2)public void query_keys() throws InterruptedException{   //查询界面按键测试query = new QueryAction(webdriver);//初始化类query.AppQueryButton();} @AfterTest  public void afterTest() { webdriver.close();  }}

此时再看test代码是不是要简单多了。

3 0
原创粉丝点击