Web UI 优化自动化测试用例的稳定性


     Web UI自动化测试的一个很重要的问题就是整个测试的稳定性,经常在运行测试的时候出现这样或那样的问题,而且大多都是稳定性问题,而非BUG,最近我针对同事的用例的稳定性问题做了些优化策略,今天跑了 500个用例 没有一个出现稳定性问题,当然 可能有运气成分吧 呵呵 总共运行时间 5小时,实际的测试用例有70-80个左右 是循环跑出了500次 特此声明一下 不然有人会质疑,UI自动化 以回归 和冒烟为主  我这不会出现那么多用例的,投入产出 会随着用例数增长而下降的,我们的自动化其实还是接口为重点。

killed the useless process
Tests run: 501, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 18,205.275 sec - in TestSuite

Results :

Tests run: 501, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:03 h
[INFO] Finished at: 2015-12-08T18:41:54+08:00
[INFO] Final Memory: 19M/222M
[INFO] ------------------------------------------------------------------------
Recording test results
Sending e-mails to: [email protected]
Finished: SUCCESS

添加页面跳转判断页面加载过程的代码:

package ec.qa.autotest.ui.common.action;


import java.util.concurrent.TimeUnit;


import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.SystemClock;
import org.testng.Assert;


import ec.qa.autotest.ui.custom.annotation.PageObject;
import ec.qa.autotest.ui.testbase.TestBase;


@PageObject
public class SupplierProgressCommonAction {


	@FindBy(how = How.ID, using = "ngProgress")
	private WebElement spProgress;
	
	private final long TIMEOUTS = 5000;
	
	private final long WAIT_TIME = 500;


	public SupplierProgressCommonAction() {
		PageFactory.initElements(TestBase.getWebDriver(), this);
	}


	public void waitForProgressTOComplete(long timeOut) throws InterruptedException {
		Assert.assertEquals(isProgressCompleted(timeOut), true);
	}


	public void waitForProgressTOComplete() throws InterruptedException {
		Assert.assertEquals(isProgressCompleted(TIMEOUTS), true);
	}


	private boolean isProgressCompleted(long timeOut) throws InterruptedException {
		SystemClock sc =new SystemClock();
		long endTime = sc.laterBy(timeOut);
		
		while (sc.isNowBefore(endTime)) {
			TimeUnit.MILLISECONDS.sleep(WAIT_TIME);
			if (spProgress.getAttribute("style").contains("width: 0%")) {
				WaitForUtil.pageIsPasered();
				return true;
			}
		}
		return false;
	}
}


一些通用的保证测试稳定的代码:

package ec.qa.autotest.ui.common.action;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.SystemClock;
import org.openqa.selenium.support.ui.WebDriverWait;

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

/**
 * @author xin.wang 等待页面解析完成
 */
public class WaitForUtil {

	private static final long TIMEOUTS = 4000;

	private static final long WAIT_TIME = 300;

	public static void pageIsPasered() {
		new WebDriverWait(TestBase.getWebDriver(), 10).until(new ExpectedCondition() {
			public Boolean apply(WebDriver driver) {
				return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
			}
		});
	}

	public static String getPageFieldText(WebElement el) throws InterruptedException {
		long startTime = System.currentTimeMillis();
		long curTime = startTime;
		while ((curTime - startTime) <= TIMEOUTS) {
			TimeUnit.MILLISECONDS.sleep(WAIT_TIME);
			if (el.getText().trim() != "" && el.getText() != null) {
				return el.getText();
			}
			curTime = System.currentTimeMillis();
		}
		return "";
	}

	/**
	 * @author xin.wang 
	 *等待元素是否可见
	 */
	public static WebElement waitForElementToDisplay(WebElement el) throws Exception {
		SystemClock sc = new SystemClock();
		long endTime = sc.laterBy(TIMEOUTS);
		boolean flag = false;

		while (sc.isNowBefore(endTime)) {
			TimeUnit.MILLISECONDS.sleep(WAIT_TIME);
			if (el.isDisplayed()) {
				flag = true;
				break;
			}
		}
		if (!flag) {
			throw new Exception("the element is not visable");
		}
		return el;
	}
}










你可能感兴趣的:(web,UI自动化测试)