selenium-WebDriver API(java)(六)

本篇知识点:

  • 查看页面元素的属性
  • 获取页面元素的css属性值
  • 隐式等待
  • 常用显式等待
  • 自定义显示等待

查看页面元素的属性

被测试网页的网址:http://www.baidu.com

// 查看页面元素的属性
	@Test
	public void getWebElementAttribute() {
		driver.get("http://www.baidu.com");
		String inputString = "测试工程师指定的输入内容";
		WebElement input = driver.findElement(By.id("kw"));
		input.sendKeys(inputString);
		String inputText = input.getAttribute("value");
		Assert.assertEquals(inputText, inputString);
	}

获取页面元素的css属性值

被测试网页的网址:http://www.baidu.com

	// 获取页面元素的css属性值
	@Test(enabled = false)
	public void getWebElementCssValue() {
		driver.get("http://www.baidu.com");
		WebElement input = driver.findElement(By.id("kw"));
		String inputcss = input.getCssValue("width");
		System.out.println(inputcss);
		Assert.assertEquals("500px", inputcss);
	}

隐式等待

被测试网页的网址:http://www.baidu.com

// 隐式等待
	@Test(enabled = false)
	public void testImplictWait() {
		driver.get("http://www.baidu.com");
		// 使用implicitlyWait方法设定查找页面元素的最大等待时间。调用findElement方法时没有立刻找到定位元素,则程序会每间隔一定时间后不断尝试判断页面的DOM中是否出现被查找元素。
		// 若超过设定的等待时长依然没有找到,则抛出NoSuchElementException
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		try {
			WebElement input = driver.findElement(By.id("kw"));
			WebElement but = driver.findElement(By.id("su"));
			input.sendKeys("输入框元素被成功找到了!");
			but.click();
		} catch (NoSuchElementException e) {
			Assert.fail("找不到输入框");
			e.printStackTrace();
		}
	}

        隐式等待的默认等待最长时间是0,一旦设置,这个隐式等待会在WebDriver对象实例的整个生命周期起作用。

常用显式等待

被测试网页的网址:http://www.baidu.com

        显式等待比隐式等待更节约测试脚本执行的时间,一般推荐使用显式等待方式来判断页面元素是否存在。使用ExpectedConditions类中自带的方法,可以进行显式等待的判断。

        只有满足显式等待的条件要求,测试代码才会继续向后执行后续的测试逻辑。当显示等待条件未被满足的时候,在设定的最大显示等待时间阈值内,会停在当前代码位置进行等待,直到设定的等待条件被满足,才能继续执行后续的测试逻辑。如果超过设定的最大显式等待时间阈值,则测试程序会抛出异常,测试用例会被认为失败。

        常用的显示等待条件如下:

  • ExpectedConditions.elementToBeClickable(By locator):页面元素是否在页面上可用或可被单击
  • ExpectedConditions.elementToBeSelected(WebElement element):页面元素处于被选中状态
  • ExpectedConditions.presenceOfElementLocated(By locator):页面元素在页面中存在
  • ExpectedConditions.textToBePresentInElement(By locator):在页面元素中是否包含特定的文本
  • ExpectedConditions.textToBePresentInElementValue(By locator, java.lang.String text):页面元素值
  •  ExpectedConditions.titleContains(java.lang.String title):标题(title)
	// 显式等待
	@Test(enabled=false)
	public void testExplicitWait(){
		driver.get("http://www.baidu.com");
		//声明一个WedDriverWait对象,设定出发条件的最长等待时间为10秒
		WebDriverWait wait=new WebDriverWait(driver, 10);
		wait.until(ExpectedConditions.titleContains("百度"));
		System.out.println("网页标题出现了“百度”关键字");
		
		
		//使用ExpectedConditions中的 presenceOfAllElementsLocatedBy判断id为kw的这个输入框是否存在
		wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("kw")));
		System.out.println("页面中id为kw的元素已显示");
		
		//查找出百度页面中的“更多产品”链接
		WebElement element1 = driver.findElement(By.name("tj_briicon"));
		//调用ExpectedConditions中的textToBePresentInElement判断element1中是否包含“更多产品”这几个字。
		wait.until(ExpectedConditions.textToBePresentInElement(element1, "更多产品"));
		System.out.println("element1中包含“更多产品”这几个字");
	}

自定义显示等待

被测试网页:http://www.baidu.com
	//自定义的显式等待
	@Test(enabled = false)
	public void testExpectedCondition() {
		driver.get("http://www.baidu.com");
		WebElement input = driver.findElement(By.id("kw"));
		input.sendKeys("zTree");
		input.submit();

		// 使用ExpectedConditions的静态方法titleContain来实现比较title里是否含有“zTree”
		new WebDriverWait(driver, 10).until(ExpectedConditions.titleContains("zTree"));

		// 使用ExpectedCondition接口,自定义方法来比较title全变成小写后是否含有“ztree”。
		// 使用ExpectedCondition接口,一定要实现apply方法
		new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
			public Boolean apply(WebDriver arg0) {
				// TODO Auto-generated method stub
				return driver.getTitle().toLowerCase().startsWith("ztree");
			}
		});
	}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值