java web 多语言_基于 Selenium WebDriver 实现多语言环境下自动化截图
到此,一個(gè) Junit4 test case 就在 Eclipse 中建成 , 如代碼清單 1.
清單 1. 將腳本導(dǎo)入 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 是一個(gè)完整的 Case 腳本,每個(gè)腳本從 Selenium IDE 中導(dǎo)出來(lái)都會(huì)由三個(gè)方法組成,setup(),tearDown() 和 testXxx()。setup() 在 testXxx() 前執(zhí)行,tearDown() 在 testXxx() 之后執(zhí)行。setup() 主要是用來(lái)初始化一些通用參數(shù),比如 WebDriver 的構(gòu)建和某些參數(shù)的初始化工作,或者系統(tǒng)的登陸。tearDown() 主要是來(lái)關(guān)閉瀏覽器等后續(xù)動(dòng)作 ,Case 的主體在 testXxx() 中完成。
[size=1em]由于每個(gè) Case 的主要步驟都是在 testXxx() 方法中完成,而 setup() 和 tearDown() 方法是一些通用的步驟,所以在寫(xiě)腳本的時(shí)候,不用為每一個(gè)導(dǎo)出的腳本都新建一個(gè) Java 類(lèi),只需要將腳本中的 testXxx() 方法拷貝到已經(jīng)建好的類(lèi)中,并重用 setup() 和 tearDown() 方法。這樣就可以在一個(gè)類(lèi)中添加多個(gè) testXxx() 方法,每個(gè)方法對(duì)應(yīng)一個(gè) Case 腳本。
[size=1em]其中 testXxx() 方法命名最好是有規(guī)律和規(guī)范的,比如 testCase_01010010(), testCase_01010020()..., 這樣在保存截圖的時(shí)候就可以使用方法名或者方法名的一部分作為圖片名,處理起來(lái)也比較簡(jiǎn)單。
[size=1em]在 testXxx() 方法中,有很多如 driver.findElement(By.id ("2-nav-tree-expandable-item")).click(); driver.findElement(By.xpath ("xxxxxxxxxxxxxxxxxxxxxxx")).click(); 的語(yǔ)句,這些語(yǔ)句就是用來(lái)查找頁(yè)面元素的,在用 Selenium WebDriver 的時(shí)候,主要挑戰(zhàn)就是查找元素,所以了解更多的 WebDriver API 才能更順利的去實(shí)現(xiàn) Case 的編寫(xiě)。前面已經(jīng)討論過(guò),在 TVT 中,重復(fù)工作在于截圖,所以接下來(lái)就要解決截圖問(wèn)題。Selenium WebDriver 已經(jīng)提供了截圖方法,我們只需在需要截圖的代碼位置,嵌入截圖語(yǔ)句。以下方法是將截圖功能封裝在一個(gè) TVTUtility 的工具類(lèi)中,如代碼清單 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) 語(yǔ)句,就能完成截圖并保存到指定的位置。這里需要注意語(yǔ)句的執(zhí)行速度一般比 Firefox 的響應(yīng)速度快,所以有必要在截圖之前顯示等待一段時(shí)間以便頁(yè)面的完全加載,如 Thread.sleep(1000), 具體的時(shí)間要看實(shí)際情況而定。
[size=1em]captureScreen() 方法中參數(shù) driver 是一個(gè) Selenium WebDriver 對(duì)象,DIRECTORY 是保存圖片的位置,TVTUtility.getMethodName() 參數(shù)的作用是獲取當(dāng)前的方法名,前面我們討論過(guò),這個(gè)方法的命名最好是有規(guī)律和規(guī)范的,這樣就可以截取其一部分來(lái)作為圖片保存的名稱,比如截取成 01010010,01010020...,FORMAT 參數(shù)為圖片的格式,如 jpg,gif 等。
總結(jié)
以上是生活随笔為你收集整理的java web 多语言_基于 Selenium WebDriver 实现多语言环境下自动化截图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C# xml文件读取与修改
- 下一篇: 基于NextCloud,挂载Aria2+