Junit+Selenium+Maven+SVN+Eclipse+AutoFrame全自动化测试实践实例(二)

相关博文:

集群式自动化测试框架(平台)设计与实现

应用于全自动化测试体系的应用实现实例(基于SVN跨平台敏捷项目)

Java和.Net版通用工具类实现--生成自定义Web Html/Excel测试用例和测试报告

(一)conf目录及配置文件

####################################################################################################
# Switch controller: 
# <browser.type>
# :: 0 is chrome
# :: 1 is firefox
# :: 2 is IE
browser.type=0

###############################
# chrome driver path: 
# <chromeDriverPath> path of chrome driver
#
browser.chromeDriverPath=conf/chromedriver.exe

###############################
# IE driver path: 
# <IEDriverPath> path of IE driver
# IE 9 suitable
#
#browser.IEDriverPath=conf\\IEDriverServer.exe
browser.IEDriverPath=conf/IEDriverServer.exe

###############################
# Business Server config:
# <cwmServer.BaseUrl> is the tested web address 
cwmServer.BaseUrl=https://10.34.64.213:8282

##############################
# User config: used to login the business server
# <userName>: account of user id
# <userPwd> : password of account
#
userName=admin
userPwd=123456
Language=English
#Language=한국어
#############################
# Test cases result path:
# <result.picWidth>: The width of picture which will be screenshot when error occur. 
# <result.picHeight>: The height of picture which will be screenshot when error occur. 
result.picWidth=1024
result.picHeight=768

#############################
#HtmlFormat: project name and work path
HtmlDoc.ProjectName=CWM
HtmlDoc.HomePath=C:\\ECSTOOL
HtmlDoc.DirPath=C:\\ECSTOOL\\nginx\\html\\CWM
#HtmlFormat: html model sample
HtmlDoc.IndexModel=conf/ListSample.htm
HtmlDoc.DetailModel=conf/DetailSample.htm
HtmlDoc.IndexList=<tr><td>$CaseID</td><td>$TaskName</td><td>$TestTime</td><td>$TestSummary</td><td style=\\"font-weight:bold;\\"><a href=\\"$href\\"><font color=\\"$color\\">$TestResult</font></a></td><td>$Comments</td></tr>
#HtmlFormat: web report address
HtmlDoc.HttpPath=http://10.34.130.62/CWM
#HtmlFormat: test code address
HtmlDoc.ScriptPath=http://svn.platform.nhncorp.cn/qa_repository/common/document/Technology document/IronPythonTool/AutoTestCWM/cwm-auto-test


EtcIO类:

package com.nhn.platform.qa.cwmtest.Utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Properties;

public class EtcIO {

	static String confPath = "conf/conf.properties";
	static int browserType = Integer.parseInt(readValue("browser.type")); 
	// 0 -
	// chrome,
	// others
	// -
	// firefox

	// chrome driver path
	public static String chromeDriverPath = readValue("browser.chromeDriverPath");
	// ie driver path
	public static String IEDriverPath = readValue("browser.IEDriverPath");

	// Business Server IP Address and port number
	public static String baseUrl = readValue("cwmServer.BaseUrl");

	// admin user and password
	public static String userName = readValue("userName");
	public static String userPwd = readValue("userPwd");
	public static String language = readValue("Language");

	// log jQuery dir
	public static String jQueryPath = readValue("jQuery.path");
	// Log Small Pic size
	public static int logPicWidth = Integer.parseInt(readValue("result.picWidth"));
	public static int logPicHeight = Integer.parseInt(readValue("result.picHeight"));
	
	// XML file tag name
	public static String RunTag = "Run";
	public static String PassTag = "Pass";
	public static String FailTag = "Fail";
	public static String ErrorTag = "Error";

	public static String readValue(String key) {
		Properties props = new Properties();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(
					confPath));
			props.load(in);
			String value = props.getProperty(key);
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void ReplaceContent(String sourcePath, String targetPath,
			String[] search, String[] replace) {
		try {
			FileReader reader = new FileReader(sourcePath);
			char[] dates = new char[1024];
			int count = 0;
			StringBuilder sb = new StringBuilder();
			while ((count = reader.read(dates)) > 0) {
				String str = String.valueOf(dates, 0, count);
				sb.append(str);
			}
			reader.close();
			// 从构造器中生成字符串,并替换搜索文本
			String str = sb.toString();
			for (int i = 0; i < search.length; i++) {
				str = str.replace(search[i], replace[i]);
			}
			FileWriter writer = new FileWriter(targetPath);
			writer.write(str.toCharArray());
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 追加文件:使用RandomAccessFile
	 * 
	 * @param fileName
	 *            文件名
	 * @param content
	 *            追加的内容
	 */
	public static void AppendContent(String fileName, String content) {
		RandomAccessFile randomFile = null;
		try {
			// 打开一个随机访问文件流,按读写方式
			randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度,字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (randomFile != null) {
				try {
					randomFile.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

 

(二)Selenium Web Driver驱动


DriverUtil类:

package com.nhn.platform.qa.cwmtest.Utils;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

/********************
 * @description
 * @version 1.0
 * @author Frank Wu
 * @update 2013-1-25 上午10:21:08
 ********************/

public class DriverUtil {

	private static ChromeDriverService chservice;
	private static InternetExplorerDriverService ieservice;
	private static Dimension screenDims = Toolkit.getDefaultToolkit()
			.getScreenSize();

	public static HtmlDoc html = new HtmlDoc();
	public static WebDriver driver;

	// public static void StartService(int browserType) {
	// switch(browserType){
	// case BrowserType.ChromeType:
	// case BrowserType.ChromeFirefox:
	// StartChromeService();
	// break;
	// case BrowserType.IEType:
	// case BrowserType.IEFirefox:
	// StartIEService();
	// break;
	// case BrowserType.ChromeIE:
	// case BrowserType.ChromeIEFirefox:
	// StartChromeService();
	// StartIEService();
	// break;
	// default:
	// break;
	// }
	// }
	private static void StartChromeService() {
		if (EtcIO.chromeDriverPath == null || EtcIO.chromeDriverPath.equals("")) {
			EtcIO.chromeDriverPath = "conf/chromedriver";
		}
		chservice = new ChromeDriverService.Builder()
				.usingChromeDriverExecutable(new File(EtcIO.chromeDriverPath))
				.usingAnyFreePort().build();
		try {
			System.setProperty("webdriver.chrome.driver",
					EtcIO.chromeDriverPath);
			chservice.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void StartIEService() {
		if (EtcIO.IEDriverPath == null || EtcIO.IEDriverPath.equals("")) {
			EtcIO.IEDriverPath = "conf/IEDriverServer.exe";
		}
		ieservice = new InternetExplorerDriverService.Builder()
				.usingDriverExecutable(new File(EtcIO.IEDriverPath))
				.usingAnyFreePort().build();
		try {
			System.setProperty("webdriver.ie.driver", EtcIO.IEDriverPath);
			ieservice.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void StopService() {
		if (chservice != null && chservice.isRunning()) {
			chservice.stop();
		}
		if (ieservice != null && ieservice.isRunning()) {
			ieservice.stop();
		}
	}

	public static void ChromeStart() {
		QuitDriver();
		if (chservice == null) {
			StartChromeService();
		}
		driver = new RemoteWebDriver(chservice.getUrl(),
				DesiredCapabilities.chrome());
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		MaxiMise(driver);
	}

	public static void IEStart() {
		QuitDriver();
		if (ieservice == null) {
			StartIEService();
		}
		DesiredCapabilities ieCapabilities = DesiredCapabilities
				.internetExplorer();
		ieCapabilities
				.setCapability(
						InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
						true);
		driver = new RemoteWebDriver(ieservice.getUrl(), ieCapabilities);
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		MaxiMise(driver);
	}

	public static void FirefoxStart() {
		QuitDriver();
		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		MaxiMise(driver);
	}

	public static void QuitDriver() {
		if (driver != null) {
			driver.quit();
		}
	}

	// 使窗口最大化函数
	public static void MaxiMise(WebDriver driver) {
		final JavascriptExecutor js = (JavascriptExecutor) driver;
		js.executeScript("window.open('','testwindow','width=400,height=200')");
		driver.close();
		driver.switchTo().window("testwindow");
		js.executeScript("window.moveTo(0,0);");
		int width = (int) screenDims.getWidth();
		int height = (int) screenDims.getHeight();
		/*
		 * 1280和1024分别为窗口的宽和高,可以用下面的代码得到 screenDims =
		 * Toolkit.getDefaultToolkit().getScreenSize(); width = (int)
		 * screenDims.getWidth(); height = (int) screenDims.getHeight();
		 */
		js.executeScript("window.resizeTo(" + width + "," + height + ");");
	}
}


 (三)测试类示例

HostTest类:

package com.nhn.platform.qa.cwmtest.firefox;

import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.nhn.platform.qa.cwmtest.Utils.*;
import com.nhn.platform.qa.cwmtest.cwmautotest.ICwmXpath;
import com.nhn.platform.qa.cwmtest.cwmautotest.Start2End;

/********************
 * @description
 * @version 1.0
 * @author Frank Wu
 * @update 2013-1-28 上午11:34:07
 ********************/

public class HostTest extends TestCase {
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	public HostTest(String name) {
		super(name);
	}
	@Before
	public void setUp() throws Exception {
		DriverUtil.FirefoxStart();
		baseUrl = EtcIO.baseUrl;
	}
	@After
	public void tearDown() throws Exception {
		DriverUtil.driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			// fail(verificationErrorString);
		}
	}
	public static Test suite() {
		TestSuite suite = new TestSuite("Test for HostTest");
		// $JUnit-BEGIN$
		suite.addTest(new HostTest("LoginTest001_010"));
		suite.addTest(new HostTest("LogoutTest001_002"));
		suite.addTest(new HostTest("ChangePwdTest001_028"));
		// $JUnit-END$
		return suite;
	}

	public void Login(String user, String password, String expect) {
		try {
			DriverUtil.driver.get(baseUrl);
			Start2End.Sleep(3000);
			String expectPath = "//span[@role='presentation']";
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser)).clear();
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser))
					.sendKeys(user);
			Start2End.Sleep(1000);
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))
					.clear();
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))
					.sendKeys(password);
			DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogin))
					.click();
			Start2End.Sleep(1000);
			String temp = DriverUtil.driver.findElement(By.xpath(expectPath))
					.getText();
			// DriverUtil.html.InsertHtml(TaskName, TestSummary, TestResult,
			// Comments, Precondition, Steps, Expects, Results, Remarks)
			boolean flag = temp.trim().contains(expect);
			DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsPopOk))
					.click();
			if (flag) {
				DriverUtil.html.InsertHtml("Login",
						"In order to check the Login-function is OK!",
						EtcIO.PassTag, "none", "none",
						"Input incorrect username:" + user + " and password:"
								+ password + ", and then click login-button.",
						expect, temp, "none");
			} else {
				DriverUtil.html.InsertHtml("Login",
						"In order to check the Login-function is OK!",
						EtcIO.FailTag, "none", "none",
						"Input incorrect username:" + user + " and password:"
								+ password + ", and then click login-button.",
						expect, temp, "none");
				DriverUtil.html.ScreenCapture(DriverUtil.driver);
			}
			// Assert.assertTrue(flag);
		} catch (Exception e) {
			DriverUtil.html.InsertHtml("Login",
					"In order to check the Login-function is OK!",
					EtcIO.ErrorTag, "exception error", "none",
					"Input incorrect username:" + user + " and password:"
							+ password + ", and then click login-button.",
					expect, e.getMessage(), "none");
			DriverUtil.html.ScreenCapture(DriverUtil.driver);
		}
	}

	public void LoginTest001_010() throws Exception {
		// 测试数据和期望结果
		String[] username = new String[] { "admin", "admin", "  ", "",
				"admin1234", "admin", "@#$%^&*)(\"\"\"\'\'\'\"\'",
				"a1234567890123456789012345678901234567890", "admin" };
		String[] password = new String[] { "admin", "    ", "123456", "",
				"123456", "123", "@#$%^&*)(\"\"\"\'\'\'\"\'", "1234",
				"a1234567890123456789012345678901234567890" };
		String[] expect = new String[] { "Login was denied!",
				"Login was denied!", "Login was denied!",
				"User name is invalid!", "Login was denied!",
				"Login was denied!", "Login was denied!", "Login was denied!",
				"Login was denied!" };
		// Login001-006
		for (int i = 0; i < username.length; i++) {
			Login(username[i], password[i], expect[i]);
		}
		// Login007
		try {
			// 登录
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser)).clear();
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser))
					.sendKeys(EtcIO.userName);
			Start2End.Sleep(1000);
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))
					.clear();
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))
					.sendKeys(EtcIO.userPwd);
			// SendKeys.SendWait("~");
			DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogin))
					.click();
		} catch (Exception e) {
			DriverUtil.html.InsertHtml("Login",
					"In order to check the Login-function is OK!",
					EtcIO.ErrorTag, "none", "none", "Input correct user:"
							+ EtcIO.userName + " and password:" + EtcIO.userPwd
							+ ", and then click login-button.", "none",
					e.getMessage(), "none");
			DriverUtil.html.ScreenCapture(DriverUtil.driver);
		}
		String expPath = "//span[text()='Information - CUBRID Database Server']";
		boolean flag = Start2End.IsElementPresent(By.xpath(expPath));
		if (flag) {
			DriverUtil.html.InsertHtml("Login",
					"In order to check the Login-function is OK!",
					EtcIO.PassTag, "none", "none", "Input correct user:"
							+ EtcIO.userName + " and password:" + EtcIO.userPwd
							+ ", and then click login-button.", expPath,
					"Have found successfully!", "none");
		} else {
			DriverUtil.html.InsertHtml("Login",
					"In order to check the Login-function is OK!",
					EtcIO.FailTag, "none", "none", "Input user:"
							+ EtcIO.userName + " and password:" + EtcIO.userPwd
							+ ", and then click login-button.", expPath,
					"Have not found!", "none");
			DriverUtil.html.ScreenCapture(DriverUtil.driver);
		}
		Assert.assertTrue(flag);
		Start2End.Logout(this.baseUrl);
	}

	public void LogoutTest001_002() throws Exception {
		Start2End.Login(this.baseUrl);
		// Logout001
		try {
			// 退出
			DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogout))
					.click();
			Start2End.Sleep(1000);
			DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsPopNo))
					.click();
			Start2End.Sleep(1000);
		} catch (Exception e) {
			DriverUtil.html.InsertHtml("Login",
					"In order to check the Login-function is OK!",
					EtcIO.ErrorTag, "none", "none",
					"Login and click Logout-button. Then click Cancel-button.",
					e.getMessage(), "Have not found!", "none");
			DriverUtil.html.ScreenCapture(DriverUtil.driver);
		}
		String expPath = "//span[text()='Information - CUBRID Database Server']";
		boolean flag = Start2End.IsElementPresent(By.xpath(expPath));
		if (flag) {
			DriverUtil.html.InsertHtml("Logout",
					"In order to check the Login-function is OK!",
					EtcIO.PassTag, "none", "none",
					"Login and click Logout-button. Then click Cancel-button.",
					expPath, "Have found successfully!", "none");
		} else {
			DriverUtil.html.InsertHtml("Logout",
					"In order to check the Logout-function is OK!",
					EtcIO.FailTag, "none", "none",
					"Login and click Logout-button. Then click Cancel-button.",
					expPath, "Have not found!", "none");
			DriverUtil.html.ScreenCapture(DriverUtil.driver);
		}
		// Logout002
		try {
			DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogout))
					.click();
			Start2End.Sleep(1000);
			DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsPopYes))
					.click();
			Start2End.Sleep(1000);
		} catch (Exception e) {
			DriverUtil.html.InsertHtml("Logout",
					"In order to check the Logout-function is OK!",
					EtcIO.ErrorTag, "none", "none",
					"Login and click Logout-button. Then click OK-button.",
					e.getMessage(), "Have not found!", "none");
			DriverUtil.html.ScreenCapture(DriverUtil.driver);
		}
		expPath = ICwmXpath.xcmsLogin;
		flag = Start2End.IsElementPresent(By.xpath(expPath));
		if (flag) {
			DriverUtil.html.InsertHtml("Logout",
					"In order to check the Logout-function is OK!",
					EtcIO.PassTag, "none", "none",
					"Login and click Logout-button. Then click OK-button..",
					expPath, "Have found successfully!", "none");
		} else {
			DriverUtil.html.InsertHtml("Logout",
					"In order to check the Logout-function is OK!",
					EtcIO.FailTag, "none", "none",
					"Login and click Logout-button. Then click OK-button.",
					expPath, "Have not found!", "none");
			DriverUtil.html.ScreenCapture(DriverUtil.driver);
		}
		Assert.assertTrue(flag);
	}

	public void ChangePwdTest001_028() throws Exception {
		// 测试数据
		String[] oldPassword = new String[] { EtcIO.userPwd, "214598", "   ",
				EtcIO.userPwd, EtcIO.userPwd, EtcIO.userPwd, EtcIO.userPwd,
				"abc", "(!@#$%^&*)(\"\"\"\'\'\'\"\'",
				"a1234567890123456789012345678901234567890", EtcIO.userPwd,
				"(!@#$%^&*)(\"\"\"\'\'\'\"\'", EtcIO.userPwd,
				"a1234567890123456789012345678901234567890" };
		String[] newPassword = new String[] { "214598", "123456", "123456",
				"    ", "", "214", EtcIO.userPwd, EtcIO.userPwd, EtcIO.userPwd,
				EtcIO.userPwd, "(!@#$%^&*)(\"\"\"\'\'\'\"\'", EtcIO.userPwd,
				"a1234567890123456789012345678901234567890", EtcIO.userPwd };
		String[] confirmPassword = new String[] { "214598", "123456", "123456",
				"    ", "", "214", EtcIO.userPwd, EtcIO.userPwd, EtcIO.userPwd,
				EtcIO.userPwd, "(!@#$%^&*)(\"\"\"\'\'\'\"\'", EtcIO.userPwd,
				"a1234567890123456789012345678901234567890", EtcIO.userPwd };
		String[] expect = new String[] { "changed successfully!",
				"changed successfully!", "Please input the old password!",
				"Invalid password.", "Invalid password.", "Invalid password.",
				"changed successfully!", "The old password is incorrect!",
				"The old password is incorrect!",
				"The old password is incorrect!", "changed successfully!",
				"changed successfully!", "changed successfully!",
				"changed successfully!" };
		String checkPath = "//span[@role='presentation']";
		Start2End.Login(this.baseUrl);
		// ChangePwdTest001_014
		for (int i = 0; i < oldPassword.length; i++) {
			try {
				// Cancel
				if (Start2End.IsElementPresent(By.xpath(ICwmXpath.xcmsChange))) {
					DriverUtil.driver.findElement(
							By.xpath(ICwmXpath.xcmsChange)).click();
					Start2End.Sleep(1000);
				} else {
					continue;
				}
				DriverUtil.driver.findElement(By.name(ICwmXpath.xnameOldPwd))
						.clear();
				DriverUtil.driver.findElement(By.name(ICwmXpath.xnameOldPwd))
						.sendKeys(oldPassword[i]);
				Start2End.Sleep(1000);
				DriverUtil.driver.findElement(By.name(ICwmXpath.xnameNewPwd))
						.clear();
				DriverUtil.driver.findElement(By.name(ICwmXpath.xnameNewPwd))
						.sendKeys(newPassword[i]);
				Start2End.Sleep(1000);
				DriverUtil.driver.findElement(
						By.name(ICwmXpath.xnameConfirmPwd)).clear();
				DriverUtil.driver.findElement(
						By.name(ICwmXpath.xnameConfirmPwd)).sendKeys(
						confirmPassword[i]);
				Start2End.Sleep(1000);
				DriverUtil.driver.findElement(
						By.xpath(ICwmXpath.xcmsChangeCancel)).click();
				Start2End.Sleep(1000);
				String temp = "//span[text()='Information - CUBRID Database Server']";
				boolean flag = Start2End.IsElementPresent(By.xpath(temp));
				if (flag) {
					Start2End.InsertHtml("ChangePassword",
							"Login and start to change cms-user's password.(oldPassword:"
									+ oldPassword[i] + "&newPassword:"
									+ newPassword[i] + "&confirmPassword:"
									+ confirmPassword[i]
									+ "). Then click Cancel-button.", temp,
							"Have found successfully!", EtcIO.PassTag);
				} else {
					Start2End.InsertHtml("ChangePassword",
							"Login and start to change cms-user's password.(oldPassword:"
									+ oldPassword[i] + "&newPassword:"
									+ newPassword[i] + "&confirmPassword:"
									+ confirmPassword[i]
									+ "). Then click Cancel-button.", temp,
							"Have not found!", EtcIO.FailTag);
					DriverUtil.html.ScreenCapture(DriverUtil.driver);
				}

				// OK
				if (Start2End.IsElementPresent(By.xpath(ICwmXpath.xcmsChange))) {
					DriverUtil.driver.findElement(
							By.xpath(ICwmXpath.xcmsChange)).click();
					Start2End.Sleep(1000);
				} else {
					continue;
				}
				DriverUtil.driver.findElement(By.name(ICwmXpath.xnameOldPwd))
						.clear();
				DriverUtil.driver.findElement(By.name(ICwmXpath.xnameOldPwd))
						.sendKeys(oldPassword[i]);
				Start2End.Sleep(1000);
				DriverUtil.driver.findElement(By.name(ICwmXpath.xnameNewPwd))
						.clear();
				DriverUtil.driver.findElement(By.name(ICwmXpath.xnameNewPwd))
						.sendKeys(newPassword[i]);
				Start2End.Sleep(1000);
				DriverUtil.driver.findElement(
						By.name(ICwmXpath.xnameConfirmPwd)).clear();
				DriverUtil.driver.findElement(
						By.name(ICwmXpath.xnameConfirmPwd)).sendKeys(
						confirmPassword[i]);
				Start2End.Sleep(1000);
				// Start2End.ClickOK(DriverUtil.driver);
				DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsChangeOk))
						.click();
				Start2End.Sleep(1000);
				temp = DriverUtil.driver.findElement(By.xpath(checkPath))
						.getText();
				flag = temp.trim().contains(expect[i]);
				if (flag) {
					Start2End.InsertHtml("ChangePassword",
							"Login and start to change cms-user's password.(oldPassword:"
									+ oldPassword[i] + "&newPassword:"
									+ newPassword[i] + "&confirmPassword:"
									+ confirmPassword[i]
									+ "). Then click OK-button.", expect[i],
							temp, EtcIO.PassTag);
				} else {
					Start2End.InsertHtml("ChangePassword",
							"Login and start to change cms-user's password.(oldPassword:"
									+ oldPassword[i] + "&newPassword:"
									+ newPassword[i] + "&confirmPassword:"
									+ confirmPassword[i]
									+ "). Then click OK-button.", expect[i],
							temp, EtcIO.FailTag);
					DriverUtil.html.ScreenCapture(DriverUtil.driver);
				}
			} catch (Exception e) {
				Start2End.InsertHtml("ChangePassword",
						"Login and start to change cms-user's password.(oldPassword:"
								+ oldPassword[i] + "&newPassword:"
								+ newPassword[i] + "&confirmPassword:"
								+ confirmPassword[i] + ").", expect[i],
						e.getMessage(), EtcIO.ErrorTag);
				DriverUtil.html.ScreenCapture(DriverUtil.driver);
			}
			// DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsChangeOk)).click();
			Start2End.ClickOK(DriverUtil.driver);
			Start2End.Sleep(1000);
			try {
				DriverUtil.driver.findElement(
						By.xpath(ICwmXpath.xcmsChangeCancel)).click();
				Start2End.Sleep(1000);
			} catch (Exception ex) {
			}
		}
		Start2End.Logout(this.baseUrl);
	}
}


(四)测试用例报告生成统计

Start2End类:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import com.nhn.platform.qa.cwmtest.Utils.DriverUtil;
import com.nhn.platform.qa.cwmtest.Utils.EtcIO;

public class Start2End extends TestCase {
	public Start2End(String name) {
		super(name);
	}

	// @Test
	public void EndJUnitTest() throws Exception {
		DriverUtil.html.CompleteCount();
		DriverUtil.StopService();
		System.out.println("Complete to clear ENV!");
	}

	public static Test suite() {
		TestSuite suite = new TestSuite("Test for Start2End");
		// $JUnit-BEGIN$
		suite.addTest(new Start2End("EndJUnitTest"));
		// $JUnit-END$
		return suite;
	}
    public static void ClickYes(WebDriver driver){
    	try {
    		driver.findElement(By.xpath(ICwmXpath.xcmsPopYes))
					.click();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    public static void ClickOK(WebDriver driver){
    	try {
    		driver.findElement(By.xpath(ICwmXpath.xcmsPopOk))
					.click();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	public static void Login(String baseUrl) {
		DriverUtil.driver.get(baseUrl);
		Sleep(3000);
		try {
			// 登录
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser)).clear();
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser))
					.sendKeys(EtcIO.userName);
			Sleep(1000);
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))
					.clear();
			DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))
					.sendKeys(EtcIO.userPwd);
			// SendKeys.SendWait("~");
			DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogin))
					.click();
			Sleep(1000);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void Logout(String baseUrl) {
		DriverUtil.driver.get(baseUrl);
		Sleep(3000);
		try {
			// 退出
			DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogout))
					.click();
			Sleep(1000);
			DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsPopYes))
					.click();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static boolean IsElementPresent(By by) {
		try {
			DriverUtil.driver.findElement(by);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	public static void Sleep(long milsecond) {
		try {
			Thread.sleep(milsecond);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void InsertHtml(String taskname,String steps,String expects,String results,String resultTag){
		DriverUtil.html.InsertHtml(taskname,
				"In order to check the "+taskname+"-function is OK!",
				resultTag, "none", "none", steps, expects,
				results, "none");
	}
}

 

(五)JUnit调用执行测试类AppTest

(以上代码均在 src/main/java目录下,src/test/java目录下只保留执行测试类AppTest)

AppTest类:

package com.nhn.platform.qa.cwmtest.cwmautotest;

import org.junit.runner.RunWith; 
import org.junit.runners.Suite;

import com.nhn.platform.qa.cwmtest.firefox.*;
//import com.nhn.platform.qa.cwmtest.chrome.*;
//import com.nhn.platform.qa.cwmtest.ie.*;

@RunWith(Suite.class)

@Suite.SuiteClasses({ 
	//SampleTest.class,
	HostTest.class,
	FrameTest.class,
	Start2End.class
})
public class AppTest{
}



 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值