Webdriver UI自动化测试定义工具类方便用例获取资源


1.测试用例中需要上传图片的时候,图片作为测试数据,维护在测试数据文件中:

Webdriver UI自动化测试定义工具类方便用例获取资源_第1张图片


2.为了方便用例里面随用随拿 定义如下工具类 :

package ec.qa.autotest.ui.utility;

/**
 * @author xin.wang
 * 获取测试图片地址
 */

public class PictureUtil {
	
	private static String baseURL;

	public static  void setBaseUrl(String baseURL){
	       PictureUtil.baseURL = baseURL;
	}
	
	public static String getPicUrl(String picName){
		return baseURL+picName;
	}
}


3在测试用例集初始化的时候,初始化图片的基础URL(testng):PropertuesUtil从配置文件读取基础路径

#测试图片文件夹地址
testcase.testpic.path=\\test-data\\pic\\

@BeforeSuite(alwaysRun = true)
	public void initTest(XmlTest xt,ITestContext tc) throws Exception {
		PictureUtil.setBaseUrl(System.getProperty("user.dir") +PropertiesUtil.getProValue("testcase.testpic.path"));
	}

4.在用例方法中,根据图片名字,随拿随用:

如上传图片文件:

@FindBy(how = How.XPATH, using = "//div[@id='fileArea']/input")
private WebElement inDetailImgFile;

inDetailImgFile.sendKeys(PictureUtil.getPicUrl("123.jpg"));

还有properties文件的util类见博客:http://blog.csdn.net/wangxin1982314/article/details/50222457

5.Cookie的工具类:

package ec.qa.autotest.ui.utility;

import org.openqa.selenium.Cookie;

/**
 * @author xin.wang
 * 保存登录后返回的COOKIE
 */

public class CookiesUtil {
	
	private static Cookie ck = null;

	public static Cookie getCk() {
		return ck;
	}

	public static void setCk(Cookie ck) {
		CookiesUtil.ck = ck;
	}
}

可以在登录用例中为其初始化,后继用例如果想绕过登录,可通过携带cookie的方式直接进入登录成功页面

public class LoginPageCase extends SupplierPortalTestBase {

	@AutoInject
	private LoginSuccessPage loginSuccessPage;

	@AutoInject
	private LoginPage loginPage;

	@AutoInject
	private IndexPage indexPage;
	LoginInfo info;
	
	@DataObject(DataNode = "LoginData")
	private HashMap logindata;

	/**
	 * @author zhihui.kuang
	 * @see 供应商门户系统,登录页面登录功能
	 */
	@Test(groups = { "login" }, invocationCount = 1)
	public void loginSupplierPortal() throws InterruptedException {
		indexPage.clickLoginLink();
		loginPage.loginPortal(logindata);
		Assert.assertEquals(loginSuccessPage.getEmailInfo(), info.getEmail());
		CookiesUtil.setCk(webDriver.manage().getCookieNamed("_s_u_i_d"));
	}
}

6.封装模拟鼠标,键盘的基本操作:如果现有的方法不满足使用 还可以拿到Actions的原生对象(getActionObj方法)使用它原生的方法进行模拟(类似于QTP的.object拿到原生对象).

package ec.qa.autotest.ui.utility;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

/**
 * 
 * @author xin.wang
 * 模拟 鼠标,键盘的操作
 */

public class ActionUtil {
	
	private static Actions act;
	
	public static void setAction(WebDriver driver){
		act = new Actions(driver);
	}
	
	public static Actions getActionObj(){
		return act;
	}
	
	public static void SEND_TAB_KEY(){
		act.sendKeys(Keys.TAB).perform();
	}
	
	public static void SEND_BACKSPACE_KEY(){
		act.sendKeys(Keys.BACK_SPACE).perform();
	}
	
	public static void SEND_SPACE_KEY(){
		act.sendKeys(Keys.SPACE).perform();
	}
	
	public static void MOUSE_LEFT_CLICK(){
		act.click().perform();
	}
	
	public static void MOUSE_LEFT_CLICK(WebElement e){
		act.click(e).perform();
	}
	
	public static void MOUSE_DOUBLE_CLICK(WebElement e){
		act.doubleClick(e).perform();
	}
	
	public static void MOUSE_DOUBLE_CLICK(){
		act.doubleClick().perform();
	}
}


7 获取当前webdriver实例:

package ec.qa.autotest.ui.utility;

import org.openqa.selenium.remote.RemoteWebDriver;

import ec.qa.autotest.ui.testbase.impl.TestBase;

public class WebDriverUtil {
	
	public static RemoteWebDriver getWebDriverInstance(){
		return TestBase.getWebDriver();
	}
}

如上

Webdriver UI自动化测试定义工具类方便用例获取资源_第2张图片



你可能感兴趣的:(web,UI自动化测试,软件测试技术,编程技术,自动化测试,质量把控)