selenium-WebDriver API(java)(四)

本篇知识点:

  • 杀掉windows的浏览器进程
  • 将当前浏览器的窗口截屏
  • 检查页面元素的文本内容是否出现
  • 执行JavaScript脚本
  • 拖拽页面元素

杀掉windows的浏览器进程

打开firefox、ie、chrome浏览器。

	//杀掉windows的浏览器进程
	@Test
	public void operateWindowsProcess(){
		//杀掉windows进程中的Firefox浏览器进程,关闭所有Firefox浏览器
		WindowsUtils.tryToKillByName("firefox.exe");
		//杀掉windows进程中的IE浏览器进程,关闭所有IE浏览器
		WindowsUtils.tryToKillByName("iexplore.exe");
		//杀掉windows进程中的chrome浏览器进程,关闭所有chrome浏览器
		WindowsUtils.tryToKillByName("chrome.exe");
	}

将当前浏览器的窗口截屏

	//将当前浏览器的窗口截屏
	@Test
	public void captureScreenInCurrentWindow(){
		driver.get("http://www.baidu.com");
		File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
		try {
			FileUtils.copyFile(srcFile, new File("C:\\testing\\test.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

检查页面元素的文本内容是否出现

被测试网页:http://www.baidu.com ,判断的文字为“新闻”

	//检查页面元素的文本内容是否出现
		@Test
		public void isElementTextPresent(){
			//读取百度首页,包含新闻字样的链接
			WebElement text=driver.findElement(By.xpath("//div[@id='u_sp']/a[1]"));
			String contentText=text.getText();
			//判断整个元素的文字是否和“新闻”完全匹配。
			Assert.assertEquals("新闻", contentText);	
			//判断是否包含“新闻”
			Assert.assertTrue(contentText.contains("新闻"));
			//判断是否以“新”字开头
			Assert.assertTrue(contentText.startsWith("新"));
			//判断是否以“闻”字结尾
			Assert.assertTrue(contentText.endsWith("闻"));
		}

执行JavaScript脚本

被测试网页:http://www.sogou.com

// 执行JavaScript脚本
	@Test
	public void excuteJavaScrio() {
		String url = "http://www.sogou.com";
		driver.navigate().to(url);

		JavascriptExecutor js = (JavascriptExecutor) driver;
		String title = (String) js.executeScript("return document.title");

		Assert.assertEquals("搜狗搜索引擎 - 上网从搜狗开始", title);
		String searchButtonTest = (String) js.executeScript("var but=document.getElementById('stb');return but.value;");
		System.out.println(searchButtonTest);
	}

拖曳页面元素

被测试页面:http://jqueryui.com/resources/demos/draggable/scroll.html

// 拖拽页面元素
	@Test
	public void dragPageElement() {
		driver.get("http://jqueryui.com/resources/demos/draggable/scroll.html");
		// 找到页面上第一个能被拖拽的方框页面对象
		WebElement draggable = driver.findElement(By.id("draggable"));
		// 向下拖动10个像素,共拖动5次
		for (int i = 0; i < 5; i++) {
			// 10表示元素的纵向坐标向下移动10个像素。0表示元素的横坐标不变
			new Actions(driver).dragAndDropBy(draggable, 0, 10).build().perform();
		}
		// 向右拖动10个像素,共拖动5次
		for (int i = 0; i < 5; i++) {
			// 10表示元素的横坐标向右移动10个像素,0表示元素的纵坐标不变
			new Actions(driver).dragAndDropBy(draggable, 10, 0).build().perform();
		}
	}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值