日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Selenium-WebDriver自学】Selenium测试设计技术(十三)

發(fā)布時(shí)間:2024/4/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Selenium-WebDriver自学】Selenium测试设计技术(十三) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?Selenium頁面對象模型

1、Selenium頁面對象模型

優(yōu)點(diǎn)
頁面的對象模型是其中測試對象和功能被彼此分開,從而保持代碼干凈的實(shí)現(xiàn)。
對象保持獨(dú)立的測試腳本。一個(gè)目的可以通過一個(gè)或多個(gè)測試腳本進(jìn)行訪問,因此,POM可以幫助我們創(chuàng)建對象一次和多次使用。
由于創(chuàng)建對象后,很容易訪問和易于更新一個(gè)對象的特定屬性。


POM流程圖

2、使用Excel數(shù)據(jù)驅(qū)動
在設(shè)計(jì)測試,參數(shù)化測試是不可避免的。我們會利用Apache的POI- Excel JAR實(shí)現(xiàn)是一樣的。它可以幫助我們來讀取和寫入到Excel中。
下載JAR
第1步:導(dǎo)航到URL- http://poi.apache.org/download.htmll并下載ZIP格式。

?

第2步:點(diǎn)擊鏡像鏈接下載JAR。

第3步:解壓縮到一個(gè)文件夾

第4步:如下所示的解壓縮后的內(nèi)容將被顯示。

第5步:現(xiàn)在創(chuàng)建一個(gè)新的項(xiàng)目,并在“External JARs”添加“POI-3.10.FINAL”文件夾中所有的jar包

第6步:現(xiàn)在,添加所有的“External JARs”在“OOXML-LIB”文件夾中。

第7步:現(xiàn)在,添加所有的“External JARs”在“l(fā)ib”文件夾中。

第8步:如下圖所示,顯示已添加的JAR文件。

第9步:如下圖所示的Package Explorer顯示。此外附加“webdriver”相關(guān)的JAR

實(shí)際使用
http://www.yiibai.com/selenium/selenium_parameterizing_using_excel.html

?3、log4j日志

http://www.yiibai.com/selenium/selenium_log4j_logging.html#article-start
讓我們來了解應(yīng)用程序運(yùn)行。
日志輸出可以保存,可以在以后進(jìn)行分析。
有助于調(diào)試,以防自動化測試失敗
也可用于審計(jì)目的看應(yīng)用的健康。
組件
1,Logger類的實(shí)例。
2,用于記錄該消息為以下之一日志級別的方法
error
warn
info
debug
log

第1步:從https://logging.apache.org/log4j/1.2/download.htmll下載log4j的JAR文件,并將下載JAR文件的解壓縮格式。

第2步:通過瀏覽到文件菜單中創(chuàng)建'New Java Project'。

第3步:輸入項(xiàng)目的名稱為“l(fā)og4j_demo”,然后單擊“Next”

第4步:單擊添加外部JAR,并添加“Log4j-1.2.17.jar”

第5步:單擊添加外部JAR,并添加Selenium webdriver的類庫。

第6步:單擊添加外部JAR,并添加Selenium webdriver的JAR文件的位于libs文件夾中。

第7步:使用它我們可以指定Log4j的屬性添加一個(gè)新的XML文件。

第8步:輸入日志文件的名稱為“l(fā)og4j.xml”。

第9步:下面的最終文件夾結(jié)構(gòu)如下所示。

第10步:現(xiàn)在增加Log4j 這將被記錄執(zhí)行過程中的性能。

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false"><appender name="fileAppender" class="org.apache.log4j.FileAppender"><param name="Threshold" value="INFO" /><param name="File" value="percent_calculator.log"/><layout class="org.apache.log4j.PatternLayout"><param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%c] (%t:%x) %m%n" /></layout></appender><root><level value="INFO"/><appender-ref ref="fileAppender"/></root> </log4j:configuration>

第11步:現(xiàn)在用于演示的目的,我們將結(jié)合log4j在相同的測試,我們已經(jīng)完成(百分比計(jì)算器)。添加一個(gè)類文件“Main”方法功能

執(zhí)行
在執(zhí)行日志文件的根文件夾中創(chuàng)建如下圖所示。在Eclipse中不能找出文件。應(yīng)該打開“Windows資源管理器”來顯示相同。

該文件的內(nèi)容如下所示。

4、異常處理

當(dāng)我們正在開發(fā)測試中,我們要確保,即使測試失敗的腳本可以繼續(xù)執(zhí)行。如果最壞的情況都處理不好意外的異常會被拋出。
如果發(fā)生異常,由于無法找到元素,或者預(yù)期的結(jié)果不與實(shí)際值相符,我們應(yīng)該抓住這個(gè)異常并結(jié)束測試的邏輯方式,以防腳本本身突然終止。
語法
實(shí)際的代碼應(yīng)該放在try塊和異常后的動作應(yīng)該放在catch塊。請注意:“finally'塊就算沒有問題,不管腳本是否已經(jīng)被拋出的異常都會執(zhí)行。

try { //Perform Action } catch(ExceptionType1 exp1) { //Catch block 1 } catch(ExceptionType2 exp2) { //Catch block 2 } catch(ExceptionType3 exp3) { //Catch block 3 } finally { //The finally block always executes. }

?

public static WebElement lnk_percent_calc(WebDriver driver)throws Exception {try{element = driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a"));return element;}catch (Exception e1){// Add a message to your Log File to capture the errorLogger.error("Link is not found.");// Take a screenshot which will be helpful for analysis.File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(screenshot, new File("D:frameworkscreenshots.jpg")); throw(e1);} }

5、多瀏覽器測試

用戶可以同時(shí)執(zhí)行多個(gè)瀏覽器中的腳本。
http://www.yiibai.com/selenium/selenium_multi_browser_testing.html

@Parameters("browser")@BeforeTestpublic void launchapp(String browser) { if (browser.equalsIgnoreCase("firefox")) {System.out.println(" Executing on FireFox");driver = new FirefoxDriver();driver.get(URL);driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);driver.manage().window().maximize(); } else if (browser.equalsIgnoreCase("chrome")) {System.out.println(" Executing on CHROME");System.out.println("Executing on IE");System.setProperty("webdriver.chrome.driver", "D:chromedriver.exe");driver = new ChromeDriver();driver.get(URL);driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);driver.manage().window().maximize(); } else if (browser.equalsIgnoreCase("ie")) {System.out.println("Executing on IE");System.setProperty("webdriver.ie.driver", "D:IEDriverServer.exe");driver = new InternetExplorerDriver();driver.get(URL);driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);driver.manage().window().maximize(); }else {throw new IllegalArgumentException("The Browser Type is Undefined");}}

創(chuàng)建一個(gè)XML這將有助于我們在參數(shù)設(shè)置瀏覽器的名字,不要忘記提及 parallel="tests"為了同時(shí)在所有瀏覽器中執(zhí)行。

通過對XML文件進(jìn)行右鍵點(diǎn)擊執(zhí)行腳本,然后選擇 'Run As' >> 'TestNG' 方式,如下圖所示。

輸出
所有的瀏覽器將平行展開,結(jié)果將被打印在控制臺上。
注:對于我們在IE瀏覽器執(zhí)行成功確保復(fù)選框“啟用保護(hù)模式”下的“IE選項(xiàng)中的安全選項(xiàng)卡中選中或未在所有區(qū)域中未檢查。

TestNG的結(jié)果以HTML格式來查看詳細(xì)的分析。

6、捕捉屏幕截圖
截圖捕獲功能可以幫助我們在需要在運(yùn)行時(shí)抓取截圖,在特別是當(dāng)故障發(fā)生。隨著截圖的幫助和日志信息,我們將能夠更好地分析結(jié)果
截圖是本地執(zhí)行和Selenium 網(wǎng)格(遠(yuǎn)程)處決配置不同。讓我們來看看他們每一個(gè)例子

本地主機(jī)執(zhí)行
我們將計(jì)算百分比之后的截圖。請確保給一個(gè)有效的路徑,用以保存屏幕截圖。

File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("D:screenshotsscreenshots1.jpg"));

輸出?

在執(zhí)行這個(gè)腳本,截圖保存在“D:screenshots”文件夾中名為'screenshots1.jpg“,如下圖所示。

Selenium網(wǎng)格- 捕捉屏幕截圖
當(dāng)Selenium網(wǎng)格工作,我們應(yīng)該確保從遠(yuǎn)程系統(tǒng)采取正確的截圖。我們將充分利用增強(qiáng)的驅(qū)動程序。
我們將連接到集線器Firefox的節(jié)點(diǎn)上執(zhí)行該腳本。更多關(guān)于配置集線器和節(jié)點(diǎn),請參閱Selenium網(wǎng)格章節(jié)。

package TestNG; import org.openqa.selenium.remote.Augmenter; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.TakesScreenshot; import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Parameters; import org.testng.annotations.Test; import java.io.File; import java.net.URL; import java.net.MalformedURLException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.remote.RemoteWebDriver; import java.io.IOException; public class TestNGClass { public WebDriver driver; public String URL, Node; protected ThreadLocal<RemoteWebDriver> threadDriver = null; @Parameters("browser") @BeforeTest public void launchapp(String browser) throws MalformedURLException { String URL = "http://www.calculator.net"; if (browser.equalsIgnoreCase("firefox")) { System.out.println(" Executing on FireFox"); String Node = "http://10.112.66.52:5555/wd/hub"; DesiredCapabilities cap = DesiredCapabilities.firefox(); cap.setBrowserName("firefox"); driver = new RemoteWebDriver(new URL(Node), cap); //Puts a Implicit wait, Will wait for 10 seconds before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Launch website driver.navigate().to(URL); driver.manage().window().maximize(); } else { throw new IllegalArgumentException("The Browser Type is Undefined"); } } @Test public void calculatepercent() throws IOException { driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Math Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Click on Percent Calculators // Make use of augmented Driver to capture Screenshots. WebDriver augmentedDriver = new Augmenter().augment(driver); File screenshot = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("D:screenshots emotescreenshot1.jpg")); // Please note - Screenshot would be saved on the system where the script is executed and NOT on remote machine. driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Enter value 50 in the second number of the percent Calculator driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Click Calculate Button String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); // Get the Result Text based on its xpath System.out.println(" The Result is " + result); //Print a Log In message to the screen if(result.equals("5")) { System.out.println(" The Result is Pass"); } else { System.out.println(" The Result is Fail"); } } @AfterTest public void closeBrowser() { driver.quit(); } }

輸出
當(dāng)執(zhí)行該腳本,截圖被捕獲并儲存在指定的位置,如下所示。

?

?

7、捕捉視頻
有時(shí)候我們未必能夠分析故障只需用日志文件或截圖的幫助。有時(shí)捕獲完整的執(zhí)行視頻幫助。讓我們了解如何捕捉視頻。
配置
第1步:導(dǎo)航到URL - http://www.randelshofer.ch/monte/index.htmll和下載屏幕記錄JAR,如下圖所示。

?

?第2步:下載后,添加JAR文件添加到當(dāng)前項(xiàng)目的庫。

?

第3步:我們會利用Java的AWT包來初始化顯卡配置。

GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();

第4步:它采用下列參數(shù)創(chuàng)建ScreenRecorder的一個(gè)實(shí)例。

示例
我們將捕獲簡單的測試執(zhí)行視頻 - 百分比計(jì)算。

import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.WebDriver; import org.openqa.selenium.By; import org.monte.media.math.Rational; import org.monte.media.Format; import org.monte.screenrecorder.ScreenRecorder; import static org.monte.media.AudioFormatKeys.*; import static org.monte.media.VideoFormatKeys.*; import java.awt.*; public class webdriverdemo { private static ScreenRecorder screenRecorder; public static void main(String[] args) throws IOException, AWTException { GraphicsConfiguration gconfig = GraphicsEnvironment .getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); screenRecorder = new ScreenRecorder(gconfig, new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI), new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, (int)24, FrameRateKey, Rational.valueOf(15), QualityKey, 1.0f, KeyFrameIntervalKey, (int) (15 * 60)), new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey,"black", FrameRateKey, Rational.valueOf(30)), null); WebDriver driver = new FirefoxDriver(); // Start Capturing the Video screenRecorder.start(); //Puts a Implicit wait, Will wait for 10 seconds before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Launch website driver.navigate().to("http://www.calculator.net/"); //Maximize the browser driver.manage().window().maximize(); // Click on Math Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Percent Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 50 in the second number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Click Calculate Button driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Get the Result Text based on its xpath String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("D:screenshotsscreenshots1.jpg")); //Print a Log In message to the screen System.out.println(" The Result is " + result); //Close the Browser. driver.close(); // Stop the ScreenRecorder screenRecorder.stop();

輸出
錄制的視頻保存在“C:users<<UserName>>Videos”文件夾,如下圖所示

C:\Users\Administrator\Videos

8、Selenium網(wǎng)格
http://www.yiibai.com/selenium/selenium_grids.html

?

轉(zhuǎn)載于:https://www.cnblogs.com/conquerorren/p/8252832.html

總結(jié)

以上是生活随笔為你收集整理的【Selenium-WebDriver自学】Selenium测试设计技术(十三)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久中文字幕无码 | 国产色a | 无码人妻精品一区二区三区9厂 | 精品国产1区 | 正在播放久久 | 亚洲国产午夜 | 亚洲av无码一区二区三区在线 | 日本大奶视频 | 超碰在线网站 | 国产午夜精品久久久久久久 | 91精品国产91久久久久久黑人 | 中文字幕自拍偷拍 | 欧美久久久一区二区三区 | 亚洲热在线视频 | 亚洲裸体视频 | 免费观看毛片 | 婷婷色激情| 快色视频在线观看 | 欧美激情一区二区三区免费观看 | 污污网站在线观看视频 | 黑人精品xxx一区一二区 | 国产精品久久久久久久午夜 | 黄色一级免费观看 | 国语对白做受按摩的注意事项 | 一区二区伦理 | 视频在线观看电影完整版高清免费 | 波多野结衣视频在线 | 欧美激情aaa | a√天堂资源 | 久久久久久久久久久国产 | 欧美在线播放视频 | 在线观看欧美视频 | 成人av一级 | 99爱视频在线 | 福利小视频在线 | 欧美另类国产 | 欧美成网| 国产精品高潮呻吟AV无码 | 色婷婷久久一区二区三区麻豆 | 97视频一区二区三区 | 天天操天天干视频 | 日韩字幕在线 | 亚洲精品1 | 成人在线精品 | 男女啪啪免费 | 欧美极品videos精品 | 国产精品久久久久久久毛片 | 激情在线观看视频 | 看黄色一级片 | 欧美xxxxx少妇 | 国产超碰av | 波多野结衣在线播放视频 | 少妇被又大又粗又爽毛片久久黑人 | 午夜少妇av | 国产小视频在线免费观看 | 黄色视屏在线播放 | 美女被捅个不停 | 日韩电影中文字幕在线观看 | 波多野吉衣视频在线观看 | 狠狠的色| 国产一区美女 | 日韩精品视频一区二区在线观看 | 亚洲喷水 | 日韩精品一区二区三区在线观看 | 日本欧美激情 | 超碰在线a | 日韩成人免费在线视频 | 搡老熟女老女人一区二区 | 玖玖在线资源 | 黄色日批 | 婷婷中文网| 老司机午夜影院 | 日本后进式猛烈xx00动态图 | 能在线看的av| 成人精品久久久午夜福利 | 97超碰国产精品无码蜜芽 | 国产性生活毛片 | 国产成人激情视频 | 成人一区二区三区四区 | 森林影视官网在线观看 | 嫩草一二三 | 一级免费观看视频 | 污污内射久久一区二区欧美日韩 | 国产日韩综合 | av影院在线 | 国产一区福利 | 久久久久久久久久久综合 | 岛国精品在线播放 | 精品999www| 午夜免费体验区 | 少妇av导航 | 久久久久99精品成人片试看 | 日本久色| 玖玖爱国产 | 国产白浆视频 | 亚洲av少妇一区二区在线观看 | 免费在线观看一区二区 | 亚洲综合五月 | www.国产视频.com |