zoukankan      html  css  js  c++  java
  • Selenium WebDriver TestNg Maven Eclipse java 简单实例

    1. 环境准备
      前提条件Eclipse 已经安装过 TestNg ,Maven 插件
      新建一个普通的java项目
      点击右键 configure->convert to Maven Project
      这里写图片描述
      之后点击finish,项目转换后会多出来几个文件夹,和pom.xml
      这里写图片描述
      然后使用Pom.xml 替我们管理jar包,修改pom.xml文件,添加jar包的依赖,

      <dependencies>
          <dependency>
              <groupId>org.seleniumhq.selenium</groupId>
              <artifactId>selenium-java</artifactId>
              <version>LATEST</version>
              <scope>test</scope>
          </dependency>
      
          <dependency>
              <groupId>org.testng</groupId>
              <artifactId>testng</artifactId>
              <version>6.1.1</version>
              <scope>test</scope>
          </dependency>
      </dependencies>

      接下里会在项目下更新所需要的jar,这样就方便我们不需要一个一个添加jar
      这里写图片描述

      maven的jar包已经准备好了,接下来需要testNG上场
      需要新建一个testNg 的类,右键点击src新建一个包,之后在包内,新建一个testNg 类
      这里写图片描述

      这里写图片描述

      这里写图片描述

      打开testNg.xml 可以看到

      <?xml version="1.0" encoding="UTF-8"?>
      <suite name="Suite" parallel="false">
      <test name="Test">
      <classes>
        <class name="seleniumTest.NewTest"/>
      </classes>
      </test> <!-- Test -->
      </suite> <!-- Suite -->
      
      

      打开NewTest 显示:

      package seleniumTest;
      
      import org.testng.annotations.Test;
      
      public class NewTest {
        @Test
        public void f() {
        }
      }
    2. 代码实例

      接下来就是代码实现了,吧代码写在f() 这个方法里,之后右键点击testNg.xml ,通过testNg.xml 文件运行

    package seleniumTest;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.annotations.Test;
    
    public class NewTest {
      @Test
      public void f() {
    
                //如果火狐浏览器没有默认安装在C盘,需要制定其路径
                System.setProperty("webdriver.firefox.bin",
                                   "D:/Program Files    (x86)/MozillaFirefox/firefox.exe"); 
    
                //定义驱动对象为 FirefoxDriver 对象
                WebDriver driver = new FirefoxDriver();
    
    
                //驱动的网址
                driver.get("http://www.baidu.com/");
    
    
                //浏览器窗口变大
                driver.manage().window().maximize();
    
                //定位输入框元素
                WebElement txtbox = driver.findElement(By.name("wd"));
    
                //在输入框输入文本
                txtbox.sendKeys("HelloWorld");
    
                //定位按钮元素
                WebElement btn = driver.findElement(By.id("su"));
    
                //点击按钮
                btn.click();
    
    
                //关闭驱动
                driver.close();
    
      }
    }
    
    事在人为,功不唐捐
  • 相关阅读:
    BLAST
    用 python 实现各种排序算法(转)
    纠错工具之
    《生物序列分析》
    比对软件
    MySQL版本升级参考资料【转】
    解决mysql开启GTID主从同步出现1236错误问题【转】
    Linux系统打开core dump的配置【转】
    MySQL在线更改binlog格式
    关于MySQL 8.0的几个重点【转】
  • 原文地址:https://www.cnblogs.com/xinleishare/p/4570139.html
Copyright © 2011-2022 走看看