笔记

1、windowshandle处理方式

import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
 * 
 * @author Kitty
 * 演示WindowsHandle的处理方式
 *
 */
public class WindowsHandleTest {
	public WebDriver driver;
	  public ChromeDriverService service;

	  @BeforeMethod
	  public void setUp() throws IOException {
	    service = new ChromeDriverService.Builder()
	        .usingDriverExecutable(new File("C:\\Windows\\System32\\chromedriver.exe"))
	        .usingAnyFreePort().build();
	    service.start();
	    driver = new ChromeDriver();
	    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	  }

	  @AfterMethod
	  public void close() throws InterruptedException {
	    // 关闭driver
	    driver.quit();
	    service.stop();
	  }

	  @Test
	  public void testHandle() {
		  //获取url
		  driver.get("http://ask.testfan.cn/");
		  //点击问题
		  driver.findElement(By.linkText("jmeter 二次开发")).click();
		  System.out.println("step1: 当前driver指向的windowHandle:  "+driver.getWindowHandle()+"  当前Tab页面标题:"+driver.getTitle());
		  //此处的Set是java.util.set, 导包需注意,通过driver获取页面所有的windowHandles,根据windowHandles可以实现Tab页面的跳转
	      Set<String> handles= driver.getWindowHandles();
	      //遍历所有handle,根据handle切换driver指向的页面
		   for(String handle:handles) {
			   //根据handle切换driver指向的页面
			   driver.switchTo().window(handle);
			   System.out.println("Step2: 当前driver指向的windowHandle:  "+driver.getWindowHandle()+"  当前Tab页面标题:"+driver.getTitle());
			   //切换完以后对比driver的title和你想要操作的页面title,如果是想要操作的title,则进行操作
			   //contains方法是包含的意思,判断title是否包含jmeter
			   if(driver.getTitle().contains("jmeter")) {
				   driver.findElement(By.id("follow-button")).click();
				   System.out.println("Step3: 当前driver指向的windowHandle:  "+driver.getWindowHandle()+"  当前Tab页面标题:"+driver.getTitle());
			   }
		   }
		  
	  }
}

2、Actions扩展driver实现鼠标悬停操作

public void testAction() throws InterruptedException {
driver.get(“https://www.jd.com”);
//driver封装为actions对象,扩展driver的鼠标键盘操作方法
//import org.openqa.selenium.interactions.Actions; (注意导入正确的包)
Actions actions=new Actions(driver);
//actions.moveToElement : 移动到selenium定位的元素上
//此处要有peform(),触发action操作
actions.moveToElement(driver.findElement(By.linkText(“运营商”))).perform();
Thread.sleep(3000);
driver.findElement(By.linkText(“固话宽带”)).click();
Thread.sleep(3000);
}

3、富文本框处理

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.util.thread.ThreadClassLoaderScope;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
*

  • @author Kitty
  • 演示富文本框输入格式化内容和嵌套Frame

*/
public class RichTextTest {
ChromeDriverService service;
WebDriver driver;

@BeforeMethod
public void setUp() throws InterruptedException, IOException {
	service = new ChromeDriverService.Builder()
			.usingDriverExecutable(new File("C:\\Windows\\System32\\chromedriver.exe")).usingAnyFreePort().build();
	service.start();
	driver = new ChromeDriver();
	driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	//cookie处理
	driver.get("http://121.40.156.59:8080/javamall/admin/backendUi!main.do");
	Cookie cookie = new Cookie("JSESSIONID", "4364E857214169F318345D57CCE24CE3");
	driver.manage().deleteAllCookies();
	driver.manage().addCookie(cookie);
	driver.get("http://121.40.156.59:8080/javamall/admin/backendUi!main.do");
	
	//Frame处理
	driver.findElement(By.cssSelector("#parent1 > a > div.icon > img")).click();
	driver.findElement(By.linkText("添加商品")).click();
	//跳入到添加商品Frame
	driver.switchTo().frame(1);
}

@AfterMethod
public void close() throws InterruptedException {
	driver.quit();
	service.stop();
}

@Test
public void testRichText() throws InterruptedException {
     //注意此时CSSSelector的值,页面操作可能会改变Css的值,抓取元素时需要保证页面没有其他的操作
	//(错误Css语法)#box_0 > li.selected
    // 依次选择商品
	driver.findElement(By.cssSelector("#box_0 > li:nth-child(3)")).click();
	driver.findElement(By.cssSelector("#box_35 > li")).click();
	driver.findElement(By.cssSelector("#box_36 > li")).click();
    //点击确定按钮
	driver.findElement(By.id("nextBtn")).click();
	//跳转到新页面,sleep增加稳定性
	Thread.sleep(2000);
	//商品名称
	driver.findElement(By.name("goods.name")).sendKeys("newProduct");
	//富文本框在嵌套frame中,需要再次跳入添加商品的子frame
	driver.switchTo().frame(0);
	//获取body元素,向body元素sendKeys来操作富文本框
	//<body spellcheck="false" class="cke_show_borders"><p><span style="background-color:yellow;">shiadafdsafav</span></p><p><span style="background-color:yellow;"><u><em></em></u>afasdfsa</span><br></p></body>
	driver.findElement(By.cssSelector("body")).sendKeys("<body spellcheck=\"false\" class=\"cke_show_borders\"><p><span style=\"background-color:yellow;\">shiadafdsafav</span></p><p><span style=\"background-color:yellow;\"><u><em></em></u>afasdfsa</span><br></p></body>");
	Thread.sleep(4000);
	//跳入父frame,查找确定按钮
	driver.switchTo().parentFrame();
	driver.findElement(By.cssSelector("#goodsinput > span")).click();
	Thread.sleep(4000);
}

}


4、时间控件 使用JavaScript方式

@Test
	public void test() throws InterruptedException {
		  // (Step1:登录)
	    driver.get("http://ask.testfan.cn/login");
	    // 输入用户名,密码
	    WebElement email = driver.findElement(By.name("email"));
	    email.sendKeys("2811920486@qq.com");
	    WebElement password = driver.findElement(By.name("password"));
	    password.sendKeys("XXXX");
	    Thread.sleep(2000);
	    WebElement btn = driver.findElement(By.className("btn-primary"));
	    // 点击确认登录按钮
	    btn.click();
	    Thread.sleep(2000);

	    // Step2: 进入个人主页
	    WebElement logo = driver.findElement(By.className("mr-5"));
	    logo.click();
	    WebElement settings = driver.findElement(By.linkText("账号设置"));
	    settings.click();
	    
	    //Step3: 编辑生日, 强制转换Javascript
	    JavascriptExecutor executor=(JavascriptExecutor)driver;
	    //通过Javascript语法注入:document.getElementById("birthday").value="1988-02-09"
        executor.executeScript("document.getElementById(\"birthday\").value=\"1988-02-09\"");
        
        Thread.sleep(5000);
        
        
	}

5、TestNG注解

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
 * 
 * @author Kitty
 * 通过单元测试演示如何使用TestNG注解
 *
 */
public class UnitTest {
	 Application application;
	   
    @BeforeSuite
    public void setUpSuite() {
    	   System.out.println(".....................BeforeSuite.......");
    }
	 
    @BeforeTest
    public void setUpTest() {
    	 System.out.println(".....................BeforeTest1......");
    }
    
    @BeforeClass
    public void setUpClass() {
    	System.out.println(".....................BeforeClass.....");
    }
    
    @BeforeMethod
   public void setup() {
	    System.out.println(".....................BeforeMethod.....");
	    application=new Application();
   }
    
	//expectedExceptions设置可接受的异常种类,用于异常测试
	@Test(expectedExceptions=RuntimeException.class, enabled=false)
	public void testNull() throws InterruptedException {
		 System.out.println("测试Null方法.......");
		 String actual= application.generateEmail(null);
	}
	
	//timeOut设置超时时间,如果超时则暂停测试方法,把测试方法标志为失败
	@Test(timeOut=5000)
	public  void testTimeout() throws InterruptedException {
		 System.out.println("测试超时方法.......");
		String actual= application.generateEmail("timeout");
	}
	//步骤3:在测试方法上通过添加dataProvider属性引入生成的数据
	//步骤4:给测试方法添加方法参数,此处test()变为test(String name)
	//步骤5:在测试方法中使用name变量完成测试
	@Test(dataProvider="getName")
	public void test(String name) throws InterruptedException {
		 System.out.println("测试正常方法.......");
	    String actual= application.generateEmail(name);
	    //Assert断言方法判定程序的返回是否为预期的返回
	    Assert.assertEquals(actual, name+"@testfan.com");
	}
	
	//dataprovider
	//步骤1: 构造dataprovider的方法, 方法的返回一定是Object[][]
	//步骤2: 给方法添加@DataProvider注解,并且标识name属性,其他测试方法通过name属性查找到dataprovider方法获取数据
	@DataProvider(name="getName")
	public Object[][] getData(){
		return new Object[][] {{"kitty"},{"aaa"},{"bbb"}};
	}
	
}

6、dataprovider使用

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
/**
 * 
 * @author Kitty
 * 演示如何用@Dataprovider实现UI测试的参数化
 * 
 */
public class UIDataProviderTest {
	ChromeDriverService service;
	WebDriver driver;
	
    @BeforeTest
    public void setUpTest() {
    	 System.out.println(".....................BeforeTest2......");
    }

	@BeforeMethod
	public void setUp() throws InterruptedException, IOException {
		service = new ChromeDriverService.Builder()
				.usingDriverExecutable(new File("C:\\Windows\\System32\\chromedriver.exe")).usingAnyFreePort().build();
		service.start();
		driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

	}

	@AfterMethod
	public void close() throws InterruptedException {
		driver.quit();
		service.stop();
	}

	//在方法上添加@Parameters注解:import org.testng.annotations.Parameters;
	//通过@Parameters注解里面的变量名匹配xml文件中的变量名,找到变量值,并完成输入
	@Parameters({"email","password"})
	@Test
	public void testByParameter(String email,String password) throws InterruptedException {
		// (Step1:登录)
		driver.get("http://ask.testfan.cn/login");
		// 输入用户名,密码
		WebElement emailElement = driver.findElement(By.name("email"));
		emailElement.sendKeys(email);
		WebElement passwordElement = driver.findElement(By.name("password"));
		passwordElement.sendKeys(password);
		Thread.sleep(2000);
		WebElement btn = driver.findElement(By.className("btn-primary"));
		// 点击确认登录按钮
		btn.click();
		Thread.sleep(2000);
		String currentUrl= driver.getCurrentUrl();
		System.out.println("---------------"+currentUrl);
	   Assert.assertEquals(currentUrl, "http://ask.testfan.cn/");
	}
	
	//@Test(dataProvider="loginData")
	public void test(String name,String password) throws InterruptedException {
		// (Step1:登录)
		driver.get("http://ask.testfan.cn/login");
		// 输入用户名,密码
		WebElement email = driver.findElement(By.name("email"));
		email.sendKeys(name);
		WebElement passwordElement = driver.findElement(By.name("password"));
		passwordElement.sendKeys(password);
		Thread.sleep(2000);
		WebElement btn = driver.findElement(By.className("btn-primary"));
		// 点击确认登录按钮
		btn.click();
		Thread.sleep(2000);
		String currentUrl= driver.getCurrentUrl();
		System.out.println("---------------"+currentUrl);
	   Assert.assertEquals(currentUrl, "http://ask.testfan.cn/");
	}
	
	@DataProvider(name="loginData")
	public Object[][] getData(){
	 return new Object[][] {{"2811920486@qq.com","123456"},{"2811920486@qq.com","XXX"}};
	}
	
	
}

7、test-basic.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<!-- Suite对应测试套件,一个Suite可以有多个Test -->
<suite name="我的Suite">
     <!-- 定义Parameter变量 -->
	<parameter name="email" value="2811920486@qq.com" />
	<parameter name="password" value="123456"></parameter>
	
	
	<!-- Test对应一个测试用例,里面可包含多个测试Class -->
	<test name="Test2">
		<classes>
			<class name="UIDataProviderTest" />
		</classes>
	</test>

	<!-- <test name="Test1">
		<classes>
			<class name="UnitTest" />
		</classes>
	</test> -->

</suite>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值