汉滨区住房和城乡建设局网站,网站开发费 会计科目,莱芜杂谈 莱芜话题,网站开发工具是什么今天#xff0c;我想帮助您更好地管理自动GUI测试#xff08;Selenium#xff09;。 在过去#xff0c;我已经看到人们处理此问题的许多不同方式。 有些人只是用Selenium-IDE编写普通HTML TestCases#xff0c;将其存储在HDD上的某个位置#xff0c;并在需要时手动运行。… 今天我想帮助您更好地管理自动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 version1.0 encodingUTF-8?
!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
html xmlnshttp://www.w3.org/1999/xhtml xml:langen langen
headmeta contenttext/html; charsetUTF-8 http-equivcontent-type/titleTest Suite/title
/head
body
table idsuiteTable cellpadding1 cellspacing1 border1 classseleniumtbodytrtdbTest Suite/b/td/trtrtda href./SomeTest1.htmlSomeTest1/a/td/trtrtda href./SomeTest2.htmlSomeTest2/a/td/tr/tbody
/table
/body
/html 示例测试 ?xml version1.0 encodingUTF-8?
!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
html xmlnshttp://www.w3.org/1999/xhtml xml:langen langen
head profilehttp://selenium-ide.openqa.org/profiles/test-casemeta http-equivContent-Type contenttext/html; charsetUTF-8/link relselenium.base href/titleSomeTest1.html/title
/head
body
table cellpadding1 cellspacing1 border1theadtrtd rowspan1 colspan3SomeTest1/td/tr/theadtbodytrtdopen/tdtd//tdtd/td/trtrtdwaitForElementPresent/tdtd//div[idsomeId]/tdtd/td/trtrtdclick/tdtdcssbutton.create/tdtd/td/tr!-- Some Steps --trtdassertText/tdtd//div[idsomeId]/tdtd${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 。 plugingroupIdorg.apache.tomcat.maven/groupIdartifactIdtomcat7-maven-plugin/artifactIdversion2.0/versionexecutionsexecutionidtomcat-run/idgoalsgoalrun-war-only/goal/goalsphasepre-integration-test/phaseconfigurationport8080/portforktrue/fork/configuration/executionexecutionidtomcat-shutdown/idgoalsgoalshutdown/goal/goalsphasepost-integration-test/phase/execution/executions
/plugin 如果在CI服务器上运行多个项目则可以考虑为每个项目使用不同的端口号。 最后运行测试 最后但并非最不重要的一点是我们需要运行测试。 幸运的是有可用的selenium-maven-plugin可以完成这项工作。 plugingroupIdorg.codehaus.mojo/groupIdartifactIdselenium-maven-plugin/artifactIdversion2.3/versionconfigurationbrowser*firefox/browsersuitesrc/test/selenium/TestSuite.html/suitestartURLhttp://localhost:8080/startURL/configurationexecutionsexecutionidrun-selenium-tests/idphaseintegration-test/phasegoalsgoalselenese/goal/goals/execution/executions
/plugin 现在每当我们在控制台中执行mvn clean verify或什至mvn clean install时都会运行测试并将报告存储在目标目录中。 这也将由您的CI工具完成。 结论 我们确实有完整干净的安装程序。 我们有一个地方可以存储我们的测试 它们在Sourcecode和Version控件内 它们可以由CI-Tools自动运行 甚至非开发人员也可以添加新的TestCases 顺便说一句如果某些事情没有按预期进行请不要放弃。 Selenium似乎有点小问题有时您必须掏点钱才能解决问题。 但它确实有效我想通了。 参考 Be Be a Better Developer博客上的JCG合作伙伴 Gregor Riegler 提供的MavenSelenium测试自动化 。 翻译自: https://www.javacodegeeks.com/2013/07/selenium-test-automation-with-maven.html