使用Maven进行Selenium测试自动化
今天,我想幫助您更好地管理自動GUI測試(Selenium)。 在過去,我已經看到人們處理此問題的許多不同方式。 有些人只是用Selenium-IDE編寫普通HTML TestCases,將其存儲在HDD上的某個位置,并在需要時手動運行。 其他人甚至不使用Selenium-IDE。 他們為Example編寫純Java,并使用JUnit自動執行。 我今天的解決方案介于兩者之間。
前提
- 我想要用Selenium-IDE創建的純HTML TestCases。 這樣一來,幾乎沒有編程技能的人仍然可以創建它們。
- 我希望這些GUI測試在構建過程中自動運行,因此我的CI工具可以在發生錯誤時通知我。
- 由于測試隨源一起增長,因此我還希望項目存儲庫中的Versioncontrol下的所有TestCases。
- 我希望付出最少的努力,取得最高的結果。 因此,我不想從HTML TestCases中導出JUnit測試,因為它可能是重復的,并且我想堅持DRY原則。
解
首先,我在我的項目中為Selenium-Tests創建一個文件夾。
資料夾結構
TestSuite示例
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><meta content="text/html; charset=UTF-8" http-equiv="content-type"/><title>Test Suite</title> </head> <body> <table id="suiteTable" cellpadding="1" cellspacing="1" border="1" class="selenium"><tbody><tr><td><b>Test Suite</b></td></tr><tr><td><a href="./SomeTest1.html">SomeTest1</a></td></tr><tr><td><a href="./SomeTest2.html">SomeTest2</a></td></tr></tbody> </table> </body> </html>示例測試
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head profile="http://selenium-ide.openqa.org/profiles/test-case"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><link rel="selenium.base" href=""/><title>SomeTest1.html</title> </head> <body> <table cellpadding="1" cellspacing="1" border="1"><thead><tr><td rowspan="1" colspan="3">SomeTest1</td></tr></thead><tbody><tr><td>open</td><td>/</td><td></td></tr><tr><td>waitForElementPresent</td><td>//div[@id='someId']</td><td></td></tr><tr><td>click</td><td>css=button.create</td><td></td></tr><!-- Some Steps --><tr><td>assertText</td><td>//div[@id='someId']</td><td>${expectedText}</td></tr></tbody> </table> </body> </html>設置Web服務器
所以我有我的TestSuite。 但是我該如何運行它們? 最重要的是,它應該在Maven Build Process中運行,因此它也可以在Jenkins-CI或其他任何版本上運行。 在針對實際運行的WebApp進行測試時,這是每個定義的IntegrationTest。 在Maven中,我們有機會在集成測試階段中運行此類測試。 如果您想了解有關Maven Build生命周期及其各個階段的更多信息,請查看this 。 因此,我們需要某種WebServer來運行我們的WebApp,否則測試將無法進行。 WebServer應該在集成測試階段之前啟動,然后再停止。 例如,我們可以使用Tomcat7或Jetty 。 在此示例中,我將使用tomcat7-maven-plugin。 我將pom.xml配置為啟動Tomcat7 pre-integration-test 。
<plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.0</version><executions><execution><id>tomcat-run</id><goals><goal>run-war-only</goal></goals><phase>pre-integration-test</phase><configuration><port>8080</port><fork>true</fork></configuration></execution><execution><id>tomcat-shutdown</id><goals><goal>shutdown</goal></goals><phase>post-integration-test</phase></execution></executions> </plugin>如果在CI服務器上運行多個項目,則可以考慮為每個項目使用不同的端口號。
最后:運行測試
最后但并非最不重要的一點是,我們需要運行測試。 幸運的是,有可用的selenium-maven-plugin可以完成這項工作。
<plugin><groupId>org.codehaus.mojo</groupId><artifactId>selenium-maven-plugin</artifactId><version>2.3</version><configuration><browser>*firefox</browser><suite>src/test/selenium/TestSuite.html</suite><startURL>http://localhost:8080</startURL></configuration><executions><execution><id>run-selenium-tests</id><phase>integration-test</phase><goals><goal>selenese</goal></goals></execution></executions> </plugin>現在,每當我們在控制臺中執行mvn clean verify或什至mvn clean install時,都會運行測試并將報告存儲在目標目錄中。 這也將由您的CI工具完成。
結論
我們確實有完整干凈的安裝程序。
- 我們有一個地方可以存儲我們的測試,
- 它們在Sourcecode和Version控件內
- 它們可以由CI-Tools自動運行
- 甚至非開發人員也可以添加新的TestCases
順便說一句:如果某些事情沒有按預期進行,請不要放棄。 Selenium似乎有點小問題,有時您必須掏點錢才能解決問題。 但它確實有效,我想通了。
翻譯自: https://www.javacodegeeks.com/2013/07/selenium-test-automation-with-maven.html
總結
以上是生活随笔為你收集整理的使用Maven进行Selenium测试自动化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ddos拦截(ddos限速)
- 下一篇: weblogic jmx_在WebLog