一:TestNG在Eclipse中的安装
(1)点击eclipse中的Help->Install New Software(2)点击【Add】按钮,输入相应的地址(3)勾选加载出来的TestNG选项,点击【Install】这样就完成了testng在eclipse的安装二:TestNG在Eclipse中的配置
(1)新建一个项目,选择项目名称点击右键,选择Build Path->【Add Libraties】,添加TestNG (2)新建一个TestNg Class,并且配置testng.xml文件三:添加并运行selenium
(1)添加selenium相应的jar包(前面文章已经介绍)(2)把selenium运行的代码添加到TestNG Class中比如:package Testng_findElement;import java.util.Iterator;import java.util.List;import java.util.Set;import javax.swing.text.AbstractDocument.Content;import org.junit.Assert;import org.openqa.selenium.By;import org.openqa.selenium.Keys;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.*;import org.openqa.selenium.interactions.Actions;import org.testng.annotations.AfterTest;import org.testng.annotations.BeforeTest;import org.testng.annotations.Test;import TestHelloWorld.Constant;import TestHelloWorld.DriverUtils;public class Testng_exp { WebDriver driver; @BeforeTest public void pre() { System.setProperty("webdriver.firefox.bin", "E:/Program Files/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); } @Test public void Basic_by() { driver.get("https://www.jd.com"); driver.manage().window().maximize(); // by classname的用法 WebElement text = driver.findElement(By.className("text")); text.sendKeys("连衣裙"); Actions builder = new Actions(driver); builder.sendKeys(Keys.ENTER).perform(); // by id的用法 driver.findElement(By.id("ttbar-myjd")).click(); WebDriver window = DriverUtils.getWantDriver(driver, Constant.jd_login_title); // by name的用法 WebElement loginname = window.findElement(By.name("loginname")); loginname.sendKeys(Constant.name); WebElement nloginpwd = window.findElement(By.name("nloginpwd")); nloginpwd.sendKeys(Constant.pwd); window.findElement(By.id("loginsubmit")).click(); } @AfterTest public void later(){ driver.close(); }}
(3)让程序飞起来(运行testng.xml)