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

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


(一)conf目录及配置文件

  1. ####################################################################################################  
  2. # Switch controller:   
  3. <browser.type>  
  4. # :: 0 is chrome  
  5. # :: 1 is firefox  
  6. # :: 2 is IE  
  7. browser.type=0  
  8.   
  9. ###############################  
  10. # chrome driver path:   
  11. <chromeDriverPath> path of chrome driver  
  12. #  
  13. browser.chromeDriverPath=conf/chromedriver.exe  
  14.   
  15. ###############################  
  16. # IE driver path:   
  17. <IEDriverPath> path of IE driver  
  18. # IE 9 suitable  
  19. #  
  20. #browser.IEDriverPath=conf\\IEDriverServer.exe  
  21. browser.IEDriverPath=conf/IEDriverServer.exe  
  22.   
  23. ###############################  
  24. # Business Server config:  
  25. <cwmServer.BaseUrl> is the tested web address   
  26. cwmServer.BaseUrl=https://10.34.64.213:8282  
  27.   
  28. ##############################  
  29. # User config: used to login the business server  
  30. <userName>: account of user id  
  31. <userPwd> : password of account  
  32. #  
  33. userName=admin  
  34. userPwd=123456  
  35. Language=English  
  36. #Language=한국어  
  37. #############################  
  38. # Test cases result path:  
  39. <result.picWidth>: The width of picture which will be screenshot when error occur.   
  40. <result.picHeight>: The height of picture which will be screenshot when error occur.   
  41. result.picWidth=1024  
  42. result.picHeight=768  
  43.   
  44. #############################  
  45. #HtmlFormat: project name and work path  
  46. HtmlDoc.ProjectName=CWM  
  47. HtmlDoc.HomePath=C:\\ECSTOOL  
  48. HtmlDoc.DirPath=C:\\ECSTOOL\\nginx\\html\\CWM  
  49. #HtmlFormat: html model sample  
  50. HtmlDoc.IndexModel=conf/ListSample.htm  
  51. HtmlDoc.DetailModel=conf/DetailSample.htm  
  52. 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>  
  53. #HtmlFormat: web report address  
  54. HtmlDoc.HttpPath=http://10.34.130.62/CWM  
  55. #HtmlFormat: test code address  
  56. HtmlDoc.ScriptPath=http://svn.platform.nhncorp.cn/qa_repository/common/document/Technology document/IronPythonTool/AutoTestCWM/cwm-auto-test  


EtcIO类:

  1. package com.nhn.platform.qa.cwmtest.Utils;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileReader;  
  6. import java.io.FileWriter;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.RandomAccessFile;  
  10. import java.util.Properties;  
  11.   
  12. public class EtcIO {  
  13.   
  14.     static String confPath = "conf/conf.properties";  
  15.     static int browserType = Integer.parseInt(readValue("browser.type"));   
  16.     // 0 -  
  17.     // chrome,  
  18.     // others  
  19.     // -  
  20.     // firefox  
  21.   
  22.     // chrome driver path  
  23.     public static String chromeDriverPath = readValue("browser.chromeDriverPath");  
  24.     // ie driver path  
  25.     public static String IEDriverPath = readValue("browser.IEDriverPath");  
  26.   
  27.     // Business Server IP Address and port number  
  28.     public static String baseUrl = readValue("cwmServer.BaseUrl");  
  29.   
  30.     // admin user and password  
  31.     public static String userName = readValue("userName");  
  32.     public static String userPwd = readValue("userPwd");  
  33.     public static String language = readValue("Language");  
  34.   
  35.     // log jQuery dir  
  36.     public static String jQueryPath = readValue("jQuery.path");  
  37.     // Log Small Pic size  
  38.     public static int logPicWidth = Integer.parseInt(readValue("result.picWidth"));  
  39.     public static int logPicHeight = Integer.parseInt(readValue("result.picHeight"));  
  40.       
  41.     // XML file tag name  
  42.     public static String RunTag = "Run";  
  43.     public static String PassTag = "Pass";  
  44.     public static String FailTag = "Fail";  
  45.     public static String ErrorTag = "Error";  
  46.   
  47.     public static String readValue(String key) {  
  48.         Properties props = new Properties();  
  49.         try {  
  50.             InputStream in = new BufferedInputStream(new FileInputStream(  
  51.                     confPath));  
  52.             props.load(in);  
  53.             String value = props.getProperty(key);  
  54.             return value;  
  55.         } catch (Exception e) {  
  56.             e.printStackTrace();  
  57.             return null;  
  58.         }  
  59.     }  
  60.   
  61.     public static void ReplaceContent(String sourcePath, String targetPath,  
  62.             String[] search, String[] replace) {  
  63.         try {  
  64.             FileReader reader = new FileReader(sourcePath);  
  65.             char[] dates = new char[1024];  
  66.             int count = 0;  
  67.             StringBuilder sb = new StringBuilder();  
  68.             while ((count = reader.read(dates)) > 0) {  
  69.                 String str = String.valueOf(dates, 0, count);  
  70.                 sb.append(str);  
  71.             }  
  72.             reader.close();  
  73.             // 从构造器中生成字符串,并替换搜索文本  
  74.             String str = sb.toString();  
  75.             for (int i = 0; i < search.length; i++) {  
  76.                 str = str.replace(search[i], replace[i]);  
  77.             }  
  78.             FileWriter writer = new FileWriter(targetPath);  
  79.             writer.write(str.toCharArray());  
  80.             writer.close();  
  81.         } catch (Exception e) {  
  82.             e.printStackTrace();  
  83.         }  
  84.     }  
  85.   
  86.     /**  
  87.      * 追加文件:使用RandomAccessFile  
  88.      *   
  89.      * @param fileName  
  90.      *            文件名  
  91.      * @param content  
  92.      *            追加的内容  
  93.      */  
  94.     public static void AppendContent(String fileName, String content) {  
  95.         RandomAccessFile randomFile = null;  
  96.         try {  
  97.             // 打开一个随机访问文件流,按读写方式  
  98.             randomFile = new RandomAccessFile(fileName, "rw");  
  99.             // 文件长度,字节数  
  100.             long fileLength = randomFile.length();  
  101.             // 将写文件指针移到文件尾。  
  102.             randomFile.seek(fileLength);  
  103.             randomFile.writeBytes(content);  
  104.         } catch (IOException e) {  
  105.             e.printStackTrace();  
  106.         } finally {  
  107.             if (randomFile != null) {  
  108.                 try {  
  109.                     randomFile.close();  
  110.                 } catch (IOException e) {  
  111.                     e.printStackTrace();  
  112.                 }  
  113.             }  
  114.         }  
  115.     }  
  116. }  

 

(二)Selenium Web Driver驱动


DriverUtil类:

  1. package com.nhn.platform.qa.cwmtest.Utils;  
  2.   
  3. import java.awt.Dimension;  
  4. import java.awt.Toolkit;  
  5. import java.io.File;  
  6. import java.io.IOException;  
  7. import java.util.concurrent.TimeUnit;  
  8.   
  9. import org.openqa.selenium.JavascriptExecutor;  
  10. import org.openqa.selenium.WebDriver;  
  11. import org.openqa.selenium.chrome.ChromeDriverService;  
  12. import org.openqa.selenium.firefox.FirefoxDriver;  
  13. import org.openqa.selenium.ie.InternetExplorerDriver;  
  14. import org.openqa.selenium.ie.InternetExplorerDriverService;  
  15. import org.openqa.selenium.remote.DesiredCapabilities;  
  16. import org.openqa.selenium.remote.RemoteWebDriver;  
  17.   
  18. /********************  
  19.  * @description  
  20.  * @version 1.0  
  21.  * @author Frank Wu  
  22.  * @update 2013-1-25 上午10:21:08  
  23.  ********************/  
  24.   
  25. public class DriverUtil {  
  26.   
  27.     private static ChromeDriverService chservice;  
  28.     private static InternetExplorerDriverService ieservice;  
  29.     private static Dimension screenDims = Toolkit.getDefaultToolkit()  
  30.             .getScreenSize();  
  31.   
  32.     public static HtmlDoc html = new HtmlDoc();  
  33.     public static WebDriver driver;  
  34.   
  35.     // public static void StartService(int browserType) {  
  36.     // switch(browserType){  
  37.     // case BrowserType.ChromeType:  
  38.     // case BrowserType.ChromeFirefox:  
  39.     // StartChromeService();  
  40.     // break;  
  41.     // case BrowserType.IEType:  
  42.     // case BrowserType.IEFirefox:  
  43.     // StartIEService();  
  44.     // break;  
  45.     // case BrowserType.ChromeIE:  
  46.     // case BrowserType.ChromeIEFirefox:  
  47.     // StartChromeService();  
  48.     // StartIEService();  
  49.     // break;  
  50.     // default:  
  51.     // break;  
  52.     // }  
  53.     // }  
  54.     private static void StartChromeService() {  
  55.         if (EtcIO.chromeDriverPath == null || EtcIO.chromeDriverPath.equals("")) {  
  56.             EtcIO.chromeDriverPath = "conf/chromedriver";  
  57.         }  
  58.         chservice = new ChromeDriverService.Builder()  
  59.                 .usingChromeDriverExecutable(new File(EtcIO.chromeDriverPath))  
  60.                 .usingAnyFreePort().build();  
  61.         try {  
  62.             System.setProperty("webdriver.chrome.driver",  
  63.                     EtcIO.chromeDriverPath);  
  64.             chservice.start();  
  65.         } catch (IOException e) {  
  66.             // TODO Auto-generated catch block  
  67.             e.printStackTrace();  
  68.         }  
  69.     }  
  70.   
  71.     private static void StartIEService() {  
  72.         if (EtcIO.IEDriverPath == null || EtcIO.IEDriverPath.equals("")) {  
  73.             EtcIO.IEDriverPath = "conf/IEDriverServer.exe";  
  74.         }  
  75.         ieservice = new InternetExplorerDriverService.Builder()  
  76.                 .usingDriverExecutable(new File(EtcIO.IEDriverPath))  
  77.                 .usingAnyFreePort().build();  
  78.         try {  
  79.             System.setProperty("webdriver.ie.driver", EtcIO.IEDriverPath);  
  80.             ieservice.start();  
  81.         } catch (IOException e) {  
  82.             // TODO Auto-generated catch block  
  83.             e.printStackTrace();  
  84.         }  
  85.     }  
  86.   
  87.     public static void StopService() {  
  88.         if (chservice != null && chservice.isRunning()) {  
  89.             chservice.stop();  
  90.         }  
  91.         if (ieservice != null && ieservice.isRunning()) {  
  92.             ieservice.stop();  
  93.         }  
  94.     }  
  95.   
  96.     public static void ChromeStart() {  
  97.         QuitDriver();  
  98.         if (chservice == null) {  
  99.             StartChromeService();  
  100.         }  
  101.         driver = new RemoteWebDriver(chservice.getUrl(),  
  102.                 DesiredCapabilities.chrome());  
  103.         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  
  104.         MaxiMise(driver);  
  105.     }  
  106.   
  107.     public static void IEStart() {  
  108.         QuitDriver();  
  109.         if (ieservice == null) {  
  110.             StartIEService();  
  111.         }  
  112.         DesiredCapabilities ieCapabilities = DesiredCapabilities  
  113.                 .internetExplorer();  
  114.         ieCapabilities  
  115.                 .setCapability(  
  116.                         InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,  
  117.                         true);  
  118.         driver = new RemoteWebDriver(ieservice.getUrl(), ieCapabilities);  
  119.         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  
  120.         MaxiMise(driver);  
  121.     }  
  122.   
  123.     public static void FirefoxStart() {  
  124.         QuitDriver();  
  125.         driver = new FirefoxDriver();  
  126.         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  
  127.         MaxiMise(driver);  
  128.     }  
  129.   
  130.     public static void QuitDriver() {  
  131.         if (driver != null) {  
  132.             driver.quit();  
  133.         }  
  134.     }  
  135.   
  136.     // 使窗口最大化函数  
  137.     public static void MaxiMise(WebDriver driver) {  
  138.         final JavascriptExecutor js = (JavascriptExecutor) driver;  
  139.         js.executeScript("window.open('','testwindow','width=400,height=200')");  
  140.         driver.close();  
  141.         driver.switchTo().window("testwindow");  
  142.         js.executeScript("window.moveTo(0,0);");  
  143.         int width = (int) screenDims.getWidth();  
  144.         int height = (int) screenDims.getHeight();  
  145.         /*  
  146.          * 1280和1024分别为窗口的宽和高,可以用下面的代码得到 screenDims =  
  147.          * Toolkit.getDefaultToolkit().getScreenSize(); width = (int)  
  148.          * screenDims.getWidth(); height = (int) screenDims.getHeight();  
  149.          */  
  150.         js.executeScript("window.resizeTo(" + width + "," + height + ");");  
  151.     }  
  152. }  


 (三)测试类示例

HostTest类:

  1. package com.nhn.platform.qa.cwmtest.firefox;  
  2.   
  3. import junit.framework.Assert;  
  4. import junit.framework.Test;  
  5. import junit.framework.TestCase;  
  6. import junit.framework.TestSuite;  
  7.   
  8. import org.junit.After;  
  9. import org.junit.Before;  
  10. import org.openqa.selenium.By;  
  11. import org.openqa.selenium.WebDriver;  
  12.   
  13. import com.nhn.platform.qa.cwmtest.Utils.*;  
  14. import com.nhn.platform.qa.cwmtest.cwmautotest.ICwmXpath;  
  15. import com.nhn.platform.qa.cwmtest.cwmautotest.Start2End;  
  16.   
  17. /********************  
  18.  * @description  
  19.  * @version 1.0  
  20.  * @author Frank Wu  
  21.  * @update 2013-1-28 上午11:34:07  
  22.  ********************/  
  23.   
  24. public class HostTest extends TestCase {  
  25.     private String baseUrl;  
  26.     private StringBuffer verificationErrors = new StringBuffer();  
  27.     public HostTest(String name) {  
  28.         super(name);  
  29.     }  
  30.     @Before  
  31.     public void setUp() throws Exception {  
  32.         DriverUtil.FirefoxStart();  
  33.         baseUrl = EtcIO.baseUrl;  
  34.     }  
  35.     @After  
  36.     public void tearDown() throws Exception {  
  37.         DriverUtil.driver.quit();  
  38.         String verificationErrorString = verificationErrors.toString();  
  39.         if (!"".equals(verificationErrorString)) {  
  40.             // fail(verificationErrorString);  
  41.         }  
  42.     }  
  43.     public static Test suite() {  
  44.         TestSuite suite = new TestSuite("Test for HostTest");  
  45.         // $JUnit-BEGIN$  
  46.         suite.addTest(new HostTest("LoginTest001_010"));  
  47.         suite.addTest(new HostTest("LogoutTest001_002"));  
  48.         suite.addTest(new HostTest("ChangePwdTest001_028"));  
  49.         // $JUnit-END$  
  50.         return suite;  
  51.     }  
  52.   
  53.     public void Login(String user, String password, String expect) {  
  54.         try {  
  55.             DriverUtil.driver.get(baseUrl);  
  56.             Start2End.Sleep(3000);  
  57.             String expectPath = "//span[@role='presentation']";  
  58.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser)).clear();  
  59.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser))  
  60.                     .sendKeys(user);  
  61.             Start2End.Sleep(1000);  
  62.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))  
  63.                     .clear();  
  64.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))  
  65.                     .sendKeys(password);  
  66.             DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogin))  
  67.                     .click();  
  68.             Start2End.Sleep(1000);  
  69.             String temp = DriverUtil.driver.findElement(By.xpath(expectPath))  
  70.                     .getText();  
  71.             // DriverUtil.html.InsertHtml(TaskName, TestSummary, TestResult,  
  72.             // Comments, Precondition, Steps, Expects, Results, Remarks)  
  73.             boolean flag = temp.trim().contains(expect);  
  74.             DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsPopOk))  
  75.                     .click();  
  76.             if (flag) {  
  77.                 DriverUtil.html.InsertHtml("Login",  
  78.                         "In order to check the Login-function is OK!",  
  79.                         EtcIO.PassTag, "none", "none",  
  80.                         "Input incorrect username:" + user + " and password:"  
  81.                                 + password + ", and then click login-button.",  
  82.                         expect, temp, "none");  
  83.             } else {  
  84.                 DriverUtil.html.InsertHtml("Login",  
  85.                         "In order to check the Login-function is OK!",  
  86.                         EtcIO.FailTag, "none", "none",  
  87.                         "Input incorrect username:" + user + " and password:"  
  88.                                 + password + ", and then click login-button.",  
  89.                         expect, temp, "none");  
  90.                 DriverUtil.html.ScreenCapture(DriverUtil.driver);  
  91.             }  
  92.             // Assert.assertTrue(flag);  
  93.         } catch (Exception e) {  
  94.             DriverUtil.html.InsertHtml("Login",  
  95.                     "In order to check the Login-function is OK!",  
  96.                     EtcIO.ErrorTag, "exception error", "none",  
  97.                     "Input incorrect username:" + user + " and password:"  
  98.                             + password + ", and then click login-button.",  
  99.                     expect, e.getMessage(), "none");  
  100.             DriverUtil.html.ScreenCapture(DriverUtil.driver);  
  101.         }  
  102.     }  
  103.   
  104.     public void LoginTest001_010() throws Exception {  
  105.         // 测试数据和期望结果  
  106.         String[] username = new String[] { "admin", "admin", "  ", "",  
  107.                 "admin1234", "admin", "@#$%^&*)(\"\"\"\'\'\'\"\'",  
  108.                 "a1234567890123456789012345678901234567890", "admin" };  
  109.         String[] password = new String[] { "admin", "    ", "123456", "",  
  110.                 "123456", "123", "@#$%^&*)(\"\"\"\'\'\'\"\'", "1234",  
  111.                 "a1234567890123456789012345678901234567890" };  
  112.         String[] expect = new String[] { "Login was denied!",  
  113.                 "Login was denied!", "Login was denied!",  
  114.                 "User name is invalid!", "Login was denied!",  
  115.                 "Login was denied!", "Login was denied!", "Login was denied!",  
  116.                 "Login was denied!" };  
  117.         // Login001-006  
  118.         for (int i = 0; i < username.length; i++) {  
  119.             Login(username[i], password[i], expect[i]);  
  120.         }  
  121.         // Login007  
  122.         try {  
  123.             // 登录  
  124.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser)).clear();  
  125.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser))  
  126.                     .sendKeys(EtcIO.userName);  
  127.             Start2End.Sleep(1000);  
  128.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))  
  129.                     .clear();  
  130.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))  
  131.                     .sendKeys(EtcIO.userPwd);  
  132.             // SendKeys.SendWait("~");  
  133.             DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogin))  
  134.                     .click();  
  135.         } catch (Exception e) {  
  136.             DriverUtil.html.InsertHtml("Login",  
  137.                     "In order to check the Login-function is OK!",  
  138.                     EtcIO.ErrorTag, "none", "none", "Input correct user:"  
  139.                             + EtcIO.userName + " and password:" + EtcIO.userPwd  
  140.                             + ", and then click login-button.", "none",  
  141.                     e.getMessage(), "none");  
  142.             DriverUtil.html.ScreenCapture(DriverUtil.driver);  
  143.         }  
  144.         String expPath = "//span[text()='Information - CUBRID Database Server']";  
  145.         boolean flag = Start2End.IsElementPresent(By.xpath(expPath));  
  146.         if (flag) {  
  147.             DriverUtil.html.InsertHtml("Login",  
  148.                     "In order to check the Login-function is OK!",  
  149.                     EtcIO.PassTag, "none", "none", "Input correct user:"  
  150.                             + EtcIO.userName + " and password:" + EtcIO.userPwd  
  151.                             + ", and then click login-button.", expPath,  
  152.                     "Have found successfully!", "none");  
  153.         } else {  
  154.             DriverUtil.html.InsertHtml("Login",  
  155.                     "In order to check the Login-function is OK!",  
  156.                     EtcIO.FailTag, "none", "none", "Input user:"  
  157.                             + EtcIO.userName + " and password:" + EtcIO.userPwd  
  158.                             + ", and then click login-button.", expPath,  
  159.                     "Have not found!", "none");  
  160.             DriverUtil.html.ScreenCapture(DriverUtil.driver);  
  161.         }  
  162.         Assert.assertTrue(flag);  
  163.         Start2End.Logout(this.baseUrl);  
  164.     }  
  165.   
  166.     public void LogoutTest001_002() throws Exception {  
  167.         Start2End.Login(this.baseUrl);  
  168.         // Logout001  
  169.         try {  
  170.             // 退出  
  171.             DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogout))  
  172.                     .click();  
  173.             Start2End.Sleep(1000);  
  174.             DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsPopNo))  
  175.                     .click();  
  176.             Start2End.Sleep(1000);  
  177.         } catch (Exception e) {  
  178.             DriverUtil.html.InsertHtml("Login",  
  179.                     "In order to check the Login-function is OK!",  
  180.                     EtcIO.ErrorTag, "none", "none",  
  181.                     "Login and click Logout-button. Then click Cancel-button.",  
  182.                     e.getMessage(), "Have not found!", "none");  
  183.             DriverUtil.html.ScreenCapture(DriverUtil.driver);  
  184.         }  
  185.         String expPath = "//span[text()='Information - CUBRID Database Server']";  
  186.         boolean flag = Start2End.IsElementPresent(By.xpath(expPath));  
  187.         if (flag) {  
  188.             DriverUtil.html.InsertHtml("Logout",  
  189.                     "In order to check the Login-function is OK!",  
  190.                     EtcIO.PassTag, "none", "none",  
  191.                     "Login and click Logout-button. Then click Cancel-button.",  
  192.                     expPath, "Have found successfully!", "none");  
  193.         } else {  
  194.             DriverUtil.html.InsertHtml("Logout",  
  195.                     "In order to check the Logout-function is OK!",  
  196.                     EtcIO.FailTag, "none", "none",  
  197.                     "Login and click Logout-button. Then click Cancel-button.",  
  198.                     expPath, "Have not found!", "none");  
  199.             DriverUtil.html.ScreenCapture(DriverUtil.driver);  
  200.         }  
  201.         // Logout002  
  202.         try {  
  203.             DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogout))  
  204.                     .click();  
  205.             Start2End.Sleep(1000);  
  206.             DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsPopYes))  
  207.                     .click();  
  208.             Start2End.Sleep(1000);  
  209.         } catch (Exception e) {  
  210.             DriverUtil.html.InsertHtml("Logout",  
  211.                     "In order to check the Logout-function is OK!",  
  212.                     EtcIO.ErrorTag, "none", "none",  
  213.                     "Login and click Logout-button. Then click OK-button.",  
  214.                     e.getMessage(), "Have not found!", "none");  
  215.             DriverUtil.html.ScreenCapture(DriverUtil.driver);  
  216.         }  
  217.         expPath = ICwmXpath.xcmsLogin;  
  218.         flag = Start2End.IsElementPresent(By.xpath(expPath));  
  219.         if (flag) {  
  220.             DriverUtil.html.InsertHtml("Logout",  
  221.                     "In order to check the Logout-function is OK!",  
  222.                     EtcIO.PassTag, "none", "none",  
  223.                     "Login and click Logout-button. Then click OK-button..",  
  224.                     expPath, "Have found successfully!", "none");  
  225.         } else {  
  226.             DriverUtil.html.InsertHtml("Logout",  
  227.                     "In order to check the Logout-function is OK!",  
  228.                     EtcIO.FailTag, "none", "none",  
  229.                     "Login and click Logout-button. Then click OK-button.",  
  230.                     expPath, "Have not found!", "none");  
  231.             DriverUtil.html.ScreenCapture(DriverUtil.driver);  
  232.         }  
  233.         Assert.assertTrue(flag);  
  234.     }  
  235.   
  236.     public void ChangePwdTest001_028() throws Exception {  
  237.         // 测试数据  
  238.         String[] oldPassword = new String[] { EtcIO.userPwd, "214598", "   ",  
  239.                 EtcIO.userPwd, EtcIO.userPwd, EtcIO.userPwd, EtcIO.userPwd,  
  240.                 "abc", "(!@#$%^&*)(\"\"\"\'\'\'\"\'",  
  241.                 "a1234567890123456789012345678901234567890", EtcIO.userPwd,  
  242.                 "(!@#$%^&*)(\"\"\"\'\'\'\"\'", EtcIO.userPwd,  
  243.                 "a1234567890123456789012345678901234567890" };  
  244.         String[] newPassword = new String[] { "214598", "123456", "123456",  
  245.                 "    ", "", "214", EtcIO.userPwd, EtcIO.userPwd, EtcIO.userPwd,  
  246.                 EtcIO.userPwd, "(!@#$%^&*)(\"\"\"\'\'\'\"\'", EtcIO.userPwd,  
  247.                 "a1234567890123456789012345678901234567890", EtcIO.userPwd };  
  248.         String[] confirmPassword = new String[] { "214598", "123456", "123456",  
  249.                 "    ", "", "214", EtcIO.userPwd, EtcIO.userPwd, EtcIO.userPwd,  
  250.                 EtcIO.userPwd, "(!@#$%^&*)(\"\"\"\'\'\'\"\'", EtcIO.userPwd,  
  251.                 "a1234567890123456789012345678901234567890", EtcIO.userPwd };  
  252.         String[] expect = new String[] { "changed successfully!",  
  253.                 "changed successfully!", "Please input the old password!",  
  254.                 "Invalid password.", "Invalid password.", "Invalid password.",  
  255.                 "changed successfully!", "The old password is incorrect!",  
  256.                 "The old password is incorrect!",  
  257.                 "The old password is incorrect!", "changed successfully!",  
  258.                 "changed successfully!", "changed successfully!",  
  259.                 "changed successfully!" };  
  260.         String checkPath = "//span[@role='presentation']";  
  261.         Start2End.Login(this.baseUrl);  
  262.         // ChangePwdTest001_014  
  263.         for (int i = 0; i < oldPassword.length; i++) {  
  264.             try {  
  265.                 // Cancel  
  266.                 if (Start2End.IsElementPresent(By.xpath(ICwmXpath.xcmsChange))) {  
  267.                     DriverUtil.driver.findElement(  
  268.                             By.xpath(ICwmXpath.xcmsChange)).click();  
  269.                     Start2End.Sleep(1000);  
  270.                 } else {  
  271.                     continue;  
  272.                 }  
  273.                 DriverUtil.driver.findElement(By.name(ICwmXpath.xnameOldPwd))  
  274.                         .clear();  
  275.                 DriverUtil.driver.findElement(By.name(ICwmXpath.xnameOldPwd))  
  276.                         .sendKeys(oldPassword[i]);  
  277.                 Start2End.Sleep(1000);  
  278.                 DriverUtil.driver.findElement(By.name(ICwmXpath.xnameNewPwd))  
  279.                         .clear();  
  280.                 DriverUtil.driver.findElement(By.name(ICwmXpath.xnameNewPwd))  
  281.                         .sendKeys(newPassword[i]);  
  282.                 Start2End.Sleep(1000);  
  283.                 DriverUtil.driver.findElement(  
  284.                         By.name(ICwmXpath.xnameConfirmPwd)).clear();  
  285.                 DriverUtil.driver.findElement(  
  286.                         By.name(ICwmXpath.xnameConfirmPwd)).sendKeys(  
  287.                         confirmPassword[i]);  
  288.                 Start2End.Sleep(1000);  
  289.                 DriverUtil.driver.findElement(  
  290.                         By.xpath(ICwmXpath.xcmsChangeCancel)).click();  
  291.                 Start2End.Sleep(1000);  
  292.                 String temp = "//span[text()='Information - CUBRID Database Server']";  
  293.                 boolean flag = Start2End.IsElementPresent(By.xpath(temp));  
  294.                 if (flag) {  
  295.                     Start2End.InsertHtml("ChangePassword",  
  296.                             "Login and start to change cms-user's password.(oldPassword:"  
  297.                                     + oldPassword[i] + "&newPassword:"  
  298.                                     + newPassword[i] + "&confirmPassword:"  
  299.                                     + confirmPassword[i]  
  300.                                     + "). Then click Cancel-button.", temp,  
  301.                             "Have found successfully!", EtcIO.PassTag);  
  302.                 } else {  
  303.                     Start2End.InsertHtml("ChangePassword",  
  304.                             "Login and start to change cms-user's password.(oldPassword:"  
  305.                                     + oldPassword[i] + "&newPassword:"  
  306.                                     + newPassword[i] + "&confirmPassword:"  
  307.                                     + confirmPassword[i]  
  308.                                     + "). Then click Cancel-button.", temp,  
  309.                             "Have not found!", EtcIO.FailTag);  
  310.                     DriverUtil.html.ScreenCapture(DriverUtil.driver);  
  311.                 }  
  312.   
  313.                 // OK  
  314.                 if (Start2End.IsElementPresent(By.xpath(ICwmXpath.xcmsChange))) {  
  315.                     DriverUtil.driver.findElement(  
  316.                             By.xpath(ICwmXpath.xcmsChange)).click();  
  317.                     Start2End.Sleep(1000);  
  318.                 } else {  
  319.                     continue;  
  320.                 }  
  321.                 DriverUtil.driver.findElement(By.name(ICwmXpath.xnameOldPwd))  
  322.                         .clear();  
  323.                 DriverUtil.driver.findElement(By.name(ICwmXpath.xnameOldPwd))  
  324.                         .sendKeys(oldPassword[i]);  
  325.                 Start2End.Sleep(1000);  
  326.                 DriverUtil.driver.findElement(By.name(ICwmXpath.xnameNewPwd))  
  327.                         .clear();  
  328.                 DriverUtil.driver.findElement(By.name(ICwmXpath.xnameNewPwd))  
  329.                         .sendKeys(newPassword[i]);  
  330.                 Start2End.Sleep(1000);  
  331.                 DriverUtil.driver.findElement(  
  332.                         By.name(ICwmXpath.xnameConfirmPwd)).clear();  
  333.                 DriverUtil.driver.findElement(  
  334.                         By.name(ICwmXpath.xnameConfirmPwd)).sendKeys(  
  335.                         confirmPassword[i]);  
  336.                 Start2End.Sleep(1000);  
  337.                 // Start2End.ClickOK(DriverUtil.driver);  
  338.                 DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsChangeOk))  
  339.                         .click();  
  340.                 Start2End.Sleep(1000);  
  341.                 temp = DriverUtil.driver.findElement(By.xpath(checkPath))  
  342.                         .getText();  
  343.                 flag = temp.trim().contains(expect[i]);  
  344.                 if (flag) {  
  345.                     Start2End.InsertHtml("ChangePassword",  
  346.                             "Login and start to change cms-user's password.(oldPassword:"  
  347.                                     + oldPassword[i] + "&newPassword:"  
  348.                                     + newPassword[i] + "&confirmPassword:"  
  349.                                     + confirmPassword[i]  
  350.                                     + "). Then click OK-button.", expect[i],  
  351.                             temp, EtcIO.PassTag);  
  352.                 } else {  
  353.                     Start2End.InsertHtml("ChangePassword",  
  354.                             "Login and start to change cms-user's password.(oldPassword:"  
  355.                                     + oldPassword[i] + "&newPassword:"  
  356.                                     + newPassword[i] + "&confirmPassword:"  
  357.                                     + confirmPassword[i]  
  358.                                     + "). Then click OK-button.", expect[i],  
  359.                             temp, EtcIO.FailTag);  
  360.                     DriverUtil.html.ScreenCapture(DriverUtil.driver);  
  361.                 }  
  362.             } catch (Exception e) {  
  363.                 Start2End.InsertHtml("ChangePassword",  
  364.                         "Login and start to change cms-user's password.(oldPassword:"  
  365.                                 + oldPassword[i] + "&newPassword:"  
  366.                                 + newPassword[i] + "&confirmPassword:"  
  367.                                 + confirmPassword[i] + ").", expect[i],  
  368.                         e.getMessage(), EtcIO.ErrorTag);  
  369.                 DriverUtil.html.ScreenCapture(DriverUtil.driver);  
  370.             }  
  371.             // DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsChangeOk)).click();  
  372.             Start2End.ClickOK(DriverUtil.driver);  
  373.             Start2End.Sleep(1000);  
  374.             try {  
  375.                 DriverUtil.driver.findElement(  
  376.                         By.xpath(ICwmXpath.xcmsChangeCancel)).click();  
  377.                 Start2End.Sleep(1000);  
  378.             } catch (Exception ex) {  
  379.             }  
  380.         }  
  381.         Start2End.Logout(this.baseUrl);  
  382.     }  
  383. }  


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

Start2End类:

  1. import org.openqa.selenium.By;  
  2. import org.openqa.selenium.WebDriver;  
  3.   
  4. import junit.framework.Test;  
  5. import junit.framework.TestCase;  
  6. import junit.framework.TestSuite;  
  7.   
  8. import com.nhn.platform.qa.cwmtest.Utils.DriverUtil;  
  9. import com.nhn.platform.qa.cwmtest.Utils.EtcIO;  
  10.   
  11. public class Start2End extends TestCase {  
  12.     public Start2End(String name) {  
  13.         super(name);  
  14.     }  
  15.   
  16.     // @Test  
  17.     public void EndJUnitTest() throws Exception {  
  18.         DriverUtil.html.CompleteCount();  
  19.         DriverUtil.StopService();  
  20.         System.out.println("Complete to clear ENV!");  
  21.     }  
  22.   
  23.     public static Test suite() {  
  24.         TestSuite suite = new TestSuite("Test for Start2End");  
  25.         // $JUnit-BEGIN$  
  26.         suite.addTest(new Start2End("EndJUnitTest"));  
  27.         // $JUnit-END$  
  28.         return suite;  
  29.     }  
  30.     public static void ClickYes(WebDriver driver){  
  31.         try {  
  32.             driver.findElement(By.xpath(ICwmXpath.xcmsPopYes))  
  33.                     .click();  
  34.         } catch (Exception e) {  
  35.             // TODO Auto-generated catch block  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39.     public static void ClickOK(WebDriver driver){  
  40.         try {  
  41.             driver.findElement(By.xpath(ICwmXpath.xcmsPopOk))  
  42.                     .click();  
  43.         } catch (Exception e) {  
  44.             // TODO Auto-generated catch block  
  45.             e.printStackTrace();  
  46.         }  
  47.     }  
  48.     public static void Login(String baseUrl) {  
  49.         DriverUtil.driver.get(baseUrl);  
  50.         Sleep(3000);  
  51.         try {  
  52.             // 登录  
  53.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser)).clear();  
  54.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnameUser))  
  55.                     .sendKeys(EtcIO.userName);  
  56.             Sleep(1000);  
  57.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))  
  58.                     .clear();  
  59.             DriverUtil.driver.findElement(By.name(ICwmXpath.xnamePassword))  
  60.                     .sendKeys(EtcIO.userPwd);  
  61.             // SendKeys.SendWait("~");  
  62.             DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogin))  
  63.                     .click();  
  64.             Sleep(1000);  
  65.         } catch (Exception e) {  
  66.             // TODO Auto-generated catch block  
  67.             e.printStackTrace();  
  68.         }  
  69.     }  
  70.     public static void Logout(String baseUrl) {  
  71.         DriverUtil.driver.get(baseUrl);  
  72.         Sleep(3000);  
  73.         try {  
  74.             // 退出  
  75.             DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsLogout))  
  76.                     .click();  
  77.             Sleep(1000);  
  78.             DriverUtil.driver.findElement(By.xpath(ICwmXpath.xcmsPopYes))  
  79.                     .click();  
  80.         } catch (Exception e) {  
  81.             // TODO Auto-generated catch block  
  82.             e.printStackTrace();  
  83.         }  
  84.     }  
  85.     public static boolean IsElementPresent(By by) {  
  86.         try {  
  87.             DriverUtil.driver.findElement(by);  
  88.             return true;  
  89.         } catch (Exception e) {  
  90.             return false;  
  91.         }  
  92.     }  
  93.     public static void Sleep(long milsecond) {  
  94.         try {  
  95.             Thread.sleep(milsecond);  
  96.         } catch (InterruptedException e) {  
  97.             // TODO Auto-generated catch block  
  98.             e.printStackTrace();  
  99.         }  
  100.     }  
  101.     public static void InsertHtml(String taskname,String steps,String expects,String results,String resultTag){  
  102.         DriverUtil.html.InsertHtml(taskname,  
  103.                 "In order to check the "+taskname+"-function is OK!",  
  104.                 resultTag, "none", "none", steps, expects,  
  105.                 results, "none");  
  106.     }  
  107. }  

 

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

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

AppTest类:

  1. package com.nhn.platform.qa.cwmtest.cwmautotest;  
  2.   
  3. import org.junit.runner.RunWith;   
  4. import org.junit.runners.Suite;  
  5.   
  6. import com.nhn.platform.qa.cwmtest.firefox.*;  
  7. //import com.nhn.platform.qa.cwmtest.chrome.*;  
  8. //import com.nhn.platform.qa.cwmtest.ie.*;  
  9.   
  10. @RunWith(Suite.class)  
  11.   
  12. @Suite.SuiteClasses({   
  13.     //SampleTest.class,  
  14.     HostTest.class,  
  15.     FrameTest.class,  
  16.     Start2End.class  
  17. })  
  18. public class AppTest{  
  19. }  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值