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

这篇主要学会将我们在第二篇中使用到的对元素的操作按照不同的页面放到对应的类中,从第一篇中可知,我们主要涉及到的是一个登录页面,和一个查询页面。则我们可以创建一个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方法中会导致其他方法执行时报空指针异常

	@BeforeTest
	public 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代码是不是要简单多了。

 

 

 

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值