TestNG-官网文档-之selenium

安装:php

Eclipse:Help -> Software Updates -> Find and Install -> Search for new features to installhtml

New Remote Sitegit

Name:TestNGgithub

URL: http://beust.com/eclipse编程

 

 目录浏览器

1.如何利用参数使用TestNG的配置方法session

2.怎么配置你的测试框架

3.为TestNG建立XML文件eclipse

4.使用Eclipse开始你的测试wordpress

5.如何让将来的测试设计更好

 

/**开始**/

创建你的测试用例

在写测试用例以前,你须要知道如何验证,或者什么将被验证。让咱们用wordpress 的“create new post”写一个测试用例。

1.打开网页 http://demo.opensourcecms.com/wordpress/wp-login.php

2.在用户名处输入‘admin’

3.在密码处输入demo123

4.点击“log In”按钮

5.确认文字“Howdy,admin”存在

6.点击“Posts”连接

7.点击“Add New”按钮

8.在标题区域输入“Selenium Demo Post”

9.点击“Publish”按钮

10.确认文字“Post published”存在

考虑这个情景,想到的第一件事情是按照步骤建立一个很长的测试用例,对于手动测试,这多是一个不错的测试用例,然而,因为咱们写的是自动化用例,咱们想要咱们的脚本竟可能在将来的情景中能够重用。

如下是我如何分解这个测试:

1.打开workpress的站点

2.打开admin登录页

3.输入登录数据

4.引导去写post页面

5.写一个post

6.发布这个post

7.确认发布成功

记住这只是一个例子,你能够设计任何你想要的测试,只要他们又自身的商业价值并证实你的价值

让咱们来看看Java代码如何实现:

复制代码
 1 @Test(description="Launches the WordPress site")  2 public void launchSite(){  3 selenium.open("");  4 selenium.waitForPageToLoad("30000");  5 assertEquals(selenium.getTitle(), "Demo | Just another WordPress site");  6 }  7  8 @Test(description="Navigates to the admin page")  9 public void openAdminPage() { 10 selenium.open("wp-admin"); 11 selenium.waitForPageToLoad("30000"); 12 assertEquals(selenium.getTitle(), "Demo 鈥� Log In"); 13 } 14 15 @Test(description="Enters valid login data") 16 public void loginAsAdmin() { 17 selenium.type("user_login", "admin"); 18 selenium.type("user_pass", "demo123"); 19 selenium.click("wp-submit"); 20 selenium.waitForPageToLoad("30000"); 21 assertTrue(selenium.isTextPresent("Howdy, admin")); 22 } 23 24 @Test(description="Navigates to the New Post screen") 25 public void navigateNewPost() { 26 selenium.click("//a[contains(text(),'Posts')]/following::a[contains(text(),'Add New')][1]"); 27 selenium.waitForPageToLoad("30000"); 28 assertTrue(selenium.isTextPresent("Add New Post")); 29 } 30 31 @Test(description="Writes the new post") 32 public void writeBlogPost() { 33 selenium.type("title", "New Blog Post"); 34 selenium.click("edButtonHTML"); 35 selenium.type("content", "This is a new post"); 36 //TODO:Assert 37 } 38 39 @Test(description="Publishes the post") 40 public void publishBlogPost() { 41 selenium.click("submitdiv"); 42 selenium.click("publish"); 43 selenium.waitForPageToLoad("30000"); 44 assertTrue(selenium.isTextPresent("Post published.")); 45 } 46 47 @Test(description="Verifies the post") 48 public void verifyBlogPost() { 49 selenium.click("//a[contains(text(),'Posts') and contains(@class,'wp-first-item')]"); 50 selenium.waitForPageToLoad("30000"); 51 assertTrue(selenium.isElementPresent("//a[text()='New Blog Post']")); 52 } 53 54 @Test(description="Logs out") 55 public void logout() { 56 selenium.click("//a[text()='Log Out']"); 57 //TODO:Assert 58 }
复制代码

咱们将使用这些测试方法(或者测试步骤)

配置方法

若是你熟悉unit测试框架,你可能知道setup、及teardown方法。TestNG将超越这种想法,并容许你在你的测试用例集、测试组或者 测试方法以前及以后定义这类方法,这对于selenium测试时很是有利的,你能够在你的测试集以前建立selenium服务及打开浏览器。

为了作到这点,咱们将使用2个TestNG的注释符:@BeforeSuite 和 @AfterSuite:

复制代码
@BeforeSuite(alwaysRun = true) public void setupBeforeSuite(ITestContext context) { String seleniumHost = context.getCurrentXmlTest().getParameter("selenium.host"); String seleniumPort = context.getCurrentXmlTest().getParameter("selenium.port"); String seleniumBrowser = context.getCurrentXmlTest().getParameter("selenium.browser"); String seleniumUrl = context.getCurrentXmlTest().getParameter("selenium.url"); RemoteControlConfiguration rcc = new RemoteControlConfiguration(); rcc.setSingleWindow(true); rcc.setPort(Integer.parseInt(seleniumPort)); try { server = new SeleniumServer(false, rcc); server.boot(); } catch (Exception e) { throw new IllegalStateException("Can't start selenium server", e); } proc = new HttpCommandProcessor(seleniumHost, Integer.parseInt(seleniumPort), seleniumBrowser, seleniumUrl); selenium = new DefaultSelenium(proc); selenium.start(); } @AfterSuite(alwaysRun = true) public void setupAfterSuite() { selenium.stop(); server.stop(); }
复制代码

PS:你有没有注意到怪异的参数?他们被储存在XML文件中(咱们将在下一部分看到这个)

经过加入这些注释符,TestNG引擎将自动在测试先后唤起这些方法(请确保这些测试用例被@test 注释符标识),启动和初始化selenium内容只有一次,在测试中相同的浏览器session将被复用

建立XML文件

为了定义测试的顺序,咱们须要建立XML文件并列出咱们想要的运行测试方法的顺序。确保这些测试用例被@test 注释符标识,不然TestNG引擎将不会执行他们。

在TestNG5.13.1以前,你不得不用Method Interceptors 若是你在XML中定义想顺序执行用例,具体能够参加做者的例子http://gist.github.com/416310,从5.13.1后,你只须要 增长参数:preserve-order在你的测试标签,并包含你想要运行的用例,减小你测试集中没必要要的代码

如下是XML文件:

复制代码
<suite name="Knorrium.info - Wordpress Demo" verbose="10"> <parameter name="selenium.host" value="localhost" /> <parameter name="selenium.port" value="3737" /> <parameter name="selenium.browser" value="*firefox" /> <parameter name="selenium.url" value="http://demo.opensourcecms.com/wordpress/" /> <test name="Write new post" preserve-order="true"> <classes> <class name="test.Wordpress"> <methods> <include name="launchSite" /> <include name="openAdminPage" /> <include name="loginAsAdmin" /> <include name="navigateNewPost" /> <include name="writeBlogPost" /> <include name="publishBlogPost" /> <include name="verifyBlogPost" /> </methods> </class> </classes> </test> </suite>
复制代码

在Eclipse中启动你的测试

咱们完成了测试的编写后,怎么运行他们呢?

你能启动从命令行启动TestNg、使用Eclipse插件或程序化。咱们将演示使用Eclipse插件,按照步骤操做便可,具体可见TestNG的官方文档:http://testng.org/doc/download.html
若是你已经装了TestNG,你将会看到右击XML文件后有选项(RunAs-TestNG Suite)

点击“RunAs-TestNG Suite”就会开始你的测试,而且你将会看到漂亮的结果树

对将来的思考

若是你想对你的测试集将来思考的更多,我会推荐你阅读 Adam Goucher’s article 在PragPub上的,他谈论了selenium2 和 Page Objects模式(一种当你使用selenium2时很是好的模式)

因为还有不少人使用selenium1,因此这片文章还会存在,但最终会被selenium2替代

随着测试数量在测试集中的增加,你会发现他们在不一样的测试类分组是个好主意。若是你这样作,你能够利用面向对象的编程方式和例如建立一个新类命名BaseTest,并留下您的配置逻辑在那里。这样,每个测试类必须继承BaseTest类并使用静态属性。

复制代码
public class WordPressAdmin extends BaseTest { @Test public void test1(){ selenium.open(""); //... } @Test public void test2(){ selenium.open(""); //... } }
复制代码
相关文章
相关标签/搜索