zoukankan      html  css  js  c++  java
  • 基于Selenium的SeLion自动化测试框架之二(代码实现)

    如何基于SeLion实现一个web的自动化测试过程呢?下面会通过代码详细实现。

    一:需要安装的软件:

      1)Java JDK 1.7

      2) Eclipse + testNg (关于在eclipse中配置testNg请参考testNg官网)

      3) Maven (可以单独下载安装,请参考maven官网进行安装并配置)

      4) Firefox 浏览器 version <= 32 (其他浏览器也可以,本文以firefox讲解)

    二:创建Sample工程,有两种方式实现。

      1)通过eclipse中直接创建Maven项目 (该过程简单不详细说)

      2)直接通过Maven命令创建 命令如下:(红色部分可以自己修改)

    mvn archetype:generate -DgroupId=com.mycompany.test -DartifactId=Sample 
      -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

    执行该命令后会生成如下的目录结构:

    Sample/
    ├── pom.xml
    ├── src/main/java/com/mycompany/test
    │                               ├── App.java
    ├── src/test/java/com/mycompany/test
    │                               ├── AppTest.java

    三:更新pom.xml文件,加入依赖包,代码如下:(关于pom.xml文件的语法请查看maven官网,有详细解释)

    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://maven.apache.org/POM/4.0.0"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mycompany.test</groupId>
      <artifactId>Sample</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>Sample</name>
      <url>http://maven.apache.org</url>
    
      <!-- you need this to pull SeLion SNAPSHOT artifacts from the global repo -->
      <repositories>
        <repository>
          <id>sonatype-nexus-snapshots</id>
          <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </repository>
      </repositories>
    
      <!-- you need this to pull SeLion SNAPSHOT plugin artifacts from the global repo -->
      <pluginRepositories>
        <pluginRepository>
          <id>sonatype-nexus-snapshots</id>
          <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </pluginRepository>
      </pluginRepositories>
    
      <dependencies>
        <dependency>
          <groupId>com.paypal.selion</groupId>
          <artifactId>SeLion</artifactId>
          <version>1.0.0-SNAPSHOT</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <configuration>
              <suiteXmlFiles>
                <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
              </suiteXmlFiles>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>

    更改完成后,执行下面的命令,maven就会自动下载所需要的依赖包到本地,第一次因为需要下载的包较多,会需要较长的时间,等第一次下载好了,以后运行就会快了

    mvn dependency:resolve

    四:创建@WebTest测试代码如下:

    package com.mycompany.test;
    
    import org.testng.annotations.Test;
    
    import com.paypal.selion.annotations.WebTest;
    import com.paypal.selion.platform.grid.Grid;
    import com.paypal.selion.platform.html.Button;
    import com.paypal.selion.platform.html.TextField;
    import com.paypal.selion.platform.utilities.WebDriverWaitUtils;
    import com.paypal.selion.reports.runtime.WebReporter;
    
    public class Demo {
        
           //testNg标记 
        @Test
         //SeLion标记 用于指示该测试是web测试
        @WebTest
        public void DemoTest1() throws InterruptedException {
            // 打开百度首页
            Grid.driver().get("http://www.baidu.com/");
    
            // 定义搜索框
            TextField field = new TextField("id=kw");
    
            // 等待网页夹在完成并且搜索框出现
            WebDriverWaitUtils.waitUntilElementIsPresent(field.getLocator());
    
            // 搜索selion关键字
            field.type("SeLion");
    
                   //点击 百度一下 按钮
            Button button = new Button("id=su");
            button.click();
                 //自动截图并输出到log中去
            WebReporter.log("Baidu Demo", true, true);
        }
    }

    通过上面测试百度首页的代码可以看到SeLion为我们封装了webdriver, TextField, Button, 等待对象出现,自动截图并且输入到log等功能。对于一个这样一个简单的baidu首页测试,如果我们完全自己封装实现这些功能,会用大量的代码去实现,由此可见在SeLion中,已经替我们完成了自动化测试需要用到的功能,我们只需要简单调用就能实现,大大提高我们开发代码的效率。

    五:创建testNg的xml文件,我们在{project}/src/test/resources路径下创建SampleSuite.xml的文件并加入下面代码:

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite thread-count="1" verbose="1" name="Selion Test"
           skipfailedinvocationcounts="false" junit="false" parallel="classes"
           data-provider-thread-count="1" annotations="JDK">
    
      <!-- optional (defaults to firefox). Firefox browser will be used for
        web test -->
      <parameter name="browser" value="*firefox"/>
    
    <!-- 该参数为true表示在本机运行,如果为false则需要指定server的地址和端口号--> <parameter name="runLocally" value="true"/> <test name="SampleTest"> <classes> <class name="com.mycompany.test.Demo"></class> </classes> </test> </suite>

    六:完成上面的过程后,我们可以直接该测试查看测试过程和结果:

      1) 直接在eclipse中右键用testNg执行该xml文件

      2) 使用如下Maven命令去执行

    mvn clean test -DsuiteXmlFile=src/test/resources/SampleSuite.xml

    执行后,我们就会看到启动firefox浏览器,打开百度首页输入selion关键字搜索,执行完成后,会自动生成test-output的文件夹,里面会看到一份带截图的漂亮的html统计report。到此一个完成的测试过程就完成了。

  • 相关阅读:
    用TextKit实现表情混排
    IOS类似9.png
    iphone 弹出键盘,文本框自动向上移动。
    IOS截取部分图片
    IOS给图片增加水印(图片、文字)
    iOS界面设计切图小结
    table tr th td thead tbody tfoot
    怎样让table没有边框
    margin、padding,border默认值
    Java 随机数
  • 原文地址:https://www.cnblogs.com/bccs/p/4542913.html
Copyright © 2011-2022 走看看