zoukankan      html  css  js  c++  java
  • TestNG-官网文档-之selenium

    安装:

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

    New Remote Site

    Name:TestNG

    URL: http://beust.com/eclipse

     

     目录

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

    2.怎么配置你的测试

    3.为TestNG创建XML文件

    4.使用Eclipse开始你的测试

    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("");
      //...
    }
    }
    复制代码
  • 相关阅读:
    PVS 7.6 部署教程
    PHP下载远程图片的3个方法
    [Xcode 实际操作]二、视图与手势-(2)UIView视图的层次关系
    [Swift]检查API可用性
    [Xcode 实际操作]二、视图与手势-(1)UIView视图的基本使用
    [Swift]LeetCode103. 二叉树的锯齿形层次遍历 | Binary Tree Zigzag Level Order Traversal
    [Swift]forEach详解
    [Swift]LeetCode937. 重新排列日志文件 | Reorder Log Files
    [Swift]LeetCode940. 不同的子序列 II | Distinct Subsequences II
    [Swift]LeetCode939. 最小面积矩形 | Minimum Area Rectangle
  • 原文地址:https://www.cnblogs.com/hua-an/p/5018704.html
Copyright © 2011-2022 走看看