java web 多语言_基于 Selenium WebDriver 实现多语言环境下自动化截图
到此,一個 Junit4 test case 就在 Eclipse 中建成 , 如代碼清單 1.
清單 1. 將腳本導入 Eclipse
package com.example.casePackage;??import static org.junit.Assert.fail ;??import java.util.concurrent.TimeUnit;??import org.junit.After;??import org.junit.Before;??import org.junit.Test;??import org.openqa.selenium.By;??import org.openqa.selenium.JavascriptExecutor;??import org.openqa.selenium.NoSuchElementException;??import org.openqa.selenium.WebDriver;??import org.openqa.selenium.WebElement;??import org.openqa.selenium.support.ui.Select;??import com.example.util.TVTUtility;??public class TestCase{? ?? ?? ?private WebDriver driver;? ?? ?? ?private String baseUrl;? ?? ?? ?/*? ?? ?? ? * This is the directory to save the screen captures, if you have finished a? ?? ?? ? * language executing, please change the directory to save another? ?? ?? ? * languages screen captures? ?? ?? ? */? ?? ?? ?public static final String DIRECTORY = "c:\\ScreenCapture\\jp\\";? ?? ?? ?// the pictures's format? ?? ?? ?public static final String FORMAT = ".gif";? ?? ?? ?private StringBuffer verificationErrors = new StringBuffer();? ?? ?? ?@Before? ?? ?? ?public void setUp() throws Exception {? ?? ?? ?? ?? ???driver = TVTUtility.openPreferenceFirefox ("en");? ?? ?? ?? ?? ???baseUrl = "https://9.115.46.97:16311";? ?? ?? ?? ?? ???driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS );? ?? ?? ?? ?? ???driver.manage().window().maximize();? ?? ?? ?? ?? ?? ?? ?? ???}? ?? ?? ???@Test? ?? ?? ???public void testCase_01010010() throws Exception {? ?? ?? ?? ?? ???System.out .println("case1 start");? ?? ?? ?? ?? ???driver.get(baseUrl + "/ibm/console/logon.jsp");? ?? ?? ?? ?? ???driver.findElement(By.id ("j_username")).clear();? ?? ?? ?? ?? ???driver.findElement(By.id ("j_username")).sendKeys("admin");? ?? ?? ?? ?? ???driver.findElement(By.id ("j_password")).clear();? ?? ?? ?? ?? ???driver.findElement(By.id ("j_password")).sendKeys("password");? ?? ?? ?? ?? ???driver.findElement(By.id ("other")).click();? ?? ?? ?? ?? ???/*? ?? ?? ?? ?? ?? ?* judge whether the user has login in the system or not, if the user? ?? ?? ?? ?? ???* has already login in, then click OK button to log-out? ?? ?? ?? ?? ?? ?* the pre-user,? ?? ?? ?? ?? ???* if there were no user has login the system, just ignore this step? ?? ?? ?? ?? ???*/? ?? ?? ?? ?? ???if (isElementPresent(By.name ("submit"))) {? ?? ?? ?? ?? ???driver.findElement(By.name ("submit")).click();? ?? ?? ?? ?? ?? ?}? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???driver.switchTo().frame("ISCNavigation");? ?? ?? ?? ?? ???driver.findElement(By.id ("2-nav-tree-expandable-item")).click();? ?? ?? ?? ?? ???driver.findElement(By.id ("3-nav-tree-expandable-item")).click();? ?? ?? ?? ?? ???driver.findElement(By.id ("? ?? ?? ?? ?? ? nav-tree-item-com.ibm.license.mgmt.ui.reports.ManageReports.node"\? ?? ?? ?? ?? ? )).click();? ?? ?? ?? ?? ? Thread.sleep (1000);? ?? ?? ?? ?? ???driver.switchTo().defaultContent();? ?? ?? ?? ?? ???driver.switchTo().frame("ISCWork");? ?? ?? ?? ?? ???driver.findElement(By.className ("btnm1")).click();? ?? ?? ?? ?? ???driver.findElement(By.className ("pop5")).click();? ?? ?? ?? ?? ???Thread.sleep (1000);? ?? ?? ?? ?? ???driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td/div/? ?? ?? ?? ?? ? table/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr/td/form/span/? ?? ?? ?? ?? ? table/tbody/tr[3]/td/table[2]/tbody/tr/td/table/tbody/tr[3]/td/table/? ?? ?? ?? ?? ? tbody/tr[3]/td[1]/table/tbody/tr/td/table/tbody/tr/td/table/tbody/? ?? ?? ?? ?? ? tr/td[4]/a/img")).click();? ? ? ?? ?? ?? ?? ?? ???Select m1= new Select(driver.findElement(By.id(TVTUtility.byDom(? ?? ?? ?? ?? ? driver,"document.getElementsByTagName('select')[0].id"))));? ?? ?? ?? ?? ? m1.selectByIndex(9);? ?? ?? ?? ?? ? // select the day? ?? ?? ?? ?? ?Select y1= new Select(driver.findElement(By.id(TVTUtility.byDom(? ?? ?? ?? ?? ?driver,"document.getElementsByTagName('select')[1].id"))));? ?? ?? ?? ?? ? y1.selectByValue("2013");? ?? ?? ?? ?? ? driver.findElement(By.linkText("9")).click();? ? ? ? ? ? ? ? ? ? ? ?? ?? ?? ?? ?? ???driver.findElement(? ?? ?? ?? ?? ? By.xpath("html/body/div[3]/div/table/tbody/tr[3]/td/input[1]"\? ?? ?? ?? ?? ? )).click();? ?? ?? ?? ?? ?driver.findElement(By.xpath("html/body/div[1]/table/tbody/tr/td/\? ?? ?? ?? ?? ?div/table/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr/td/form/\? ?? ?? ?? ?? ?span/table/tbody/tr[3]/td/table[2]/tbody/tr/td/table/tbody/tr[3]/\? ?? ?? ?? ?? ?td/table/tbody/tr[5]/td/table/? ?? ?? ?? ?? ? tbody/tr/td[1]/input")).click();? ?? ?? ?? ?? ? Thread.sleep(1000);? ?? ?? ?? ?? ? TVTUtility.captureScreen(driver, DIRECTORY,\? ?? ?? ?? ?? ?? ???TVTUtility.getMethodName(),FORMAT);? ?? ?? ?? ?? ? System.out.println("case1 finished");? ? ? ?? ?? ?? ? }? ?? ???@After? ?? ???public void tearDown() throws Exception {? ?? ?? ?? ?? ? driver.quit();? ?? ?? ?? ?? ? String verificationErrorString = verificationErrors.toString();? ?? ?? ?? ?? ? if (!"".equals(verificationErrorString)) {? ?? ?? ?? ?? ?? ?? ?fail(verificationErrorString);? ?? ?? ?? ?? ? }? ?? ???}}
[size=1em]清單 1 是一個完整的 Case 腳本,每個腳本從 Selenium IDE 中導出來都會由三個方法組成,setup(),tearDown() 和 testXxx()。setup() 在 testXxx() 前執行,tearDown() 在 testXxx() 之后執行。setup() 主要是用來初始化一些通用參數,比如 WebDriver 的構建和某些參數的初始化工作,或者系統的登陸。tearDown() 主要是來關閉瀏覽器等后續動作 ,Case 的主體在 testXxx() 中完成。
[size=1em]由于每個 Case 的主要步驟都是在 testXxx() 方法中完成,而 setup() 和 tearDown() 方法是一些通用的步驟,所以在寫腳本的時候,不用為每一個導出的腳本都新建一個 Java 類,只需要將腳本中的 testXxx() 方法拷貝到已經建好的類中,并重用 setup() 和 tearDown() 方法。這樣就可以在一個類中添加多個 testXxx() 方法,每個方法對應一個 Case 腳本。
[size=1em]其中 testXxx() 方法命名最好是有規律和規范的,比如 testCase_01010010(), testCase_01010020()..., 這樣在保存截圖的時候就可以使用方法名或者方法名的一部分作為圖片名,處理起來也比較簡單。
[size=1em]在 testXxx() 方法中,有很多如 driver.findElement(By.id ("2-nav-tree-expandable-item")).click(); driver.findElement(By.xpath ("xxxxxxxxxxxxxxxxxxxxxxx")).click(); 的語句,這些語句就是用來查找頁面元素的,在用 Selenium WebDriver 的時候,主要挑戰就是查找元素,所以了解更多的 WebDriver API 才能更順利的去實現 Case 的編寫。前面已經討論過,在 TVT 中,重復工作在于截圖,所以接下來就要解決截圖問題。Selenium WebDriver 已經提供了截圖方法,我們只需在需要截圖的代碼位置,嵌入截圖語句。以下方法是將截圖功能封裝在一個 TVTUtility 的工具類中,如代碼清單 2.
清單 2. 截圖方法
public static void captureScreen(WebDriver dr, String directory,String captureName,? ?? ?? ?? ???String format)? ?? ?File screenShotFile = ((TakesScreenshot) dr).getScreenshotAs(OutputType.FILE );? ?? ? try {? ?? ?? ?FileUtils.copyFile (screenShotFile, new File(directory+ captureName + \? ?? ?? ? format));? ?? ?? ?? ? } catch (IOException e) {? ?? ?? ???e.printStackTrace();? ?? ?? ?? ? }? ?? ?? ???}
[size=1em]在需要截圖的位置,嵌入 TVTUtility.captureScreen(driver, DIRECTORY, TVTUtility.getMethodName(),FORMAT) 語句,就能完成截圖并保存到指定的位置。這里需要注意語句的執行速度一般比 Firefox 的響應速度快,所以有必要在截圖之前顯示等待一段時間以便頁面的完全加載,如 Thread.sleep(1000), 具體的時間要看實際情況而定。
[size=1em]captureScreen() 方法中參數 driver 是一個 Selenium WebDriver 對象,DIRECTORY 是保存圖片的位置,TVTUtility.getMethodName() 參數的作用是獲取當前的方法名,前面我們討論過,這個方法的命名最好是有規律和規范的,這樣就可以截取其一部分來作為圖片保存的名稱,比如截取成 01010010,01010020...,FORMAT 參數為圖片的格式,如 jpg,gif 等。
總結
以上是生活随笔為你收集整理的java web 多语言_基于 Selenium WebDriver 实现多语言环境下自动化截图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# xml文件读取与修改
- 下一篇: 基于NextCloud,挂载Aria2+